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.7KB

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