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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /* global $, APP, toastr */
  2. import UIUtil from './UIUtil';
  3. import jitsiLocalStorage from '../../util/JitsiLocalStorage';
  4. /**
  5. * Flag for enable/disable of the notifications.
  6. * @type {boolean}
  7. */
  8. let notificationsEnabled = true;
  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. let 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. console.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. let 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. } else {
  88. return submitFunction;
  89. }
  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. var 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. openMessageDialog:
  114. function(titleKey, messageKey, i18nOptions, closeFunction) {
  115. if (!popupEnabled)
  116. return null;
  117. let dialog = $.prompt(
  118. APP.translation.generateTranslationHTML(messageKey, i18nOptions), {
  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: loadedFunction,
  271. submit: dontShowAgainSubmitFunctionWrapper(
  272. dontShowAgain, submitFunction),
  273. close: closeFunction,
  274. classes: this._getDialogClasses()
  275. };
  276. if (persistent) {
  277. args.closeText = '';
  278. }
  279. let dialog = $.prompt(
  280. msgString + generateDontShowCheckbox(dontShowAgain), args);
  281. APP.translation.translateElement(dialog);
  282. return $.prompt.getApi();
  283. },
  284. /**
  285. * Returns the formatted title string.
  286. *
  287. * @return the title string formatted as a div.
  288. */
  289. _getFormattedTitleString(titleKey) {
  290. let $titleString = $('<h2>');
  291. $titleString.addClass('aui-dialog2-header-main');
  292. $titleString.attr('data-i18n',titleKey);
  293. return $('<div>').append($titleString).html();
  294. },
  295. /**
  296. * Returns the dialog css classes.
  297. *
  298. * @return the dialog css classes
  299. */
  300. _getDialogClasses(size = 'small') {
  301. return {
  302. box: '',
  303. form: '',
  304. prompt: `dialog aui-layer aui-dialog2 aui-dialog2-${size}`,
  305. close: 'aui-icon aui-icon-small aui-iconfont-close-dialog',
  306. fade: 'aui-blanket',
  307. button: 'button-control',
  308. message: 'aui-dialog2-content',
  309. buttons: 'aui-dialog2-footer',
  310. defaultButton: 'button-control_primary',
  311. title: 'aui-dialog2-header'
  312. };
  313. },
  314. /**
  315. * Shows a dialog with different states to the user.
  316. *
  317. * @param statesObject object containing all the states of the dialog.
  318. * @param options impromptu options
  319. * @param translateOptions options passed to translation
  320. */
  321. openDialogWithStates: function (statesObject, options, translateOptions) {
  322. if (!popupEnabled)
  323. return;
  324. let { classes, size } = options;
  325. let defaultClasses = this._getDialogClasses(size);
  326. options.classes = Object.assign({}, defaultClasses, classes);
  327. options.promptspeed = options.promptspeed || 0;
  328. for (let state in statesObject) {
  329. let currentState = statesObject[state];
  330. if(currentState.titleKey) {
  331. currentState.title
  332. = this._getFormattedTitleString(currentState.titleKey);
  333. }
  334. }
  335. let dialog = $.prompt(statesObject, options);
  336. APP.translation.translateElement(dialog, translateOptions);
  337. return $.prompt.getApi();
  338. },
  339. /**
  340. * Opens new popup window for given <tt>url</tt> centered over current
  341. * window.
  342. *
  343. * @param url the URL to be displayed in the popup window
  344. * @param w the width of the popup window
  345. * @param h the height of the popup window
  346. * @param onPopupClosed optional callback function called when popup window
  347. * has been closed.
  348. *
  349. * @returns {object} popup window object if opened successfully or undefined
  350. * in case we failed to open it(popup blocked)
  351. */
  352. openCenteredPopup: function (url, w, h, onPopupClosed) {
  353. if (!popupEnabled)
  354. return;
  355. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  356. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  357. var popup = window.open(
  358. url, '_blank',
  359. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  360. if (popup && onPopupClosed) {
  361. var pollTimer = window.setInterval(function () {
  362. if (popup.closed !== false) {
  363. window.clearInterval(pollTimer);
  364. onPopupClosed();
  365. }
  366. }, 200);
  367. }
  368. return popup;
  369. },
  370. /**
  371. * Shows a dialog prompting the user to send an error report.
  372. *
  373. * @param titleKey the title of the message
  374. * @param msgKey the text of the message
  375. * @param error the error that is being reported
  376. */
  377. openReportDialog: function(titleKey, msgKey, error) {
  378. this.openMessageDialog(titleKey, msgKey);
  379. console.log(error);
  380. //FIXME send the error to the server
  381. },
  382. /**
  383. * Shows an error dialog to the user.
  384. * @param titleKey the title of the message.
  385. * @param msgKey the text of the message.
  386. */
  387. showError: function(titleKey, msgKey) {
  388. if (!titleKey) {
  389. titleKey = "dialog.oops";
  390. }
  391. if (!msgKey) {
  392. msgKey = "dialog.defaultError";
  393. }
  394. messageHandler.openMessageDialog(titleKey, msgKey);
  395. },
  396. /**
  397. * Displays a notification.
  398. * @param displayName the display name of the participant that is
  399. * associated with the notification.
  400. * @param displayNameKey the key from the language file for the display
  401. * name. Only used if displayName i not provided.
  402. * @param cls css class for the notification
  403. * @param messageKey the key from the language file for the text of the
  404. * message.
  405. * @param messageArguments object with the arguments for the message.
  406. * @param options object with language options.
  407. */
  408. notify: function(displayName, displayNameKey, cls, messageKey,
  409. messageArguments, options) {
  410. // If we're in ringing state we skip all toaster notifications.
  411. if(!notificationsEnabled || APP.UI.isOverlayVisible())
  412. return;
  413. var displayNameSpan = '<span class="nickname" ';
  414. if (displayName) {
  415. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  416. } else {
  417. displayNameSpan += "data-i18n='" + displayNameKey + "'>";
  418. }
  419. displayNameSpan += "</span>";
  420. let element = toastr.info(
  421. displayNameSpan + '<br>' +
  422. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  423. (messageArguments?
  424. " data-i18n-options='"
  425. + JSON.stringify(messageArguments) + "'"
  426. : "") + "></span>", null, options);
  427. APP.translation.translateElement(element);
  428. return element;
  429. },
  430. /**
  431. * Removes the toaster.
  432. * @param toasterElement
  433. */
  434. remove: function(toasterElement) {
  435. toasterElement.remove();
  436. },
  437. /**
  438. * Enables / disables notifications.
  439. */
  440. enableNotifications: function (enable) {
  441. notificationsEnabled = enable;
  442. },
  443. enablePopups: function (enable) {
  444. popupEnabled = enable;
  445. },
  446. /**
  447. * Returns true if dialog is opened
  448. * false otherwise
  449. * @returns {boolean} isOpened
  450. */
  451. isDialogOpened: function () {
  452. return !!$.prompt.getCurrentStateName();
  453. }
  454. };
  455. module.exports = messageHandler;