Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

MessageHandler.js 16KB

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