Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.web.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if (props.tooltipText) {
  36. result.content = props.tooltipText;
  37. }
  38. return result;
  39. }
  40. /* eslint-enable flowtype/space-before-type-colon */
  41. /**
  42. * Returns an object which contains the default buttons for the primary and
  43. * secondary toolbars.
  44. *
  45. * @param {Object} buttonHandlers - Contains additional toolbox button
  46. * handlers.
  47. * @returns {Object}
  48. */
  49. export function getDefaultToolboxButtons(buttonHandlers: Object): Object {
  50. let toolbarButtons = {
  51. primaryToolbarButtons: new Map(),
  52. secondaryToolbarButtons: new Map()
  53. };
  54. if (typeof interfaceConfig !== 'undefined'
  55. && interfaceConfig.TOOLBAR_BUTTONS) {
  56. toolbarButtons
  57. = interfaceConfig.TOOLBAR_BUTTONS.reduce(
  58. (acc, buttonName) => {
  59. let button = defaultToolbarButtons[buttonName];
  60. const currentButtonHandlers = buttonHandlers[buttonName];
  61. if (button) {
  62. const place = _getToolbarButtonPlace(buttonName);
  63. button.buttonName = buttonName;
  64. if (currentButtonHandlers) {
  65. button = {
  66. ...button,
  67. ...currentButtonHandlers
  68. };
  69. }
  70. // If isDisplayed method is not defined, display the
  71. // button only for non-filmstripOnly mode
  72. if (button.isDisplayed()) {
  73. acc[place].set(buttonName, button);
  74. }
  75. }
  76. return acc;
  77. },
  78. toolbarButtons);
  79. }
  80. return toolbarButtons;
  81. }
  82. /**
  83. * Returns toolbar class names to add while rendering.
  84. *
  85. * @param {Object} props - Props object pass to React component.
  86. * @returns {Object}
  87. * @private
  88. */
  89. export function getToolbarClassNames(props: Object) {
  90. const primaryToolbarClassNames = [
  91. interfaceConfig.filmStripOnly
  92. ? 'toolbar_filmstrip-only'
  93. : 'toolbar_primary'
  94. ];
  95. const secondaryToolbarClassNames = [ 'toolbar_secondary' ];
  96. if (props._visible) {
  97. const slideInAnimation
  98. = SideContainerToggler.isVisible ? 'slideInExtX' : 'slideInX';
  99. primaryToolbarClassNames.push('fadeIn');
  100. secondaryToolbarClassNames.push(slideInAnimation);
  101. } else {
  102. const slideOutAnimation
  103. = SideContainerToggler.isVisible ? 'slideOutExtX' : 'slideOutX';
  104. primaryToolbarClassNames.push('fadeOut');
  105. secondaryToolbarClassNames.push(slideOutAnimation);
  106. }
  107. return {
  108. primaryToolbarClassName: primaryToolbarClassNames.join(' '),
  109. secondaryToolbarClassName: secondaryToolbarClassNames.join(' ')
  110. };
  111. }
  112. /**
  113. * Indicates if a toolbar button is enabled.
  114. *
  115. * @param {string} name - The name of the setting section as defined in
  116. * interface_config.js.
  117. * @returns {boolean} - True to indicate that the given toolbar button
  118. * is enabled, false - otherwise.
  119. */
  120. export function isButtonEnabled(name) {
  121. return interfaceConfig.TOOLBAR_BUTTONS.indexOf(name) !== -1
  122. || interfaceConfig.MAIN_TOOLBAR_BUTTONS.indexOf(name) !== -1;
  123. }
  124. /**
  125. * Show custom popup/tooltip for a specified button.
  126. *
  127. * @param {string} popupSelectorID - The selector id of the popup to show.
  128. * @param {boolean} show - True or false/show or hide the popup.
  129. * @param {number} timeout - The time to show the popup.
  130. * @returns {void}
  131. */
  132. export function showCustomToolbarPopup(
  133. popupSelectorID: string,
  134. show: boolean,
  135. timeout: number) {
  136. AJS.$(popupSelectorID).tooltip({
  137. gravity: $(popupSelectorID).attr('data-popup'),
  138. html: true,
  139. title: 'title',
  140. trigger: 'manual'
  141. });
  142. if (show) {
  143. AJS.$(popupSelectorID).tooltip('show');
  144. setTimeout(
  145. () => {
  146. // hide the tooltip
  147. AJS.$(popupSelectorID).tooltip('hide');
  148. },
  149. timeout);
  150. } else {
  151. AJS.$(popupSelectorID).tooltip('hide');
  152. }
  153. }
  154. /**
  155. * Get place for toolbar button. Now it can be in the primary Toolbar or in the
  156. * secondary Toolbar.
  157. *
  158. * @param {string} btn - Button name.
  159. * @private
  160. * @returns {string}
  161. */
  162. function _getToolbarButtonPlace(btn) {
  163. return (
  164. interfaceConfig.MAIN_TOOLBAR_BUTTONS.includes(btn)
  165. ? 'primaryToolbarButtons'
  166. : 'secondaryToolbarButtons');
  167. }