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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* global $, APP, toastr, Impromptu */
  2. import UIUtil from './UIUtil';
  3. /**
  4. * Flag for enable/disable of the notifications.
  5. * @type {boolean}
  6. */
  7. let notificationsEnabled = true;
  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. var messageHandler = {
  19. OK: "dialog.OK",
  20. CANCEL: "dialog.Cancel",
  21. /**
  22. * Shows a message to the user.
  23. *
  24. * @param titleKey the key used to find the translation of the title of the
  25. * message, if a message title is not provided.
  26. * @param messageKey the key used to find the translation of the message
  27. * @param i18nOptions the i18n options (optional)
  28. * @param closeFunction function to be called after
  29. * the prompt is closed (optional)
  30. * @return the prompt that was created, or null
  31. */
  32. openMessageDialog:
  33. function(titleKey, messageKey, i18nOptions, closeFunction) {
  34. if (!popupEnabled)
  35. return null;
  36. let dialog = $.prompt(
  37. APP.translation.generateTranslationHTML(messageKey, i18nOptions), {
  38. title: this._getFormattedTitleString(titleKey),
  39. persistent: false,
  40. promptspeed: 0,
  41. classes: this._getDialogClasses(),
  42. close: function (e, v, m, f) {
  43. if(closeFunction)
  44. closeFunction(e, v, m, f);
  45. }
  46. });
  47. APP.translation.translateElement(dialog, i18nOptions);
  48. return dialog;
  49. },
  50. /**
  51. * Shows a message to the user with two buttons: first is given as a
  52. * parameter and the second is Cancel.
  53. *
  54. * @param titleKey the key for the title of the message
  55. * @param msgString the text of the message
  56. * @param persistent boolean value which determines whether the message is
  57. * persistent or not
  58. * @param leftButton the fist button's text
  59. * @param submitFunction function to be called on submit
  60. * @param loadedFunction function to be called after the prompt is fully
  61. * loaded
  62. * @param closeFunction function to be called after the prompt is closed
  63. * @param focus optional focus selector or button index to be focused after
  64. * the dialog is opened
  65. * @param defaultButton index of default button which will be activated when
  66. * the user press 'enter'. Indexed from 0.
  67. * @return the prompt that was created, or null
  68. */
  69. openTwoButtonDialog: function(options) {
  70. let {
  71. titleKey,
  72. msgKey,
  73. msgString,
  74. leftButtonKey,
  75. submitFunction,
  76. loadedFunction,
  77. closeFunction,
  78. focus,
  79. size,
  80. defaultButton,
  81. wrapperClass,
  82. classes
  83. } = options;
  84. if (!popupEnabled || twoButtonDialog)
  85. return null;
  86. var buttons = [];
  87. var leftButton = leftButtonKey ?
  88. APP.translation.generateTranslationHTML(leftButtonKey) :
  89. APP.translation.generateTranslationHTML('dialog.Submit');
  90. buttons.push({ title: leftButton, value: true});
  91. var cancelButton
  92. = APP.translation.generateTranslationHTML("dialog.Cancel");
  93. buttons.push({title: cancelButton, value: false});
  94. var message = msgString;
  95. if (msgKey) {
  96. message = APP.translation.generateTranslationHTML(msgKey);
  97. }
  98. classes = classes || this._getDialogClasses(size);
  99. if (wrapperClass) {
  100. classes.prompt += ` ${wrapperClass}`;
  101. }
  102. twoButtonDialog = $.prompt(message, {
  103. title: this._getFormattedTitleString(titleKey),
  104. persistent: false,
  105. buttons: buttons,
  106. defaultButton: defaultButton,
  107. focus: focus,
  108. loaded: loadedFunction,
  109. promptspeed: 0,
  110. classes,
  111. submit: function (e, v, m, f) {
  112. twoButtonDialog = null;
  113. if (v){
  114. if (submitFunction)
  115. submitFunction(e, v, m, f);
  116. }
  117. },
  118. close: function (e, v, m, f) {
  119. twoButtonDialog = null;
  120. if (closeFunction) {
  121. closeFunction(e, v, m, f);
  122. }
  123. }
  124. });
  125. APP.translation.translateElement(twoButtonDialog);
  126. return twoButtonDialog;
  127. },
  128. /**
  129. * Shows a message to the user with two buttons: first is given as a
  130. * parameter and the second is Cancel.
  131. *
  132. * @param titleKey the key for the title of the message
  133. * @param msgString the text of the message
  134. * @param persistent boolean value which determines whether the message is
  135. * persistent or not
  136. * @param buttons object with the buttons. The keys must be the name of the
  137. * button and value is the value that will be passed to
  138. * submitFunction
  139. * @param submitFunction function to be called on submit
  140. * @param loadedFunction function to be called after the prompt is fully
  141. * loaded
  142. * @param closeFunction function to be called on dialog close
  143. */
  144. openDialog: function (titleKey, msgString, persistent, buttons,
  145. submitFunction, loadedFunction, closeFunction) {
  146. if (!popupEnabled)
  147. return;
  148. let args = {
  149. title: this._getFormattedTitleString(titleKey),
  150. persistent: persistent,
  151. buttons: buttons,
  152. defaultButton: 1,
  153. promptspeed: 0,
  154. loaded: loadedFunction,
  155. submit: submitFunction,
  156. close: closeFunction,
  157. classes: this._getDialogClasses()
  158. };
  159. if (persistent) {
  160. args.closeText = '';
  161. }
  162. let dialog = new Impromptu(msgString, args);
  163. APP.translation.translateElement(dialog.getPrompt());
  164. return dialog;
  165. },
  166. /**
  167. * Returns the formatted title string.
  168. *
  169. * @return the title string formatted as a div.
  170. */
  171. _getFormattedTitleString(titleKey) {
  172. let $titleString = $('<h2>');
  173. $titleString.addClass('aui-dialog2-header-main');
  174. $titleString.attr('data-i18n',titleKey);
  175. return $('<div>').append($titleString).html();
  176. },
  177. /**
  178. * Returns the dialog css classes.
  179. *
  180. * @return the dialog css classes
  181. */
  182. _getDialogClasses(size = 'small') {
  183. return {
  184. box: '',
  185. form: '',
  186. prompt: `dialog aui-layer aui-dialog2 aui-dialog2-${size}`,
  187. close: 'aui-icon aui-icon-small aui-iconfont-close-dialog',
  188. fade: 'aui-blanket',
  189. button: 'button-control',
  190. message: 'aui-dialog2-content',
  191. buttons: 'aui-dialog2-footer',
  192. defaultButton: 'button-control_primary',
  193. title: 'aui-dialog2-header'
  194. };
  195. },
  196. /**
  197. * Closes currently opened dialog.
  198. */
  199. closeDialog: function () {
  200. $.prompt.close();
  201. },
  202. /**
  203. * Shows a dialog with different states to the user.
  204. *
  205. * @param statesObject object containing all the states of the dialog.
  206. * @param options impromptu options
  207. * @param translateOptions options passed to translation
  208. */
  209. openDialogWithStates: function (statesObject, options, translateOptions) {
  210. if (!popupEnabled)
  211. return;
  212. let { classes, size } = options;
  213. let defaultClasses = this._getDialogClasses(size);
  214. options.classes = Object.assign({}, defaultClasses, classes);
  215. options.promptspeed = options.promptspeed || 0;
  216. for (let state in statesObject) {
  217. let currentState = statesObject[state];
  218. if(currentState.titleKey) {
  219. currentState.title
  220. = this._getFormattedTitleString(currentState.titleKey);
  221. }
  222. }
  223. let dialog = new Impromptu(statesObject, options);
  224. APP.translation.translateElement(dialog.getPrompt(), translateOptions);
  225. return dialog;
  226. },
  227. /**
  228. * Opens new popup window for given <tt>url</tt> centered over current
  229. * window.
  230. *
  231. * @param url the URL to be displayed in the popup window
  232. * @param w the width of the popup window
  233. * @param h the height of the popup window
  234. * @param onPopupClosed optional callback function called when popup window
  235. * has been closed.
  236. *
  237. * @returns {object} popup window object if opened successfully or undefined
  238. * in case we failed to open it(popup blocked)
  239. */
  240. openCenteredPopup: function (url, w, h, onPopupClosed) {
  241. if (!popupEnabled)
  242. return;
  243. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  244. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  245. var popup = window.open(
  246. url, '_blank',
  247. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  248. if (popup && onPopupClosed) {
  249. var pollTimer = window.setInterval(function () {
  250. if (popup.closed !== false) {
  251. window.clearInterval(pollTimer);
  252. onPopupClosed();
  253. }
  254. }, 200);
  255. }
  256. return popup;
  257. },
  258. /**
  259. * Shows a dialog prompting the user to send an error report.
  260. *
  261. * @param titleKey the title of the message
  262. * @param msgKey the text of the message
  263. * @param error the error that is being reported
  264. */
  265. openReportDialog: function(titleKey, msgKey, error) {
  266. this.openMessageDialog(titleKey, msgKey);
  267. console.log(error);
  268. //FIXME send the error to the server
  269. },
  270. /**
  271. * Shows an error dialog to the user.
  272. * @param titleKey the title of the message.
  273. * @param msgKey the text of the message.
  274. */
  275. showError: function(titleKey, msgKey) {
  276. if (!titleKey) {
  277. titleKey = "dialog.oops";
  278. }
  279. if (!msgKey) {
  280. msgKey = "dialog.defaultError";
  281. }
  282. messageHandler.openMessageDialog(titleKey, msgKey);
  283. },
  284. /**
  285. * Displays a notification.
  286. * @param displayName the display name of the participant that is
  287. * associated with the notification.
  288. * @param displayNameKey the key from the language file for the display
  289. * name. Only used if displayName i not provided.
  290. * @param cls css class for the notification
  291. * @param messageKey the key from the language file for the text of the
  292. * message.
  293. * @param messageArguments object with the arguments for the message.
  294. * @param options object with language options.
  295. */
  296. notify: function(displayName, displayNameKey, cls, messageKey,
  297. messageArguments, options) {
  298. // If we're in ringing state we skip all toaster notifications.
  299. if(!notificationsEnabled || APP.UI.isRingOverlayVisible())
  300. return;
  301. var displayNameSpan = '<span class="nickname" ';
  302. if (displayName) {
  303. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  304. } else {
  305. displayNameSpan += "data-i18n='" + displayNameKey + "'>";
  306. }
  307. displayNameSpan += "</span>";
  308. let element = toastr.info(
  309. displayNameSpan + '<br>' +
  310. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  311. (messageArguments?
  312. " data-i18n-options='"
  313. + JSON.stringify(messageArguments) + "'"
  314. : "") + "></span>", null, options);
  315. APP.translation.translateElement(element);
  316. return element;
  317. },
  318. /**
  319. * Removes the toaster.
  320. * @param toasterElement
  321. */
  322. remove: function(toasterElement) {
  323. toasterElement.remove();
  324. },
  325. /**
  326. * Enables / disables notifications.
  327. */
  328. enableNotifications: function (enable) {
  329. notificationsEnabled = enable;
  330. },
  331. enablePopups: function (enable) {
  332. popupEnabled = enable;
  333. },
  334. /**
  335. * Returns true if dialog is opened
  336. * false otherwise
  337. * @returns {boolean} isOpened
  338. */
  339. isDialogOpened: function () {
  340. return !!$.prompt.getCurrentStateName();
  341. }
  342. };
  343. module.exports = messageHandler;