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.

actions.any.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { getLocationContextRoot } from '../base/util';
  4. import { addTrackStateToURL } from './functions.any';
  5. /**
  6. * Redirects to another page generated by replacing the path in the original URL
  7. * with the given path.
  8. *
  9. * @param {(string)} pathname - The path to navigate to.
  10. * @returns {Function}
  11. */
  12. export function redirectWithStoredParams(pathname: string) {
  13. return (dispatch: Dispatch<any>, getState: Function) => {
  14. const { locationURL } = getState()['features/base/connection'];
  15. const newLocationURL = new URL(locationURL.href);
  16. newLocationURL.pathname = pathname;
  17. window.location.assign(newLocationURL.toString());
  18. };
  19. }
  20. /**
  21. * Assigns a specific pathname to window.location.pathname taking into account
  22. * the context root of the Web app.
  23. *
  24. * @param {string} pathname - The pathname to assign to
  25. * window.location.pathname. If the specified pathname is relative, the context
  26. * root of the Web app will be prepended to the specified pathname before
  27. * assigning it to window.location.pathname.
  28. * @param {string} hashParam - Optional hash param to assign to
  29. * window.location.hash.
  30. * @returns {Function}
  31. */
  32. export function redirectToStaticPage(pathname: string, hashParam: ?string) {
  33. return () => {
  34. const windowLocation = window.location;
  35. let newPathname = pathname;
  36. if (!newPathname.startsWith('/')) {
  37. // A pathname equal to ./ specifies the current directory. It will be
  38. // fine but pointless to include it because contextRoot is the current
  39. // directory.
  40. newPathname.startsWith('./')
  41. && (newPathname = newPathname.substring(2));
  42. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  43. }
  44. if (hashParam) {
  45. windowLocation.hash = hashParam;
  46. }
  47. windowLocation.pathname = newPathname;
  48. };
  49. }
  50. /**
  51. * Reloads the page by restoring the original URL.
  52. *
  53. * @returns {Function}
  54. */
  55. export function reloadWithStoredParams() {
  56. return (dispatch: Dispatch<any>, getState: Function) => {
  57. const state = getState();
  58. const { locationURL } = state['features/base/connection'];
  59. // Preserve the local tracks muted states.
  60. const newURL = addTrackStateToURL(locationURL, state);
  61. const windowLocation = window.location;
  62. const oldSearchString = windowLocation.search;
  63. windowLocation.replace(newURL.toString());
  64. if (newURL.search === oldSearchString) {
  65. // NOTE: Assuming that only the hash or search part of the URL will
  66. // be changed!
  67. // location.replace will not trigger redirect/reload when
  68. // only the hash params are changed. That's why we need to call
  69. // reload in addition to replace.
  70. windowLocation.reload();
  71. }
  72. };
  73. }