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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * Show custom popup/tooltip for a specified button.
  114. *
  115. * @param {string} popupSelectorID - The selector id of the popup to show.
  116. * @param {boolean} show - True or false/show or hide the popup.
  117. * @param {number} timeout - The time to show the popup.
  118. * @returns {void}
  119. */
  120. export function showCustomToolbarPopup(
  121. popupSelectorID: string,
  122. show: boolean,
  123. timeout: number) {
  124. AJS.$(popupSelectorID).tooltip({
  125. gravity: $(popupSelectorID).attr('data-popup'),
  126. html: true,
  127. title: 'title',
  128. trigger: 'manual'
  129. });
  130. if (show) {
  131. AJS.$(popupSelectorID).tooltip('show');
  132. setTimeout(
  133. () => {
  134. // hide the tooltip
  135. AJS.$(popupSelectorID).tooltip('hide');
  136. },
  137. timeout);
  138. } else {
  139. AJS.$(popupSelectorID).tooltip('hide');
  140. }
  141. }
  142. /**
  143. * Get place for toolbar button. Now it can be in the primary Toolbar or in the
  144. * secondary Toolbar.
  145. *
  146. * @param {string} btn - Button name.
  147. * @private
  148. * @returns {string}
  149. */
  150. function _getToolbarButtonPlace(btn) {
  151. return (
  152. interfaceConfig.MAIN_TOOLBAR_BUTTONS.includes(btn)
  153. ? 'primaryToolbarButtons'
  154. : 'secondaryToolbarButtons');
  155. }