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

InfoDialog.web.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. import PropTypes from 'prop-types';
  2. import React, { Component } from 'react';
  3. import { connect } from 'react-redux';
  4. import { setPassword } from '../../../base/conference';
  5. import { getInviteURL } from '../../../base/connection';
  6. import { translate } from '../../../base/i18n';
  7. import {
  8. PARTICIPANT_ROLE,
  9. getLocalParticipant
  10. } from '../../../base/participants';
  11. import { updateDialInNumbers } from '../../actions';
  12. import DialInNumber from './DialInNumber';
  13. import PasswordForm from './PasswordForm';
  14. const logger = require('jitsi-meet-logger').getLogger(__filename);
  15. /**
  16. * A React Component with the contents for a dialog that shows information about
  17. * the current conference.
  18. *
  19. * @extends Component
  20. */
  21. class InfoDialog extends Component {
  22. /**
  23. * {@code InfoDialog} component's property types.
  24. *
  25. * @static
  26. */
  27. static propTypes = {
  28. /**
  29. * Whether or not the current user can modify the current password.
  30. */
  31. _canEditPassword: PropTypes.bool,
  32. /**
  33. * The JitsiConference for which to display a lock state and change the
  34. * password.
  35. *
  36. * @type {JitsiConference}
  37. */
  38. _conference: PropTypes.object,
  39. /**
  40. * The name of the current conference. Used as part of inviting users.
  41. */
  42. _conferenceName: PropTypes.string,
  43. /**
  44. * The redux state representing the dial-in numbers feature.
  45. */
  46. _dialIn: PropTypes.object,
  47. /**
  48. * The current url of the conference to be copied onto the clipboard.
  49. */
  50. _inviteURL: PropTypes.string,
  51. /**
  52. * The value for how the conference is locked (or undefined if not
  53. * locked) as defined by room-lock constants.
  54. */
  55. _locked: PropTypes.string,
  56. /**
  57. * The current known password for the JitsiConference.
  58. */
  59. _password: PropTypes.string,
  60. /**
  61. * Invoked to open a dialog for adding participants to the conference.
  62. */
  63. dispatch: PropTypes.func,
  64. /**
  65. * Callback invoked when the dialog should be closed.
  66. */
  67. onClose: PropTypes.func,
  68. /**
  69. * Callback invoked when a mouse-related event has been detected.
  70. */
  71. onMouseOver: PropTypes.func,
  72. /**
  73. * Invoked to obtain translated strings.
  74. */
  75. t: PropTypes.func
  76. };
  77. /**
  78. * {@code InfoDialog} component's local state.
  79. *
  80. * @type {Object}
  81. * @property {boolean} passwordEditEnabled - Whether or not to show the
  82. * {@code PasswordForm} in its editing state.
  83. * @property {string} phoneNumber - The number to display for dialing into
  84. * the conference.
  85. */
  86. state = {
  87. passwordEditEnabled: false,
  88. phoneNumber: ''
  89. };
  90. /**
  91. * Initializes new {@code InfoDialog} instance.
  92. *
  93. * @param {Object} props - The read-only properties with which the new
  94. * instance is to be initialized.
  95. */
  96. constructor(props) {
  97. super(props);
  98. const { defaultCountry, numbers } = props._dialIn;
  99. if (numbers) {
  100. this.state.phoneNumber
  101. = this._getDefaultPhoneNumber(numbers, defaultCountry);
  102. }
  103. /**
  104. * The internal reference to the DOM/HTML element backing the React
  105. * {@code Component} text area. It is necessary for the implementation
  106. * of copying to the clipboard.
  107. *
  108. * @private
  109. * @type {HTMLTextAreaElement}
  110. */
  111. this._copyElement = null;
  112. // Bind event handlers so they are only bound once for every instance.
  113. this._onCopyInviteURL = this._onCopyInviteURL.bind(this);
  114. this._onPasswordRemove = this._onPasswordRemove.bind(this);
  115. this._onPasswordSubmit = this._onPasswordSubmit.bind(this);
  116. this._onTogglePasswordEditState
  117. = this._onTogglePasswordEditState.bind(this);
  118. this._setCopyElement = this._setCopyElement.bind(this);
  119. }
  120. /**
  121. * Implements {@link Component#componentDidMount()}. Invoked immediately
  122. * after this component is mounted. Requests dial-in numbers if not
  123. * already known.
  124. *
  125. * @inheritdoc
  126. * @returns {void}
  127. */
  128. componentDidMount() {
  129. if (!this.state.phoneNumber) {
  130. this.props.dispatch(updateDialInNumbers());
  131. }
  132. }
  133. /**
  134. * Implements React's {@link Component#componentWillReceiveProps()}. Invoked
  135. * before this mounted component receives new props.
  136. *
  137. * @inheritdoc
  138. * @param {Props} nextProps - New props component will receive.
  139. */
  140. componentWillReceiveProps(nextProps) {
  141. if (!this.props._password && nextProps._password) {
  142. this.setState({ passwordEditEnabled: false });
  143. }
  144. if (!this.state.phoneNumber && nextProps._dialIn.numbers) {
  145. const { defaultCountry, numbers } = nextProps._dialIn;
  146. this.setState({
  147. phoneNumber:
  148. this._getDefaultPhoneNumber(numbers, defaultCountry)
  149. });
  150. }
  151. }
  152. /**
  153. * Implements React's {@link Component#render()}.
  154. *
  155. * @inheritdoc
  156. * @returns {ReactElement}
  157. */
  158. render() {
  159. const { onMouseOver, t } = this.props;
  160. return (
  161. <div
  162. className = 'info-dialog'
  163. onMouseOver = { onMouseOver } >
  164. <div className = 'info-dialog-column'>
  165. <h4 className = 'info-dialog-icon'>
  166. <i className = 'icon-info' />
  167. </h4>
  168. </div>
  169. <div className = 'info-dialog-column'>
  170. <div className = 'info-dialog-title'>
  171. { t('info.title') }
  172. </div>
  173. <div className = 'info-dialog-conference-url'>
  174. { t('info.conferenceURL',
  175. { url: this._getURLToDisplay() }) }
  176. <textarea
  177. className = 'info-dialog-copy-element'
  178. readOnly = { true }
  179. ref = { this._setCopyElement }
  180. tabIndex = '-1'
  181. value = { this._getTextToCopy() } />
  182. </div>
  183. <div className = 'info-dialog-dial-in'>
  184. { this._renderDialInDisplay() }
  185. </div>
  186. <div className = 'info-dialog-password'>
  187. <PasswordForm
  188. editEnabled = { this.state.passwordEditEnabled }
  189. locked = { this.props._locked }
  190. onSubmit = { this._onPasswordSubmit }
  191. password = { this.props._password } />
  192. </div>
  193. <div className = 'info-dialog-action-links'>
  194. <div className = 'info-dialog-action-link'>
  195. <a
  196. className = 'info-copy'
  197. onClick = { this._onCopyInviteURL }>
  198. { t('dialog.copy') }
  199. </a>
  200. </div>
  201. { this._renderPasswordAction() }
  202. </div>
  203. </div>
  204. </div>
  205. );
  206. }
  207. /**
  208. * Sets the internal state of which dial-in number to display.
  209. *
  210. * @param {Array<string>|Object} dialInNumbers - The array or object of
  211. * numbers to choose a number from.
  212. * @param {string} defaultCountry - The country code for the country
  213. * whose phone number should display.
  214. * @private
  215. * @returns {string|null}
  216. */
  217. _getDefaultPhoneNumber(dialInNumbers, defaultCountry = 'US') {
  218. if (Array.isArray(dialInNumbers)) {
  219. // Dumbly return the first number if an array.
  220. return dialInNumbers[0];
  221. } else if (Object.keys(dialInNumbers).length > 0) {
  222. const defaultNumbers = dialInNumbers[defaultCountry];
  223. if (defaultNumbers) {
  224. return defaultNumbers[0];
  225. }
  226. const firstRegion = Object.keys(dialInNumbers)[0];
  227. return firstRegion && firstRegion[0];
  228. }
  229. return null;
  230. }
  231. /**
  232. * Creates a message describing how to dial in to the conference.
  233. *
  234. * @private
  235. * @returns {string}
  236. */
  237. _getTextToCopy() {
  238. const { _conferenceName, t } = this.props;
  239. let invite = t('info.inviteURL', {
  240. url: this.props._inviteURL
  241. });
  242. if (this._shouldDisplayDialIn()) {
  243. const dial = t('info.invitePhone', {
  244. number: this.state.phoneNumber,
  245. conferenceID: this.props._dialIn.conferenceID
  246. });
  247. const moreNumbers = t('info.invitePhoneAlternatives', {
  248. url: `${window.location.origin}/static/dialInInfo.html?room=${
  249. encodeURIComponent(_conferenceName)}`
  250. });
  251. invite = `${invite}\n${dial}\n${moreNumbers}`;
  252. }
  253. return invite;
  254. }
  255. /**
  256. * Modifies the inviteURL for display in the modal.
  257. *
  258. * @private
  259. * @returns {string}
  260. */
  261. _getURLToDisplay() {
  262. return this.props._inviteURL.replace(/^https?:\/\//i, '');
  263. }
  264. /**
  265. * Callback invoked to copy the contents of {@code this._copyElement} to the
  266. * clipboard.
  267. *
  268. * @private
  269. * @returns {void}
  270. */
  271. _onCopyInviteURL() {
  272. try {
  273. this._copyElement.select();
  274. document.execCommand('copy');
  275. this._copyElement.blur();
  276. } catch (err) {
  277. logger.error('error when copying the text', err);
  278. }
  279. }
  280. /**
  281. * Callback invoked to unlock the current JitsiConference.
  282. *
  283. * @private
  284. * @returns {void}
  285. */
  286. _onPasswordRemove() {
  287. this._onPasswordSubmit('');
  288. }
  289. /**
  290. * Callback invoked to set a password on the current JitsiConference.
  291. *
  292. * @param {string} enteredPassword - The new password to be used to lock the
  293. * current JitsiConference.
  294. * @private
  295. * @returns {void}
  296. */
  297. _onPasswordSubmit(enteredPassword) {
  298. const { _conference } = this.props;
  299. this.props.dispatch(setPassword(
  300. _conference,
  301. _conference.lock,
  302. enteredPassword
  303. ));
  304. }
  305. /**
  306. * Toggles whether or not the password should currently be shown as being
  307. * edited locally.
  308. *
  309. * @private
  310. * @returns {void}
  311. */
  312. _onTogglePasswordEditState() {
  313. this.setState({
  314. passwordEditEnabled: !this.state.passwordEditEnabled
  315. });
  316. }
  317. /**
  318. * Returns a ReactElement for showing how to dial into the conference, if
  319. * dialing in is available.
  320. *
  321. * @private
  322. * @returns {null|ReactElement}
  323. */
  324. _renderDialInDisplay() {
  325. if (!this._shouldDisplayDialIn()) {
  326. return null;
  327. }
  328. return (
  329. <div>
  330. <DialInNumber
  331. conferenceID = { this.props._dialIn.conferenceID }
  332. phoneNumber = { this.state.phoneNumber } />
  333. <a
  334. className = 'more-numbers'
  335. href = { `static/dialInInfo.html?room=${
  336. encodeURIComponent(this.props._conferenceName)}` }
  337. rel = 'noopener noreferrer'
  338. target = '_blank'>
  339. { this.props.t('info.moreNumbers') }
  340. </a>
  341. </div>
  342. );
  343. }
  344. /**
  345. * Returns a ReactElement for interacting with the password field.
  346. *
  347. * @private
  348. * @returns {null|ReactElement}
  349. */
  350. _renderPasswordAction() {
  351. const { t } = this.props;
  352. let className, onClick, textKey;
  353. if (!this.props._canEditPassword) {
  354. // intentionally left blank to prevent rendering anything
  355. } else if (this.state.passwordEditEnabled) {
  356. className = 'cancel-password';
  357. onClick = this._onTogglePasswordEditState;
  358. textKey = 'info.cancelPassword';
  359. } else if (this.props._locked) {
  360. className = 'remove-password';
  361. onClick = this._onPasswordRemove;
  362. textKey = 'dialog.removePassword';
  363. } else {
  364. className = 'add-password';
  365. onClick = this._onTogglePasswordEditState;
  366. textKey = 'info.addPassword';
  367. }
  368. return className && onClick && textKey
  369. ? <div className = 'info-dialog-action-link'>
  370. <a
  371. className = { className }
  372. onClick = { onClick }>
  373. { t(textKey) }
  374. </a>
  375. </div>
  376. : null;
  377. }
  378. /**
  379. * Returns whether or not dial-in related UI should be displayed.
  380. *
  381. * @private
  382. * @returns {boolean}
  383. */
  384. _shouldDisplayDialIn() {
  385. const { conferenceID, numbers, numbersEnabled } = this.props._dialIn;
  386. const { phoneNumber } = this.state;
  387. return Boolean(
  388. conferenceID
  389. && numbers
  390. && numbersEnabled
  391. && phoneNumber);
  392. }
  393. /**
  394. * Sets the internal reference to the DOM/HTML element backing the React
  395. * {@code Component} input.
  396. *
  397. * @param {HTMLInputElement} element - The DOM/HTML element for this
  398. * {@code Component}'s input.
  399. * @private
  400. * @returns {void}
  401. */
  402. _setCopyElement(element) {
  403. this._copyElement = element;
  404. }
  405. }
  406. /**
  407. * Maps (parts of) the Redux state to the associated props for the
  408. * {@code InfoDialog} component.
  409. *
  410. * @param {Object} state - The Redux state.
  411. * @private
  412. * @returns {{
  413. * _canEditPassword: boolean,
  414. * _conference: Object,
  415. * _conferenceName: string,
  416. * _dialIn: Object,
  417. * _inviteURL: string,
  418. * _locked: string,
  419. * _password: string
  420. * }}
  421. */
  422. function _mapStateToProps(state) {
  423. const {
  424. conference,
  425. locked,
  426. password,
  427. room
  428. } = state['features/base/conference'];
  429. const isModerator
  430. = getLocalParticipant(state).role === PARTICIPANT_ROLE.MODERATOR;
  431. let canEditPassword;
  432. if (state['features/base/config'].enableUserRolesBasedOnToken) {
  433. canEditPassword = isModerator && !state['features/base/jwt'].isGuest;
  434. } else {
  435. canEditPassword = isModerator;
  436. }
  437. return {
  438. _canEditPassword: canEditPassword,
  439. _conference: conference,
  440. _conferenceName: room,
  441. _dialIn: state['features/invite'],
  442. _inviteURL: getInviteURL(state),
  443. _locked: locked,
  444. _password: password
  445. };
  446. }
  447. export default translate(connect(_mapStateToProps)(InfoDialog));