Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AbstractApp.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* global APP */
  2. import React, { Component } from 'react';
  3. import { Provider } from 'react-redux';
  4. import { compose, createStore } from 'redux';
  5. import Thunk from 'redux-thunk';
  6. import {
  7. localParticipantJoined,
  8. localParticipantLeft
  9. } from '../../base/participants';
  10. import { RouteRegistry } from '../../base/react';
  11. import { MiddlewareRegistry, ReducerRegistry } from '../../base/redux';
  12. import {
  13. appNavigate,
  14. appWillMount,
  15. appWillUnmount
  16. } from '../actions';
  17. /**
  18. * Base (abstract) class for main App component.
  19. *
  20. * @abstract
  21. */
  22. export class AbstractApp extends Component {
  23. /**
  24. * AbstractApp component's property types.
  25. *
  26. * @static
  27. */
  28. static propTypes = {
  29. config: React.PropTypes.object,
  30. store: React.PropTypes.object,
  31. /**
  32. * The URL, if any, with which the app was launched.
  33. */
  34. url: React.PropTypes.string
  35. }
  36. /**
  37. * Initializes a new AbstractApp instance.
  38. *
  39. * @param {Object} props - The read-only React Component props with which
  40. * the new instance is to be initialized.
  41. */
  42. constructor(props) {
  43. super(props);
  44. this.state = {
  45. /**
  46. * The Route rendered by this AbstractApp.
  47. *
  48. * @type {Route}
  49. */
  50. route: undefined,
  51. /**
  52. * The Redux store used by this AbstractApp.
  53. *
  54. * @type {Store}
  55. */
  56. store: this._maybeCreateStore(props)
  57. };
  58. }
  59. /**
  60. * Init lib-jitsi-meet and create local participant when component is going
  61. * to be mounted.
  62. *
  63. * @inheritdoc
  64. */
  65. componentWillMount() {
  66. const dispatch = this._getStore().dispatch;
  67. dispatch(appWillMount(this));
  68. dispatch(localParticipantJoined());
  69. // If a URL was explicitly specified to this React Component, then open
  70. // it; otherwise, use a default.
  71. this._openURL(this.props.url || this._getDefaultURL());
  72. }
  73. /**
  74. * Notifies this mounted React Component that it will receive new props.
  75. * Makes sure that this AbstractApp has a Redux store to use.
  76. *
  77. * @inheritdoc
  78. * @param {Object} nextProps - The read-only React Component props that this
  79. * instance will receive.
  80. * @returns {void}
  81. */
  82. componentWillReceiveProps(nextProps) {
  83. // The consumer of this AbstractApp did not provide a Redux store.
  84. if (typeof nextProps.store === 'undefined'
  85. // The consumer of this AbstractApp did provide a Redux store
  86. // before. Which means that the consumer changed their mind. In
  87. // such a case this instance should create its own internal
  88. // Redux store. If the consumer did not provide a Redux store
  89. // before, then this instance is using its own internal Redux
  90. // store already.
  91. && typeof this.props.store !== 'undefined') {
  92. this.setState({
  93. store: this._maybeCreateStore(nextProps)
  94. });
  95. }
  96. }
  97. /**
  98. * Dispose lib-jitsi-meet and remove local participant when component is
  99. * going to be unmounted.
  100. *
  101. * @inheritdoc
  102. */
  103. componentWillUnmount() {
  104. const dispatch = this._getStore().dispatch;
  105. dispatch(localParticipantLeft());
  106. dispatch(appWillUnmount(this));
  107. }
  108. /**
  109. * Implements React's {@link Component#render()}.
  110. *
  111. * @inheritdoc
  112. * @returns {ReactElement}
  113. */
  114. render() {
  115. const route = this.state.route;
  116. if (route) {
  117. return (
  118. <Provider store = { this._getStore() }>
  119. {
  120. this._createElement(route.component)
  121. }
  122. </Provider>
  123. );
  124. }
  125. return null;
  126. }
  127. /**
  128. * Create a ReactElement from the specified component, the specified props
  129. * and the props of this AbstractApp which are suitable for propagation to
  130. * the children of this Component.
  131. *
  132. * @param {Component} component - The component from which the ReactElement
  133. * is to be created.
  134. * @param {Object} props - The read-only React Component props with which
  135. * the ReactElement is to be initialized.
  136. * @returns {ReactElement}
  137. * @protected
  138. */
  139. _createElement(component, props) {
  140. /* eslint-disable no-unused-vars, lines-around-comment */
  141. const {
  142. // Don't propagate the config prop(erty) because the config is
  143. // stored inside the Redux state and, thus, is visible to the
  144. // children anyway.
  145. config,
  146. // Don't propagate the dispatch and store props because they usually
  147. // come from react-redux and programmers don't really expect them to
  148. // be inherited but rather explicitly connected.
  149. dispatch, // eslint-disable-line react/prop-types
  150. store,
  151. // The url property was introduced to be consumed entirely by
  152. // AbstractApp.
  153. url,
  154. // The remaining props, if any, are considered suitable for
  155. // propagation to the children of this Component.
  156. ...thisProps
  157. } = this.props;
  158. /* eslint-enable no-unused-vars, lines-around-comment */
  159. // eslint-disable-next-line object-property-newline
  160. return React.createElement(component, { ...thisProps, ...props });
  161. }
  162. /**
  163. * Initializes a new Redux store instance suitable for use by
  164. * this AbstractApp.
  165. *
  166. * @private
  167. * @returns {Store} - A new Redux store instance suitable for use by
  168. * this AbstractApp.
  169. */
  170. _createStore() {
  171. // Create combined reducer from all reducers in ReducerRegistry.
  172. const reducer = ReducerRegistry.combineReducers();
  173. // Apply all registered middleware from the MiddlewareRegistry and
  174. // additional 3rd party middleware:
  175. // - Thunk - allows us to dispatch async actions easily. For more info
  176. // @see https://github.com/gaearon/redux-thunk.
  177. let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
  178. // Try to enable Redux DevTools Chrome extension in order to make it
  179. // available for the purposes of facilitating development.
  180. let devToolsExtension;
  181. if (typeof window === 'object'
  182. && (devToolsExtension = window.devToolsExtension)) {
  183. middleware = compose(middleware, devToolsExtension());
  184. }
  185. return createStore(reducer, middleware);
  186. }
  187. /**
  188. * Gets the default URL to be opened when this App mounts.
  189. *
  190. * @protected
  191. * @returns {string} The default URL to be opened when this App mounts.
  192. */
  193. _getDefaultURL() {
  194. // If the execution environment provides a Location abstraction, then
  195. // this App at already at that location but it must be made aware of the
  196. // fact.
  197. const windowLocation = this._getWindowLocation();
  198. if (windowLocation) {
  199. const href = windowLocation.toString();
  200. if (href) {
  201. return href;
  202. }
  203. }
  204. // By default, open the domain configured in the configuration file
  205. // which may be the domain at which the whole server infrastructure is
  206. // deployed.
  207. const config = this.props.config;
  208. if (typeof config === 'object') {
  209. const hosts = config.hosts;
  210. if (typeof hosts === 'object') {
  211. const domain = hosts.domain;
  212. if (domain) {
  213. return `https://${domain}`;
  214. }
  215. }
  216. }
  217. return 'https://meet.jit.si';
  218. }
  219. /**
  220. * Gets the Redux store used by this AbstractApp.
  221. *
  222. * @protected
  223. * @returns {Store} - The Redux store used by this AbstractApp.
  224. */
  225. _getStore() {
  226. let store = this.state.store;
  227. if (typeof store === 'undefined') {
  228. store = this.props.store;
  229. }
  230. return store;
  231. }
  232. /**
  233. * Gets a Location object from the window with information about the current
  234. * location of the document. Explicitly defined to allow extenders to
  235. * override because React Native does not usually have a location property
  236. * on its window unless debugging remotely in which case the browser that is
  237. * the remote debugger will provide a location property on the window.
  238. *
  239. * @protected
  240. * @returns {Location} A Location object with information about the current
  241. * location of the document.
  242. */
  243. _getWindowLocation() {
  244. return undefined;
  245. }
  246. /**
  247. * Creates a Redux store to be used by this AbstractApp if such as store is
  248. * not defined by the consumer of this AbstractApp through its
  249. * read-only React Component props.
  250. *
  251. * @param {Object} props - The read-only React Component props that will
  252. * eventually be received by this AbstractApp.
  253. * @private
  254. * @returns {Store} - The Redux store to be used by this AbstractApp.
  255. */
  256. _maybeCreateStore(props) {
  257. // The application Jitsi Meet is architected with Redux. However, I do
  258. // not want consumers of the App React Component to be forced into
  259. // dealing with Redux. If the consumer did not provide an external Redux
  260. // store, utilize an internal Redux store.
  261. let store = props.store;
  262. if (typeof store === 'undefined') {
  263. store = this._createStore();
  264. // This is temporary workaround to be able to dispatch actions from
  265. // non-reactified parts of the code (conference.js for example).
  266. // Don't use in the react code!!!
  267. // FIXME: remove when the reactification is finished!
  268. APP.store = store;
  269. }
  270. return store;
  271. }
  272. /**
  273. * Navigates to a specific Route.
  274. *
  275. * @param {Route} route - The Route to which to navigate.
  276. * @returns {void}
  277. */
  278. _navigate(route) {
  279. if (RouteRegistry.areRoutesEqual(this.state.route, route)) {
  280. return;
  281. }
  282. let nextState = {
  283. ...this.state,
  284. route
  285. };
  286. // The Web App was using react-router so it utilized react-router's
  287. // onEnter. During the removal of react-router, modifications were
  288. // minimized by preserving the onEnter interface:
  289. // (1) Router would provide its nextState to the Route's onEnter. As the
  290. // role of Router is now this AbstractApp, provide its nextState.
  291. // (2) A replace function would be provided to the Route in case it
  292. // chose to redirect to another path.
  293. this._onRouteEnter(route, nextState, pathname => {
  294. this._openURL(pathname);
  295. // Do not proceed with the route because it chose to redirect to
  296. // another path.
  297. nextState = undefined;
  298. });
  299. nextState && this.setState(nextState);
  300. }
  301. /**
  302. * Notifies this App that a specific Route is about to be rendered.
  303. *
  304. * @param {Route} route - The Route that is about to be rendered.
  305. * @private
  306. * @returns {void}
  307. */
  308. _onRouteEnter(route, ...args) {
  309. // Notify the route that it is about to be entered.
  310. const onEnter = route.onEnter;
  311. if (typeof onEnter === 'function') {
  312. onEnter(...args);
  313. }
  314. }
  315. /**
  316. * Navigates this AbstractApp to (i.e. opens) a specific URL.
  317. *
  318. * @param {string} url - The URL to which to navigate this AbstractApp (i.e.
  319. * the URL to open).
  320. * @protected
  321. * @returns {void}
  322. */
  323. _openURL(url) {
  324. this._getStore().dispatch(appNavigate(url));
  325. }
  326. }