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

AbstractWelcomePage.js 6.0KB

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