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.

MessageHandler.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* global $, APP */
  2. const logger = require('jitsi-meet-logger').getLogger(__filename);
  3. import jitsiLocalStorage from '../../util/JitsiLocalStorage';
  4. import {
  5. Notification,
  6. showErrorNotification,
  7. showNotification,
  8. showWarningNotification
  9. } from '../../../react/features/notifications';
  10. /**
  11. * Flag for enabling/disabling popups.
  12. * @type {boolean}
  13. */
  14. let popupEnabled = true;
  15. /**
  16. * Currently displayed two button dialog.
  17. * @type {null}
  18. */
  19. let twoButtonDialog = null;
  20. /**
  21. * Generates html for dont show again checkbox.
  22. * @param {object} options options
  23. * @param {string} options.id the id of the checkbox.
  24. * @param {string} options.textKey the key for the text displayed next to
  25. * checkbox
  26. * @param {boolean} options.checked if true the checkbox is foing to be checked
  27. * by default.
  28. * @returns {string}
  29. */
  30. function generateDontShowCheckbox(options) {
  31. if (!isDontShowAgainEnabled(options)) {
  32. return '';
  33. }
  34. const checked
  35. = options.checked === true ? 'checked' : '';
  36. return `<br />
  37. <label>
  38. <input type='checkbox' ${checked} id='${options.id}' />
  39. <span data-i18n='${options.textKey}'></span>
  40. </label>`;
  41. }
  42. /**
  43. * Checks whether the dont show again checkbox was checked before.
  44. * @param {object} options - options for dont show again checkbox.
  45. * @param {string} options.id the id of the checkbox.
  46. * @param {string} options.localStorageKey the key for the local storage. if
  47. * not provided options.id will be used.
  48. * @returns {boolean} true if the dialog mustn't be displayed and
  49. * false otherwise.
  50. */
  51. function dontShowTheDialog(options) {
  52. if (isDontShowAgainEnabled(options)) {
  53. if (jitsiLocalStorage.getItem(options.localStorageKey || options.id)
  54. === 'true') {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. /**
  61. * Wraps the submit function to process the dont show again status and store
  62. * it.
  63. * @param {object} options - options for dont show again checkbox.
  64. * @param {string} options.id the id of the checkbox.
  65. * @param {Array} options.buttonValues The button values that will trigger
  66. * storing he checkbox value
  67. * @param {string} options.localStorageKey the key for the local storage. if
  68. * not provided options.id will be used.
  69. * @param {Function} submitFunction the submit function to be wrapped
  70. * @returns {Function} wrapped function
  71. */
  72. function dontShowAgainSubmitFunctionWrapper(options, submitFunction) {
  73. if (isDontShowAgainEnabled(options)) {
  74. return (...args) => {
  75. logger.debug(args, options.buttonValues);
  76. // args[1] is the value associated with the pressed button
  77. if (!options.buttonValues || options.buttonValues.length === 0
  78. || options.buttonValues.indexOf(args[1]) !== -1) {
  79. const checkbox = $(`#${options.id}`);
  80. if (checkbox.length) {
  81. jitsiLocalStorage.setItem(
  82. options.localStorageKey || options.id,
  83. checkbox.prop('checked'));
  84. }
  85. }
  86. submitFunction(...args);
  87. };
  88. }
  89. return submitFunction;
  90. }
  91. /**
  92. * Check whether dont show again checkbox is enabled or not.
  93. * @param {object} options - options for dont show again checkbox.
  94. * @returns {boolean} true if enabled and false if not.
  95. */
  96. function isDontShowAgainEnabled(options) {
  97. return typeof options === 'object';
  98. }
  99. const messageHandler = {
  100. OK: 'dialog.OK',
  101. CANCEL: 'dialog.Cancel',
  102. /**
  103. * Shows a message to the user.
  104. *
  105. * @param titleKey the key used to find the translation of the title of the
  106. * message, if a message title is not provided.
  107. * @param messageKey the key used to find the translation of the message
  108. * @param i18nOptions the i18n options (optional)
  109. * @param closeFunction function to be called after
  110. * the prompt is closed (optional)
  111. * @return the prompt that was created, or null
  112. */
  113. // eslint-disable-next-line max-params
  114. openMessageDialog(titleKey, messageKey, i18nOptions, closeFunction) {
  115. if (!popupEnabled) {
  116. return null;
  117. }
  118. const dialog = $.prompt(
  119. APP.translation.generateTranslationHTML(messageKey, i18nOptions),
  120. {
  121. title: this._getFormattedTitleString(titleKey),
  122. persistent: false,
  123. promptspeed: 0,
  124. classes: this._getDialogClasses(),
  125. // eslint-disable-next-line max-params
  126. close(e, v, m, f) {
  127. if (closeFunction) {
  128. closeFunction(e, v, m, f);
  129. }
  130. }
  131. });
  132. APP.translation.translateElement(dialog, i18nOptions);
  133. return $.prompt.getApi();
  134. },
  135. /**
  136. * Shows a message to the user with two buttons: first is given as a
  137. * parameter and the second is Cancel.
  138. *
  139. * @param titleKey the key for the title of the message
  140. * @param msgKey the key for the message
  141. * @param msgString the text of the message
  142. * @param persistent boolean value which determines whether the message is
  143. * persistent or not
  144. * @param leftButton the fist button's text
  145. * @param submitFunction function to be called on submit
  146. * @param loadedFunction function to be called after the prompt is fully
  147. * loaded
  148. * @param closeFunction function to be called after the prompt is closed
  149. * @param focus optional focus selector or button index to be focused after
  150. * the dialog is opened
  151. * @param defaultButton index of default button which will be activated when
  152. * the user press 'enter'. Indexed from 0.
  153. * @param {object} dontShowAgain - options for dont show again checkbox.
  154. * @param {string} dontShowAgain.id the id of the checkbox.
  155. * @param {string} dontShowAgain.textKey the key for the text displayed
  156. * next to checkbox
  157. * @param {boolean} dontShowAgain.checked if true the checkbox is foing to
  158. * be checked
  159. * @param {Array} dontShowAgain.buttonValues The button values that will
  160. * trigger storing the checkbox value
  161. * @param {string} dontShowAgain.localStorageKey the key for the local
  162. * storage. if not provided dontShowAgain.id will be used.
  163. * @return the prompt that was created, or null
  164. */
  165. openTwoButtonDialog(options) {
  166. const {
  167. titleKey,
  168. msgKey,
  169. msgString,
  170. leftButtonKey,
  171. submitFunction,
  172. loadedFunction,
  173. closeFunction,
  174. focus,
  175. size,
  176. defaultButton,
  177. wrapperClass,
  178. dontShowAgain
  179. } = options;
  180. let { classes } = options;
  181. if (!popupEnabled || twoButtonDialog) {
  182. return null;
  183. }
  184. if (dontShowTheDialog(dontShowAgain)) {
  185. // Maybe we should pass some parameters here? I'm not sure
  186. // and currently we don't need any parameters.
  187. submitFunction();
  188. return null;
  189. }
  190. const buttons = [];
  191. const leftButton = leftButtonKey
  192. ? APP.translation.generateTranslationHTML(leftButtonKey)
  193. : APP.translation.generateTranslationHTML('dialog.Submit');
  194. buttons.push({ title: leftButton,
  195. value: true });
  196. const cancelButton
  197. = APP.translation.generateTranslationHTML('dialog.Cancel');
  198. buttons.push({ title: cancelButton,
  199. value: false });
  200. let message = msgString;
  201. if (msgKey) {
  202. message = APP.translation.generateTranslationHTML(msgKey);
  203. }
  204. message += generateDontShowCheckbox(dontShowAgain);
  205. classes = classes || this._getDialogClasses(size);
  206. if (wrapperClass) {
  207. classes.prompt += ` ${wrapperClass}`;
  208. }
  209. twoButtonDialog = $.prompt(message, {
  210. title: this._getFormattedTitleString(titleKey),
  211. persistent: false,
  212. buttons,
  213. defaultButton,
  214. focus,
  215. loaded: loadedFunction,
  216. promptspeed: 0,
  217. classes,
  218. submit: dontShowAgainSubmitFunctionWrapper(dontShowAgain,
  219. (e, v, m, f) => { // eslint-disable-line max-params
  220. twoButtonDialog = null;
  221. if (v && submitFunction) {
  222. submitFunction(e, v, m, f);
  223. }
  224. }),
  225. close(e, v, m, f) { // eslint-disable-line max-params
  226. twoButtonDialog = null;
  227. if (closeFunction) {
  228. closeFunction(e, v, m, f);
  229. }
  230. }
  231. });
  232. APP.translation.translateElement(twoButtonDialog);
  233. return $.prompt.getApi();
  234. },
  235. /**
  236. * Shows a message to the user with two buttons: first is given as a
  237. * parameter and the second is Cancel.
  238. *
  239. * @param titleKey the key for the title of the message
  240. * @param msgString the text of the message
  241. * @param persistent boolean value which determines whether the message is
  242. * persistent or not
  243. * @param buttons object with the buttons. The keys must be the name of the
  244. * button and value is the value that will be passed to
  245. * submitFunction
  246. * @param submitFunction function to be called on submit
  247. * @param loadedFunction function to be called after the prompt is fully
  248. * loaded
  249. * @param closeFunction function to be called on dialog close
  250. * @param {object} dontShowAgain - options for dont show again checkbox.
  251. * @param {string} dontShowAgain.id the id of the checkbox.
  252. * @param {string} dontShowAgain.textKey the key for the text displayed
  253. * next to checkbox
  254. * @param {boolean} dontShowAgain.checked if true the checkbox is foing to
  255. * be checked
  256. * @param {Array} dontShowAgain.buttonValues The button values that will
  257. * trigger storing the checkbox value
  258. * @param {string} dontShowAgain.localStorageKey the key for the local
  259. * storage. if not provided dontShowAgain.id will be used.
  260. */
  261. openDialog(// eslint-disable-line max-params
  262. titleKey,
  263. msgString,
  264. persistent,
  265. buttons,
  266. submitFunction,
  267. loadedFunction,
  268. closeFunction,
  269. dontShowAgain) {
  270. if (!popupEnabled) {
  271. return;
  272. }
  273. if (dontShowTheDialog(dontShowAgain)) {
  274. // Maybe we should pass some parameters here? I'm not sure
  275. // and currently we don't need any parameters.
  276. submitFunction();
  277. return;
  278. }
  279. const args = {
  280. title: this._getFormattedTitleString(titleKey),
  281. persistent,
  282. buttons,
  283. defaultButton: 1,
  284. promptspeed: 0,
  285. loaded() {
  286. if (loadedFunction) {
  287. // eslint-disable-next-line prefer-rest-params
  288. loadedFunction.apply(this, arguments);
  289. }
  290. // Hide the close button
  291. if (persistent) {
  292. $('.jqiclose', this).hide();
  293. }
  294. },
  295. submit: dontShowAgainSubmitFunctionWrapper(
  296. dontShowAgain, submitFunction),
  297. close: closeFunction,
  298. classes: this._getDialogClasses()
  299. };
  300. if (persistent) {
  301. args.closeText = '';
  302. }
  303. const dialog = $.prompt(
  304. msgString + generateDontShowCheckbox(dontShowAgain), args);
  305. APP.translation.translateElement(dialog);
  306. return $.prompt.getApi();
  307. },
  308. /**
  309. * Returns the formatted title string.
  310. *
  311. * @return the title string formatted as a div.
  312. */
  313. _getFormattedTitleString(titleKey) {
  314. const $titleString = $('<h2>');
  315. $titleString.addClass('aui-dialog2-header-main');
  316. $titleString.attr('data-i18n', titleKey);
  317. return $('<div>').append($titleString)
  318. .html();
  319. },
  320. /**
  321. * Returns the dialog css classes.
  322. *
  323. * @return the dialog css classes
  324. */
  325. _getDialogClasses(size = 'small') {
  326. return {
  327. box: '',
  328. form: '',
  329. prompt: `dialog aui-layer aui-dialog2 aui-dialog2-${size}`,
  330. close: 'aui-icon aui-icon-small aui-iconfont-close-dialog',
  331. fade: 'aui-blanket',
  332. button: 'button-control',
  333. message: 'aui-dialog2-content',
  334. buttons: 'aui-dialog2-footer',
  335. defaultButton: 'button-control_primary',
  336. title: 'aui-dialog2-header'
  337. };
  338. },
  339. /**
  340. * Shows a dialog with different states to the user.
  341. *
  342. * @param statesObject object containing all the states of the dialog.
  343. * @param options impromptu options
  344. * @param translateOptions options passed to translation
  345. */
  346. openDialogWithStates(statesObject, options, translateOptions) {
  347. if (!popupEnabled) {
  348. return;
  349. }
  350. const { classes, size } = options;
  351. const defaultClasses = this._getDialogClasses(size);
  352. options.classes = Object.assign({}, defaultClasses, classes);
  353. options.promptspeed = options.promptspeed || 0;
  354. for (const state in statesObject) { // eslint-disable-line guard-for-in
  355. const currentState = statesObject[state];
  356. if (currentState.titleKey) {
  357. currentState.title
  358. = this._getFormattedTitleString(currentState.titleKey);
  359. }
  360. }
  361. const dialog = $.prompt(statesObject, options);
  362. APP.translation.translateElement(dialog, translateOptions);
  363. return $.prompt.getApi();
  364. },
  365. /**
  366. * Opens new popup window for given <tt>url</tt> centered over current
  367. * window.
  368. *
  369. * @param url the URL to be displayed in the popup window
  370. * @param w the width of the popup window
  371. * @param h the height of the popup window
  372. * @param onPopupClosed optional callback function called when popup window
  373. * has been closed.
  374. *
  375. * @returns {object} popup window object if opened successfully or undefined
  376. * in case we failed to open it(popup blocked)
  377. */
  378. // eslint-disable-next-line max-params
  379. openCenteredPopup(url, w, h, onPopupClosed) {
  380. if (!popupEnabled) {
  381. return;
  382. }
  383. const l = window.screenX + (window.innerWidth / 2) - (w / 2);
  384. const t = window.screenY + (window.innerHeight / 2) - (h / 2);
  385. const popup = window.open(
  386. url, '_blank',
  387. String(`top=${t}, left=${l}, width=${w}, height=${h}`));
  388. if (popup && onPopupClosed) {
  389. const pollTimer = window.setInterval(() => {
  390. if (popup.closed !== false) {
  391. window.clearInterval(pollTimer);
  392. onPopupClosed();
  393. }
  394. }, 200);
  395. }
  396. return popup;
  397. },
  398. /**
  399. * Shows an error dialog to the user.
  400. *
  401. * @param {object} props - The properties to pass to the
  402. * showErrorNotification action.
  403. */
  404. showError(props) {
  405. APP.store.dispatch(showErrorNotification(props));
  406. },
  407. /**
  408. * Shows a warning dialog to the user.
  409. *
  410. * @param {object} props - The properties to pass to the
  411. * showWarningNotification action.
  412. */
  413. showWarning(props) {
  414. APP.store.dispatch(showWarningNotification(props));
  415. },
  416. /**
  417. * Displays a notification about participant action.
  418. * @param displayName the display name of the participant that is
  419. * associated with the notification.
  420. * @param displayNameKey the key from the language file for the display
  421. * name. Only used if displayName i not provided.
  422. * @param cls css class for the notification
  423. * @param messageKey the key from the language file for the text of the
  424. * message.
  425. * @param messageArguments object with the arguments for the message.
  426. * @param optional configurations for the notification (e.g. timeout)
  427. */
  428. participantNotification( // eslint-disable-line max-params
  429. displayName,
  430. displayNameKey,
  431. cls,
  432. messageKey,
  433. messageArguments,
  434. timeout = 2500) {
  435. APP.store.dispatch(
  436. showNotification(
  437. Notification,
  438. {
  439. descriptionArguments: messageArguments,
  440. descriptionKey: messageKey,
  441. titleKey: displayNameKey,
  442. title: displayName
  443. },
  444. timeout));
  445. },
  446. /**
  447. * Displays a notification.
  448. *
  449. * @param {string} titleKey - The key from the language file for the title
  450. * of the notification.
  451. * @param {string} messageKey - The key from the language file for the text
  452. * of the message.
  453. * @param {Object} messageArguments - The arguments for the message
  454. * translation.
  455. * @returns {void}
  456. */
  457. notify(titleKey, messageKey, messageArguments) {
  458. this.participantNotification(
  459. null, titleKey, null, messageKey, messageArguments);
  460. },
  461. enablePopups(enable) {
  462. popupEnabled = enable;
  463. },
  464. /**
  465. * Returns true if dialog is opened
  466. * false otherwise
  467. * @returns {boolean} isOpened
  468. */
  469. isDialogOpened() {
  470. return Boolean($.prompt.getCurrentStateName());
  471. }
  472. };
  473. export default messageHandler;