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.

middleware.web.ts 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { batch } from 'react-redux';
  2. import { AnyAction } from 'redux';
  3. import { IReduxState } from '../app/types';
  4. import { OVERWRITE_CONFIG, SET_CONFIG, UPDATE_CONFIG } from '../base/config/actionTypes';
  5. import { NotifyClickButton } from '../base/config/configType';
  6. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  7. import { I_AM_VISITOR_MODE } from '../visitors/actionTypes';
  8. import { iAmVisitor } from '../visitors/functions';
  9. import {
  10. CLEAR_TOOLBOX_TIMEOUT,
  11. SET_BUTTONS_WITH_NOTIFY_CLICK,
  12. SET_FULL_SCREEN,
  13. SET_PARTICIPANT_MENU_BUTTONS_WITH_NOTIFY_CLICK,
  14. SET_TOOLBAR_BUTTONS,
  15. SET_TOOLBOX_TIMEOUT
  16. } from './actionTypes';
  17. import { TOOLBAR_BUTTONS, VISITORS_MODE_BUTTONS } from './constants';
  18. import { NOTIFY_CLICK_MODE } from './types';
  19. import './subscriber.web';
  20. /**
  21. * Middleware which intercepts Toolbox actions to handle changes to the
  22. * visibility timeout of the Toolbox.
  23. *
  24. * @param {Store} store - The redux store.
  25. * @returns {Function}
  26. */
  27. MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case CLEAR_TOOLBOX_TIMEOUT: {
  30. const { timeoutID } = store.getState()['features/toolbox'];
  31. clearTimeout(timeoutID ?? undefined);
  32. break;
  33. }
  34. case UPDATE_CONFIG:
  35. case OVERWRITE_CONFIG:
  36. case I_AM_VISITOR_MODE:
  37. case SET_CONFIG: {
  38. const result = next(action);
  39. const { dispatch, getState } = store;
  40. const state = getState();
  41. if (action.type !== I_AM_VISITOR_MODE) {
  42. const {
  43. customToolbarButtons,
  44. buttonsWithNotifyClick,
  45. participantMenuButtonsWithNotifyClick,
  46. customParticipantMenuButtons
  47. } = state['features/base/config'];
  48. batch(() => {
  49. dispatch({
  50. type: SET_BUTTONS_WITH_NOTIFY_CLICK,
  51. buttonsWithNotifyClick: _buildButtonsArray(buttonsWithNotifyClick, customToolbarButtons)
  52. });
  53. dispatch({
  54. type: SET_PARTICIPANT_MENU_BUTTONS_WITH_NOTIFY_CLICK,
  55. participantMenuButtonsWithNotifyClick:
  56. _buildButtonsArray(participantMenuButtonsWithNotifyClick, customParticipantMenuButtons)
  57. });
  58. });
  59. }
  60. const toolbarButtons = _getToolbarButtons(state);
  61. dispatch({
  62. type: SET_TOOLBAR_BUTTONS,
  63. toolbarButtons
  64. });
  65. return result;
  66. }
  67. case SET_FULL_SCREEN:
  68. return _setFullScreen(next, action);
  69. case SET_TOOLBOX_TIMEOUT: {
  70. const { timeoutID } = store.getState()['features/toolbox'];
  71. const { handler, timeoutMS }: { handler: Function; timeoutMS: number; } = action;
  72. clearTimeout(timeoutID ?? undefined);
  73. action.timeoutID = setTimeout(handler, timeoutMS);
  74. break;
  75. }
  76. }
  77. return next(action);
  78. });
  79. type DocumentElement = {
  80. requestFullscreen?: Function;
  81. webkitRequestFullscreen?: Function;
  82. };
  83. /**
  84. * Makes an external request to enter or exit full screen mode.
  85. *
  86. * @param {Dispatch} next - The redux dispatch function to dispatch the
  87. * specified action to the specified store.
  88. * @param {Action} action - The redux action SET_FULL_SCREEN which is being
  89. * dispatched in the specified store.
  90. * @private
  91. * @returns {Object} The value returned by {@code next(action)}.
  92. */
  93. function _setFullScreen(next: Function, action: AnyAction) {
  94. const result = next(action);
  95. const { fullScreen } = action;
  96. if (fullScreen) {
  97. const documentElement: DocumentElement
  98. = document.documentElement || {};
  99. if (typeof documentElement.requestFullscreen === 'function') {
  100. documentElement.requestFullscreen();
  101. } else if (
  102. typeof documentElement.webkitRequestFullscreen === 'function') {
  103. documentElement.webkitRequestFullscreen();
  104. }
  105. return result;
  106. }
  107. if (typeof document.exitFullscreen === 'function') {
  108. document.exitFullscreen();
  109. } else if (typeof document.webkitExitFullscreen === 'function') {
  110. document.webkitExitFullscreen();
  111. }
  112. return result;
  113. }
  114. /**
  115. * Common logic to gather buttons that have to notify the api when clicked.
  116. *
  117. * @param {Array} buttonsWithNotifyClick - The array of systme buttons that need to notify the api.
  118. * @param {Array} customButtons - The custom buttons.
  119. * @returns {Array}
  120. */
  121. function _buildButtonsArray(
  122. buttonsWithNotifyClick?: NotifyClickButton[],
  123. customButtons?: {
  124. icon: string;
  125. id: string;
  126. text: string;
  127. }[]
  128. ): Map<string, NOTIFY_CLICK_MODE> {
  129. const customButtonsWithNotifyClick = customButtons?.map(
  130. ({ id }) => ([ id, NOTIFY_CLICK_MODE.ONLY_NOTIFY ]) as [string, NOTIFY_CLICK_MODE]) ?? [];
  131. const buttons = (Array.isArray(buttonsWithNotifyClick) ? buttonsWithNotifyClick : [])
  132. .filter(button => typeof button === 'string' || (typeof button === 'object' && typeof button.key === 'string'))
  133. .map(button => {
  134. if (typeof button === 'string') {
  135. return [ button, NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY ] as [string, NOTIFY_CLICK_MODE];
  136. }
  137. return [
  138. button.key,
  139. button.preventExecution ? NOTIFY_CLICK_MODE.PREVENT_AND_NOTIFY : NOTIFY_CLICK_MODE.ONLY_NOTIFY
  140. ] as [string, NOTIFY_CLICK_MODE];
  141. });
  142. return new Map([ ...customButtonsWithNotifyClick, ...buttons ]);
  143. }
  144. /**
  145. * Returns the list of enabled toolbar buttons.
  146. *
  147. * @param {Object} state - The redux state.
  148. * @returns {Array<string>} - The list of enabled toolbar buttons.
  149. */
  150. function _getToolbarButtons(state: IReduxState): Array<string> {
  151. const { toolbarButtons, customToolbarButtons } = state['features/base/config'];
  152. const customButtons = customToolbarButtons?.map(({ id }) => id);
  153. let buttons = Array.isArray(toolbarButtons) ? toolbarButtons : TOOLBAR_BUTTONS;
  154. if (iAmVisitor(state)) {
  155. buttons = VISITORS_MODE_BUTTONS.filter(button => buttons.indexOf(button) > -1);
  156. }
  157. if (customButtons) {
  158. return [ ...buttons, ...customButtons ];
  159. }
  160. return buttons;
  161. }