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

AbstractWelcomePage.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import React, { Component } from 'react';
  2. import { appNavigate } from '../../app';
  3. import { isRoomValid } from '../../base/conference';
  4. import { VideoTrack } from '../../base/media';
  5. import { getLocalVideoTrack } from '../../base/tracks';
  6. import { generateRoomWithoutSeparator } from '../roomnameGenerator';
  7. /**
  8. * Base (abstract) class for container component rendering the welcome page.
  9. *
  10. * @abstract
  11. */
  12. export class AbstractWelcomePage extends Component {
  13. /**
  14. * {@code AbstractWelcomePage} component's property types.
  15. *
  16. * @static
  17. */
  18. static propTypes = {
  19. _localVideoTrack: React.PropTypes.object,
  20. _room: React.PropTypes.string,
  21. dispatch: React.PropTypes.func
  22. };
  23. /**
  24. * Initializes a new {@code AbstractWelcomePage} instance.
  25. *
  26. * @param {Object} props - The React {@code Component} props to initialize
  27. * the new {@code AbstractWelcomePage} instance with.
  28. */
  29. constructor(props) {
  30. super(props);
  31. /**
  32. * Save room name into component's local state.
  33. *
  34. * @type {Object}
  35. * @property {number|null} animateTimeoutId - Identifier of the letter
  36. * animation timeout.
  37. * @property {string} generatedRoomname - Automatically generated
  38. * room name.
  39. * @property {string} room - Room name.
  40. * @property {string} roomPlaceholder - Room placeholder
  41. * that's used as a placeholder for input.
  42. * @property {nubmer|null} updateTimeoutId - Identifier of the timeout
  43. * updating the generated room name.
  44. */
  45. this.state = {
  46. animateTimeoutId: null,
  47. generatedRoomname: '',
  48. room: '',
  49. roomPlaceholder: '',
  50. updateTimeoutId: null
  51. };
  52. // Bind event handlers so they are only bound once per instance.
  53. this._animateRoomnameChanging
  54. = this._animateRoomnameChanging.bind(this);
  55. this._onJoin = this._onJoin.bind(this);
  56. this._onRoomChange = this._onRoomChange.bind(this);
  57. this._updateRoomname = this._updateRoomname.bind(this);
  58. }
  59. /**
  60. * This method is executed when component receives new properties.
  61. *
  62. * @inheritdoc
  63. * @param {Object} nextProps - New props component will receive.
  64. */
  65. componentWillReceiveProps(nextProps) {
  66. this.setState({ room: nextProps._room });
  67. }
  68. /**
  69. * This method is executed when method will be unmounted from DOM.
  70. *
  71. * @inheritdoc
  72. */
  73. componentWillUnmount() {
  74. this._clearTimeouts();
  75. }
  76. /**
  77. * Animates the changing of the room name.
  78. *
  79. * @param {string} word - The part of room name that should be added to
  80. * placeholder.
  81. * @private
  82. * @returns {void}
  83. */
  84. _animateRoomnameChanging(word) {
  85. let animateTimeoutId = null;
  86. const roomPlaceholder = this.state.roomPlaceholder + word.substr(0, 1);
  87. if (word.length > 1) {
  88. animateTimeoutId
  89. = setTimeout(
  90. () => {
  91. this._animateRoomnameChanging(
  92. word.substring(1, word.length));
  93. },
  94. 70);
  95. }
  96. this.setState({
  97. animateTimeoutId,
  98. roomPlaceholder
  99. });
  100. }
  101. /**
  102. * Method that clears timeouts for animations and updates of room name.
  103. *
  104. * @private
  105. * @returns {void}
  106. */
  107. _clearTimeouts() {
  108. clearTimeout(this.state.animateTimeoutId);
  109. clearTimeout(this.state.updateTimeoutId);
  110. }
  111. /**
  112. * Determines whether the 'Join' button is (to be) disabled i.e. there's no
  113. * valid room name typed into the respective text input field.
  114. *
  115. * @protected
  116. * @returns {boolean} If the 'Join' button is (to be) disabled, true;
  117. * otherwise, false.
  118. */
  119. _isJoinDisabled() {
  120. return !isRoomValid(this.state.room);
  121. }
  122. /**
  123. * Handles joining. Either by clicking on 'Join' button
  124. * or by pressing 'Enter' in room name input field.
  125. *
  126. * @protected
  127. * @returns {void}
  128. */
  129. _onJoin() {
  130. const room = this.state.room || this.state.generatedRoomname;
  131. room && this.props.dispatch(appNavigate(room));
  132. }
  133. /**
  134. * Handles 'change' event for the room name text input field.
  135. *
  136. * @param {string} value - The text typed into the respective text input
  137. * field.
  138. * @protected
  139. * @returns {void}
  140. */
  141. _onRoomChange(value) {
  142. this.setState({ room: value });
  143. }
  144. /**
  145. * Renders a local video if any.
  146. *
  147. * @protected
  148. * @returns {(ReactElement|null)}
  149. */
  150. _renderLocalVideo() {
  151. return (
  152. <VideoTrack videoTrack = { this.props._localVideoTrack } />
  153. );
  154. }
  155. /**
  156. * Triggers the generation of a new room name and initiates an animation of
  157. * its changing.
  158. *
  159. * @protected
  160. * @returns {void}
  161. */
  162. _updateRoomname() {
  163. const generatedRoomname = generateRoomWithoutSeparator();
  164. const roomPlaceholder = '';
  165. const updateTimeoutId = setTimeout(this._updateRoomname, 10000);
  166. this._clearTimeouts();
  167. this.setState(
  168. {
  169. generatedRoomname,
  170. roomPlaceholder,
  171. updateTimeoutId
  172. },
  173. () => this._animateRoomnameChanging(generatedRoomname));
  174. }
  175. }
  176. /**
  177. * Selects local video track from tracks in state, local participant and room
  178. * and maps them to component props. It seems it's not possible to 'connect'
  179. * base component and then extend from it. So we export this function in order
  180. * to be used in child classes for 'connect'.
  181. *
  182. * @param {Object} state - Redux state.
  183. * @protected
  184. * @returns {{
  185. * _localVideoTrack: (Track|undefined),
  186. * _room: string
  187. * }}
  188. */
  189. export function _mapStateToProps(state) {
  190. const conference = state['features/base/conference'];
  191. const tracks = state['features/base/tracks'];
  192. return {
  193. _localVideoTrack: getLocalVideoTrack(tracks),
  194. _room: conference.room
  195. };
  196. }