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.9KB

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