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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import React from 'react';
  2. import { Provider } from 'react-redux';
  3. import {
  4. browserHistory,
  5. Route,
  6. Router
  7. } from 'react-router';
  8. import { push, syncHistoryWithStore } from 'react-router-redux';
  9. import { getDomain } from '../../base/connection';
  10. import { RouteRegistry } from '../../base/navigator';
  11. import { AbstractApp } from './AbstractApp';
  12. /**
  13. * Root application component.
  14. *
  15. * @extends AbstractApp
  16. */
  17. export class App extends AbstractApp {
  18. /**
  19. * Initializes a new App instance.
  20. *
  21. * @param {Object} props - The read-only React Component props with which
  22. * the new instance is to be initialized.
  23. */
  24. constructor(props) {
  25. super(props);
  26. /**
  27. * Create an enhanced history that syncs navigation events with the
  28. * store.
  29. * @link https://github.com/reactjs/react-router-redux#how-it-works
  30. */
  31. this.history = syncHistoryWithStore(browserHistory, props.store);
  32. // Bind event handlers so they are only bound once for every instance.
  33. this._onRouteEnter = this._onRouteEnter.bind(this);
  34. this._routerCreateElement = this._routerCreateElement.bind(this);
  35. }
  36. /**
  37. * Implements React's {@link Component#render()}.
  38. *
  39. * @inheritdoc
  40. * @returns {ReactElement}
  41. */
  42. render() {
  43. const routes = RouteRegistry.getRoutes();
  44. /* eslint-disable no-extra-parens */
  45. return (
  46. <Provider store = { this.props.store }>
  47. <Router
  48. createElement = { this._routerCreateElement }
  49. history = { this.history }>
  50. { routes.map(r => (
  51. <Route
  52. component = { r.component }
  53. key = { r.component }
  54. onEnter = { this._onRouteEnter }
  55. path = { r.path } />
  56. )) }
  57. </Router>
  58. </Provider>
  59. );
  60. /* eslint-enable no-extra-parens */
  61. }
  62. /**
  63. * Navigates to a specific Route (via platform-specific means).
  64. *
  65. * @param {Route} route - The Route to which to navigate.
  66. * @returns {void}
  67. */
  68. _navigate(route) {
  69. let path = route.path;
  70. const store = this.props.store;
  71. // The syntax :room bellow is defined by react-router. It "matches a URL
  72. // segment up to the next /, ?, or #. The matched string is called a
  73. // param."
  74. path
  75. = path.replace(
  76. /:room/g,
  77. store.getState()['features/base/conference'].room);
  78. return store.dispatch(push(path));
  79. }
  80. /**
  81. * Invoked by react-router to notify this App that a Route is about to be
  82. * rendered.
  83. *
  84. * @private
  85. * @returns {void}
  86. */
  87. _onRouteEnter() {
  88. // XXX The following is mandatory. Otherwise, moving back & forward
  89. // through the browser's history could leave this App on the Conference
  90. // page without a room name.
  91. // Our Router configuration (at the time of this writing) is such that
  92. // each Route corresponds to a single URL. Hence, entering into a Route
  93. // is like opening a URL.
  94. // XXX In order to unify work with URLs in web and native environments,
  95. // we will construct URL here with correct domain from config.
  96. const currentDomain = getDomain(this.props.store.getState);
  97. const url
  98. = new URL(window.location.pathname, `https://${currentDomain}`)
  99. .toString();
  100. this._openURL(url);
  101. }
  102. /**
  103. * Create a ReactElement from the specified component and props on behalf of
  104. * the associated Router.
  105. *
  106. * @param {Component} component - The component from which the ReactElement
  107. * is to be created.
  108. * @param {Object} props - The read-only React Component props with which
  109. * the ReactElement is to be initialized.
  110. * @private
  111. * @returns {ReactElement}
  112. */
  113. _routerCreateElement(component, props) {
  114. return this._createElement(component, props);
  115. }
  116. }
  117. /**
  118. * App component's property types.
  119. *
  120. * @static
  121. */
  122. App.propTypes = AbstractApp.propTypes;