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

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