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

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