You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.web.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import SideContainerToggler
  2. from '../../../modules/UI/side_pannels/SideContainerToggler';
  3. import defaultToolbarButtons from './defaultToolbarButtons';
  4. type MapOfAttributes = { [key: string]: * };
  5. declare var $: Function;
  6. declare var AJS: Object;
  7. declare var interfaceConfig: Object;
  8. export { abstractMapStateToProps } from './functions.native';
  9. /* eslint-disable flowtype/space-before-type-colon */
  10. /**
  11. * Takes toolbar button props and maps them to HTML attributes to set.
  12. *
  13. * @param {Object} props - Props set to the React component.
  14. * @returns {MapOfAttributes}
  15. */
  16. export function getButtonAttributesByProps(props: Object = {})
  17. : MapOfAttributes {
  18. // XXX Make sure to not modify props.classNames because that'd be bad
  19. // practice.
  20. const classNames = (props.classNames && [ ...props.classNames ]) || [];
  21. props.toggled && classNames.push('toggled');
  22. props.unclickable && classNames.push('unclickable');
  23. const result: MapOfAttributes = {
  24. className: classNames.join(' '),
  25. 'data-container': 'body',
  26. 'data-placement': 'bottom',
  27. id: props.id
  28. };
  29. if (!props.enabled) {
  30. result.disabled = 'disabled';
  31. }
  32. if (props.hidden) {
  33. result.style = { display: 'none' };
  34. }
  35. return result;
  36. }
  37. /* eslint-enable flowtype/space-before-type-colon */
  38. /**
  39. * Returns an object which contains the default buttons for the primary and
  40. * secondary toolbars.
  41. *
  42. * @param {Object} buttonHandlers - Contains additional toolbox button
  43. * handlers.
  44. * @returns {Object}
  45. */
  46. export function getDefaultToolboxButtons(buttonHandlers: Object): Object {
  47. let toolbarButtons = {
  48. primaryToolbarButtons: new Map(),
  49. secondaryToolbarButtons: new Map()
  50. };
  51. if (typeof interfaceConfig !== 'undefined'
  52. && interfaceConfig.TOOLBAR_BUTTONS) {
  53. toolbarButtons
  54. = interfaceConfig.TOOLBAR_BUTTONS.reduce(
  55. (acc, buttonName) => {
  56. let button = defaultToolbarButtons[buttonName];
  57. const currentButtonHandlers = buttonHandlers[buttonName];
  58. if (button) {
  59. const place = _getToolbarButtonPlace(buttonName);
  60. button.buttonName = buttonName;
  61. if (currentButtonHandlers) {
  62. button = {
  63. ...button,
  64. ...currentButtonHandlers
  65. };
  66. }
  67. // If isDisplayed method is not defined, display the
  68. // button only for non-filmstripOnly mode
  69. if (button.isDisplayed()) {
  70. acc[place].set(buttonName, button);
  71. }
  72. }
  73. return acc;
  74. },
  75. toolbarButtons);
  76. }
  77. return toolbarButtons;
  78. }
  79. /**
  80. * Returns toolbar class names to add while rendering.
  81. *
  82. * @param {Object} props - Props object pass to React component.
  83. * @returns {Object}
  84. * @private
  85. */
  86. export function getToolbarClassNames(props: Object) {
  87. const primaryToolbarClassNames = [
  88. interfaceConfig.filmStripOnly
  89. ? 'toolbar_filmstrip-only'
  90. : 'toolbar_primary'
  91. ];
  92. const secondaryToolbarClassNames = [ 'toolbar_secondary' ];
  93. if (props._visible) {
  94. const slideInAnimation
  95. = SideContainerToggler.isVisible ? 'slideInExtX' : 'slideInX';
  96. primaryToolbarClassNames.push('fadeIn');
  97. secondaryToolbarClassNames.push(slideInAnimation);
  98. } else {
  99. const slideOutAnimation
  100. = SideContainerToggler.isVisible ? 'slideOutExtX' : 'slideOutX';
  101. primaryToolbarClassNames.push('fadeOut');
  102. secondaryToolbarClassNames.push(slideOutAnimation);
  103. }
  104. return {
  105. primaryToolbarClassName: primaryToolbarClassNames.join(' '),
  106. secondaryToolbarClassName: secondaryToolbarClassNames.join(' ')
  107. };
  108. }
  109. /**
  110. * Show custom popup/tooltip for a specified button.
  111. *
  112. * @param {string} popupSelectorID - The selector id of the popup to show.
  113. * @param {boolean} show - True or false/show or hide the popup.
  114. * @param {number} timeout - The time to show the popup.
  115. * @returns {void}
  116. */
  117. export function showCustomToolbarPopup(
  118. popupSelectorID: string,
  119. show: boolean,
  120. timeout: number) {
  121. AJS.$(popupSelectorID).tooltip({
  122. gravity: $(popupSelectorID).attr('data-popup'),
  123. html: true,
  124. title: 'title',
  125. trigger: 'manual'
  126. });
  127. if (show) {
  128. AJS.$(popupSelectorID).tooltip('show');
  129. setTimeout(
  130. () => {
  131. // hide the tooltip
  132. AJS.$(popupSelectorID).tooltip('hide');
  133. },
  134. timeout);
  135. } else {
  136. AJS.$(popupSelectorID).tooltip('hide');
  137. }
  138. }
  139. /**
  140. * Get place for toolbar button. Now it can be in the primary Toolbar or in the
  141. * secondary Toolbar.
  142. *
  143. * @param {string} btn - Button name.
  144. * @private
  145. * @returns {string}
  146. */
  147. function _getToolbarButtonPlace(btn) {
  148. return (
  149. interfaceConfig.MAIN_TOOLBAR_BUTTONS.includes(btn)
  150. ? 'primaryToolbarButtons'
  151. : 'secondaryToolbarButtons');
  152. }