You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DialInNumbersForm.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import { StatelessDropdownMenu } from '@atlaskit/dropdown-menu';
  2. import ExpandIcon from '@atlaskit/icon/glyph/expand';
  3. import React, { Component } from 'react';
  4. import { connect } from 'react-redux';
  5. import { translate } from '../../base/i18n';
  6. import { getLocalParticipant } from '../../base/participants';
  7. import { updateDialInNumbers } from '../actions';
  8. const logger = require('jitsi-meet-logger').getLogger(__filename);
  9. const EXPAND_ICON = <ExpandIcon label = 'expand' />;
  10. /**
  11. * React {@code Component} responsible for fetching and displaying telephone
  12. * numbers for dialing into the conference. Also supports copying a selected
  13. * dial-in number to the clipboard.
  14. *
  15. * @extends Component
  16. */
  17. class DialInNumbersForm extends Component {
  18. /**
  19. * {@code DialInNumbersForm}'s property types.
  20. *
  21. * @static
  22. */
  23. static propTypes = {
  24. /**
  25. * The redux state representing the dial-in numbers feature.
  26. */
  27. _dialIn: React.PropTypes.object,
  28. /**
  29. * The display name of the local user.
  30. */
  31. _localUserDisplayName: React.PropTypes.string,
  32. /**
  33. * The url for the JitsiConference.
  34. */
  35. conferenceUrl: React.PropTypes.string,
  36. /**
  37. * Invoked to send an ajax request for dial-in numbers.
  38. */
  39. dispatch: React.PropTypes.func,
  40. /**
  41. * Invoked to obtain translated strings.
  42. */
  43. t: React.PropTypes.func
  44. }
  45. /**
  46. * Initializes a new {@code DialInNumbersForm} instance.
  47. *
  48. * @param {Object} props - The read-only properties with which the new
  49. * instance is to be initialized.
  50. */
  51. constructor(props) {
  52. super(props);
  53. this.state = {
  54. /**
  55. * Whether or not the dropdown should be open.
  56. *
  57. * @type {boolean}
  58. */
  59. isDropdownOpen: false,
  60. /**
  61. * The dial-in number to display as currently selected in the
  62. * dropdown. The value should be an object which has two key/value
  63. * pairs, content and number. The value of "content" will display in
  64. * the dropdown while the value of "number" is a substring of
  65. * "content" which will be copied to clipboard.
  66. *
  67. * @type {object}
  68. */
  69. selectedNumber: null
  70. };
  71. /**
  72. * The internal reference to the DOM/HTML element backing the React
  73. * {@code Component} text area. It is necessary for the implementation
  74. * of copying to the clipboard.
  75. *
  76. * @private
  77. * @type {HTMLTextAreaElement}
  78. */
  79. this._copyElement = null;
  80. // Bind event handlers so they are only bound once for every instance.
  81. this._onCopyClick = this._onCopyClick.bind(this);
  82. this._onOpenChange = this._onOpenChange.bind(this);
  83. this._onSelect = this._onSelect.bind(this);
  84. this._setCopyElement = this._setCopyElement.bind(this);
  85. }
  86. /**
  87. * Sets a default number to display in the dropdown trigger.
  88. *
  89. * @inheritdoc
  90. * returns {void}
  91. */
  92. componentWillMount() {
  93. if (this.props._dialIn.numbers) {
  94. this._setDefaultNumber(this.props._dialIn.numbers);
  95. } else {
  96. this.props.dispatch(updateDialInNumbers());
  97. }
  98. }
  99. /**
  100. * Monitors for number updates and sets a default number to display in the
  101. * dropdown trigger if not already set.
  102. *
  103. * @inheritdoc
  104. * returns {void}
  105. */
  106. componentWillReceiveProps(nextProps) {
  107. if (!this.state.selectedNumber && nextProps._dialIn.numbers) {
  108. this._setDefaultNumber(nextProps._dialIn.numbers);
  109. }
  110. }
  111. /**
  112. * Implements React's {@link Component#render()}. Returns null if the
  113. * component is not ready for display.
  114. *
  115. * @inheritdoc
  116. * @returns {ReactElement|null}
  117. */
  118. render() {
  119. const { _dialIn, t } = this.props;
  120. const { conferenceId, numbers, numbersEnabled } = _dialIn;
  121. const { selectedNumber } = this.state;
  122. if (!conferenceId || !numbers || !numbersEnabled || !selectedNumber) {
  123. return null;
  124. }
  125. const items = numbers ? this._formatNumbers(numbers) : [];
  126. return (
  127. <div className = 'form-control dial-in-numbers'>
  128. <label className = 'form-control__label'>
  129. { t('invite.howToDialIn') }
  130. <span className = 'dial-in-numbers-conference-id'>
  131. { conferenceId }
  132. </span>
  133. </label>
  134. <div className = 'form-control__container'>
  135. { this._createDropdownMenu(items, selectedNumber.content) }
  136. <button
  137. className = 'button-control button-control_light'
  138. onClick = { this._onCopyClick }
  139. type = 'button'>
  140. Copy
  141. </button>
  142. </div>
  143. <textarea
  144. className = 'dial-in-numbers-copy'
  145. readOnly = { true }
  146. ref = { this._setCopyElement }
  147. tabIndex = '-1'
  148. value = { this._generateCopyText() } />
  149. </div>
  150. );
  151. }
  152. /**
  153. * Creates a {@code StatelessDropdownMenu} instance.
  154. *
  155. * @param {Array} items - The content to display within the dropdown.
  156. * @param {string} triggerText - The text to display within the
  157. * trigger element.
  158. * @returns {ReactElement}
  159. */
  160. _createDropdownMenu(items, triggerText) {
  161. return (
  162. <StatelessDropdownMenu
  163. isOpen = { this.state.isDropdownOpen }
  164. items = { [ { items } ] }
  165. onItemActivated = { this._onSelect }
  166. onOpenChange = { this._onOpenChange }
  167. shouldFitContainer = { true }>
  168. { this._createDropdownTrigger(triggerText) }
  169. </StatelessDropdownMenu>
  170. );
  171. }
  172. /**
  173. * Creates a React {@code Component} with a readonly HTMLInputElement as a
  174. * trigger for displaying the dropdown menu. The {@code Component} will also
  175. * display the currently selected number.
  176. *
  177. * @param {string} triggerText - Text to display in the HTMLInputElement.
  178. * @private
  179. * @returns {ReactElement}
  180. */
  181. _createDropdownTrigger(triggerText) {
  182. return (
  183. <div className = 'dial-in-numbers-trigger'>
  184. <input
  185. className = 'input-control'
  186. readOnly = { true }
  187. type = 'text'
  188. value = { triggerText || '' } />
  189. <span className = 'dial-in-numbers-trigger-icon'>
  190. { EXPAND_ICON }
  191. </span>
  192. </div>
  193. );
  194. }
  195. /**
  196. * Detects whether the response from dialInNumbersUrl returned an array or
  197. * an object with dial-in numbers and calls the appropriate method to
  198. * transform the numbers into the format expected by
  199. * {@code StatelessDropdownMenu}.
  200. *
  201. * @param {Array<string>|Object} dialInNumbers - The numbers returned from
  202. * requesting dialInNumbersUrl.
  203. * @private
  204. * @returns {Array<Object>}
  205. */
  206. _formatNumbers(dialInNumbers) {
  207. if (Array.isArray(dialInNumbers)) {
  208. return this._formatNumbersArray(dialInNumbers);
  209. }
  210. return this._formatNumbersObject(dialInNumbers);
  211. }
  212. /**
  213. * Transforms the passed in numbers array into an array of objects that can
  214. * be parsed by {@code StatelessDropdownMenu}.
  215. *
  216. * @param {Array<string>} dialInNumbers - An array with dial-in numbers to
  217. * display and copy.
  218. * @private
  219. * @returns {Array<Object>}
  220. */
  221. _formatNumbersArray(dialInNumbers) {
  222. return dialInNumbers.map(number => {
  223. return {
  224. content: number,
  225. number
  226. };
  227. });
  228. }
  229. /**
  230. * Transforms the passed in numbers object into an array of objects that can
  231. * be parsed by {@code StatelessDropdownMenu}.
  232. *
  233. * @param {Object} dialInNumbers - The numbers object to parse. The
  234. * expected format is an object with keys being the name of the country
  235. * and the values being an array of numbers as strings.
  236. * @private
  237. * @returns {Array<Object>}
  238. */
  239. _formatNumbersObject(dialInNumbers) {
  240. const phoneRegions = Object.keys(dialInNumbers);
  241. if (!phoneRegions.length) {
  242. return [];
  243. }
  244. const formattedNumbers = phoneRegions.map(region => {
  245. const numbers = dialInNumbers[region];
  246. return numbers.map(number => {
  247. return {
  248. content: `${region}: ${number}`,
  249. number
  250. };
  251. });
  252. });
  253. return Array.prototype.concat(...formattedNumbers);
  254. }
  255. /**
  256. * Creates a message describing how to dial in to the conference.
  257. *
  258. * @private
  259. * @returns {string}
  260. */
  261. _generateCopyText() {
  262. const welcome = this.props.t('invite.invitedYouTo', {
  263. meetingUrl: this.props.conferenceUrl,
  264. userName: this.props._localUserDisplayName
  265. });
  266. const callNumber = this.props.t('invite.callNumber',
  267. { number: this.state.selectedNumber.number });
  268. const stepOne = `1) ${callNumber}`;
  269. const enterId = this.props.t('invite.enterId',
  270. { meetingId: this.props._dialIn.conferenceId });
  271. const stepTwo = `2) ${enterId}`;
  272. return `${welcome}\n${stepOne}\n${stepTwo}`;
  273. }
  274. /**
  275. * Copies part of the number displayed in the dropdown trigger into the
  276. * clipboard. Only the value specified in selectedNumber.number, which
  277. * should be a substring of the displayed value, will be copied.
  278. *
  279. * @private
  280. * @returns {void}
  281. */
  282. _onCopyClick() {
  283. try {
  284. this._copyElement.select();
  285. document.execCommand('copy');
  286. this._copyElement.blur();
  287. } catch (err) {
  288. logger.error('error when copying the text', err);
  289. }
  290. }
  291. /**
  292. * Sets the internal state to either open or close the dropdown. If the
  293. * dropdown is disabled, the state will always be set to false.
  294. *
  295. * @param {Object} dropdownEvent - The even returned from clicking on the
  296. * dropdown trigger.
  297. * @private
  298. * @returns {void}
  299. */
  300. _onOpenChange(dropdownEvent) {
  301. this.setState({
  302. isDropdownOpen: dropdownEvent.isOpen
  303. });
  304. }
  305. /**
  306. * Updates the internal state of the currently selected number.
  307. *
  308. * @param {Object} selection - Event from choosing an dropdown option.
  309. * @private
  310. * @returns {void}
  311. */
  312. _onSelect(selection) {
  313. this.setState({
  314. isDropdownOpen: false,
  315. selectedNumber: selection.item
  316. });
  317. }
  318. /**
  319. * Sets the internal reference to the DOM/HTML element backing the React
  320. * {@code Component} text area.
  321. *
  322. * @param {HTMLTextAreaElement} element - The DOM/HTML element for this
  323. * {@code Component}'s text area.
  324. * @private
  325. * @returns {void}
  326. */
  327. _setCopyElement(element) {
  328. this._copyElement = element;
  329. }
  330. /**
  331. * Updates the internal state of the currently selected number by defaulting
  332. * to the first available number.
  333. *
  334. * @param {Object} dialInNumbers - The array or object of numbers to parse.
  335. * @private
  336. * @returns {void}
  337. */
  338. _setDefaultNumber(dialInNumbers) {
  339. const numbers = this._formatNumbers(dialInNumbers);
  340. this.setState({
  341. selectedNumber: numbers[0]
  342. });
  343. }
  344. }
  345. /**
  346. * Maps (parts of) the Redux state to the associated
  347. * {@code DialInNumbersForm}'s props.
  348. *
  349. * @param {Object} state - The Redux state.
  350. * @private
  351. * @returns {{
  352. * _localUserDisplayName: React.PropTypes.string,
  353. * _dialIn: React.PropTypes.object
  354. * }}
  355. */
  356. function _mapStateToProps(state) {
  357. const { name }
  358. = getLocalParticipant(state['features/base/participants']);
  359. return {
  360. _localUserDisplayName: name,
  361. _dialIn: state['features/invite/dial-in']
  362. };
  363. }
  364. export default translate(connect(_mapStateToProps)(DialInNumbersForm));