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

App.web.js 5.0KB

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