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

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