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.

App.web.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { appInit } from '../actions';
  2. import { AbstractApp } from './AbstractApp';
  3. /**
  4. * Root application component.
  5. *
  6. * @extends AbstractApp
  7. */
  8. export class App extends AbstractApp {
  9. /**
  10. * App component's property types.
  11. *
  12. * @static
  13. */
  14. static propTypes = AbstractApp.propTypes
  15. /**
  16. * Initializes a new App instance.
  17. *
  18. * @param {Object} props - The read-only React Component props with which
  19. * the new instance is to be initialized.
  20. */
  21. constructor(props) {
  22. super(props);
  23. this.state = {
  24. ...this.state,
  25. /**
  26. * The context root of window.location i.e. this Web App.
  27. *
  28. * @type {string}
  29. */
  30. windowLocationContextRoot: this._getWindowLocationContextRoot()
  31. };
  32. }
  33. /**
  34. * Inits the app before component will mount.
  35. *
  36. * @inheritdoc
  37. */
  38. componentWillMount(...args) {
  39. super.componentWillMount(...args);
  40. this._getStore().dispatch(appInit());
  41. }
  42. /**
  43. * Gets a Location object from the window with information about the current
  44. * location of the document.
  45. *
  46. * @inheritdoc
  47. */
  48. getWindowLocation() {
  49. return window.location;
  50. }
  51. /**
  52. * Gets the context root of this Web App from window.location.
  53. *
  54. * @private
  55. * @returns {string} The context root of window.location i.e. this Web App.
  56. */
  57. _getWindowLocationContextRoot() {
  58. const pathname = this.getWindowLocation().pathname;
  59. const contextRootEndIndex = pathname.lastIndexOf('/');
  60. return (
  61. contextRootEndIndex === -1
  62. ? '/'
  63. : pathname.substring(0, contextRootEndIndex + 1));
  64. }
  65. /**
  66. * Navigates to a specific Route (via platform-specific means).
  67. *
  68. * @param {Route} route - The Route to which to navigate.
  69. * @protected
  70. * @returns {void}
  71. */
  72. _navigate(route) {
  73. let path = route.path;
  74. const store = this._getStore();
  75. // The syntax :room bellow is defined by react-router. It "matches a URL
  76. // segment up to the next /, ?, or #. The matched string is called a
  77. // param."
  78. path
  79. = path.replace(
  80. /:room/g,
  81. store.getState()['features/base/conference'].room);
  82. path = this._routePath2WindowLocationPathname(path);
  83. // Navigate to the specified Route.
  84. const windowLocation = this.getWindowLocation();
  85. if (windowLocation.pathname === path) {
  86. // The browser is at the specified path already and what remains is
  87. // to make this App instance aware of the route to be rendered at
  88. // the current location.
  89. super._navigate(route);
  90. } else {
  91. // The browser must go to the specified location. Once the specified
  92. // location becomes current, the App will be made aware of the route
  93. // to be rendered at it.
  94. windowLocation.pathname = path;
  95. }
  96. }
  97. /**
  98. * Converts a specific Route path to a window.location.pathname.
  99. *
  100. * @param {string} path - A Route path to be converted to/represeted as a
  101. * window.location.pathname.
  102. * @private
  103. * @returns {string} A window.location.pathname-compatible representation of
  104. * the specified Route path.
  105. */
  106. _routePath2WindowLocationPathname(path) {
  107. let pathname = this.state.windowLocationContextRoot;
  108. pathname.endsWith('/') || (pathname += '/');
  109. pathname += path.startsWith('/') ? path.substring(1) : path;
  110. return pathname;
  111. }
  112. }