您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.any.ts 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { getLocationContextRoot } from '../base/util/uri';
  2. import { addTrackStateToURL } from './functions.any';
  3. import { IStore } from './types';
  4. /**
  5. * Redirects to another page generated by replacing the path in the original URL
  6. * with the given path.
  7. *
  8. * @param {(string)} pathname - The path to navigate to.
  9. * @returns {Function}
  10. */
  11. export function redirectWithStoredParams(pathname: string) {
  12. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  13. const { locationURL } = getState()['features/base/connection'];
  14. const newLocationURL = new URL(locationURL?.href ?? '');
  15. newLocationURL.pathname = pathname;
  16. window.location.assign(newLocationURL.toString());
  17. };
  18. }
  19. /**
  20. * Assigns a specific pathname to window.location.pathname taking into account
  21. * the context root of the Web app.
  22. *
  23. * @param {string} pathname - The pathname to assign to
  24. * window.location.pathname. If the specified pathname is relative, the context
  25. * root of the Web app will be prepended to the specified pathname before
  26. * assigning it to window.location.pathname.
  27. * @param {string} hashParam - Optional hash param to assign to
  28. * window.location.hash.
  29. * @returns {Function}
  30. */
  31. export function redirectToStaticPage(pathname: string, hashParam?: string) {
  32. return () => {
  33. const windowLocation = window.location;
  34. let newPathname = pathname;
  35. if (!newPathname.startsWith('/')) {
  36. // A pathname equal to ./ specifies the current directory. It will be
  37. // fine but pointless to include it because contextRoot is the current
  38. // directory.
  39. newPathname.startsWith('./')
  40. && (newPathname = newPathname.substring(2));
  41. newPathname = getLocationContextRoot(windowLocation) + newPathname;
  42. }
  43. if (hashParam) {
  44. windowLocation.hash = hashParam;
  45. }
  46. windowLocation.pathname = newPathname;
  47. };
  48. }
  49. /**
  50. * Reloads the page by restoring the original URL.
  51. *
  52. * @returns {Function}
  53. */
  54. export function reloadWithStoredParams() {
  55. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  56. const state = getState();
  57. const { locationURL } = state['features/base/connection'];
  58. // Preserve the local tracks muted states.
  59. // @ts-ignore
  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. }