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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // @flow
  2. import { MiddlewareRegistry } from '../base/redux';
  3. import {
  4. CLEAR_TOOLBOX_TIMEOUT,
  5. SET_FULL_SCREEN,
  6. SET_TOOLBOX_TIMEOUT
  7. } from './actionTypes';
  8. import './subscriber';
  9. declare var APP: Object;
  10. /**
  11. * Middleware which intercepts Toolbox actions to handle changes to the
  12. * visibility timeout of the Toolbox.
  13. *
  14. * @param {Store} store - The redux store.
  15. * @returns {Function}
  16. */
  17. MiddlewareRegistry.register(store => next => action => {
  18. switch (action.type) {
  19. case CLEAR_TOOLBOX_TIMEOUT: {
  20. const { timeoutID } = store.getState()['features/toolbox'];
  21. clearTimeout(timeoutID);
  22. break;
  23. }
  24. case SET_FULL_SCREEN:
  25. return _setFullScreen(next, action);
  26. case SET_TOOLBOX_TIMEOUT: {
  27. const { timeoutID } = store.getState()['features/toolbox'];
  28. const { handler, timeoutMS } = action;
  29. clearTimeout(timeoutID);
  30. action.timeoutID = setTimeout(handler, timeoutMS);
  31. break;
  32. }
  33. }
  34. return next(action);
  35. });
  36. type DocumentElement = {
  37. +requestFullscreen?: Function,
  38. +mozRequestFullScreen?: Function,
  39. +webkitRequestFullscreen?: Function
  40. }
  41. /**
  42. * Makes an external request to enter or exit full screen mode.
  43. *
  44. * @param {Dispatch} next - The redux dispatch function to dispatch the
  45. * specified action to the specified store.
  46. * @param {Action} action - The redux action SET_FULL_SCREEN which is being
  47. * dispatched in the specified store.
  48. * @private
  49. * @returns {Object} The value returned by {@code next(action)}.
  50. */
  51. function _setFullScreen(next, action) {
  52. const result = next(action);
  53. if (typeof APP === 'object') {
  54. const { fullScreen } = action;
  55. if (fullScreen) {
  56. const documentElement: DocumentElement
  57. = document.documentElement || {};
  58. if (typeof documentElement.requestFullscreen === 'function') {
  59. documentElement.requestFullscreen();
  60. } else if (
  61. typeof documentElement.mozRequestFullScreen === 'function') {
  62. documentElement.mozRequestFullScreen();
  63. } else if (
  64. typeof documentElement.webkitRequestFullscreen === 'function') {
  65. documentElement.webkitRequestFullscreen();
  66. }
  67. return result;
  68. }
  69. if (typeof document.exitFullscreen === 'function') {
  70. document.exitFullscreen();
  71. // $FlowExpectedError
  72. } else if (typeof document.mozCancelFullScreen === 'function') {
  73. // $FlowExpectedError
  74. document.mozCancelFullScreen();
  75. // $FlowExpectedError
  76. } else if (typeof document.webkitExitFullscreen === 'function') {
  77. // $FlowExpectedError
  78. document.webkitExitFullscreen();
  79. }
  80. }
  81. return result;
  82. }