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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * if a message is not provided.
  28. * @param message the message to show. If a falsy value is provided,
  29. * messageKey will be used to get a message via the translation API.
  30. * @param closeFunction function to be called after
  31. * the prompt is closed (optional)
  32. * @return the prompt that was created, or null
  33. */
  34. openMessageDialog: function(titleKey, messageKey, message, closeFunction) {
  35. if (!popupEnabled)
  36. return null;
  37. if (!message) {
  38. message = APP.translation.generateTranslationHTML(messageKey);
  39. }
  40. return $.prompt(message, {
  41. title: this._getFormattedTitleString(titleKey),
  42. persistent: false,
  43. promptspeed: 0,
  44. classes: this._getDialogClasses(),
  45. close: function (e, v, m, f) {
  46. if(closeFunction)
  47. closeFunction(e, v, m, f);
  48. }
  49. });
  50. },
  51. /**
  52. * Shows a message to the user with two buttons: first is given as a
  53. * parameter and the second is Cancel.
  54. *
  55. * @param titleKey the key for the title of 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. 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. return new Impromptu(msgString, args);
  163. },
  164. /**
  165. * Returns the formatted title string.
  166. *
  167. * @return the title string formatted as a div.
  168. */
  169. _getFormattedTitleString(titleKey) {
  170. let $titleString = $('<h2>');
  171. $titleString.addClass('aui-dialog2-header-main');
  172. $titleString.attr('data-i18n',titleKey);
  173. $titleString.append(APP.translation.translateString(titleKey));
  174. return $('<div>').append($titleString).html();
  175. },
  176. /**
  177. * Returns the dialog css classes.
  178. *
  179. * @return the dialog css classes
  180. */
  181. _getDialogClasses(size = 'small') {
  182. return {
  183. box: '',
  184. form: '',
  185. prompt: `dialog aui-layer aui-dialog2 aui-dialog2-${size}`,
  186. close: 'aui-icon aui-icon-small aui-iconfont-close-dialog',
  187. fade: 'aui-blanket',
  188. button: 'button-control',
  189. message: 'aui-dialog2-content',
  190. buttons: 'aui-dialog2-footer',
  191. defaultButton: 'button-control_primary',
  192. title: 'aui-dialog2-header'
  193. };
  194. },
  195. /**
  196. * Closes currently opened dialog.
  197. */
  198. closeDialog: function () {
  199. $.prompt.close();
  200. },
  201. /**
  202. * Shows a dialog with different states to the user.
  203. *
  204. * @param statesObject object containing all the states of the dialog.
  205. */
  206. openDialogWithStates: function (statesObject, options) {
  207. if (!popupEnabled)
  208. return;
  209. let { classes, size } = options;
  210. let defaultClasses = this._getDialogClasses(size);
  211. options.classes = Object.assign({}, defaultClasses, classes);
  212. options.promptspeed = options.promptspeed || 0;
  213. for (let state in statesObject) {
  214. let currentState = statesObject[state];
  215. if(currentState.titleKey) {
  216. currentState.title
  217. = this._getFormattedTitleString(currentState.titleKey);
  218. }
  219. }
  220. return new Impromptu(statesObject, options);
  221. },
  222. /**
  223. * Opens new popup window for given <tt>url</tt> centered over current
  224. * window.
  225. *
  226. * @param url the URL to be displayed in the popup window
  227. * @param w the width of the popup window
  228. * @param h the height of the popup window
  229. * @param onPopupClosed optional callback function called when popup window
  230. * has been closed.
  231. *
  232. * @returns {object} popup window object if opened successfully or undefined
  233. * in case we failed to open it(popup blocked)
  234. */
  235. openCenteredPopup: function (url, w, h, onPopupClosed) {
  236. if (!popupEnabled)
  237. return;
  238. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  239. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  240. var popup = window.open(
  241. url, '_blank',
  242. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  243. if (popup && onPopupClosed) {
  244. var pollTimer = window.setInterval(function () {
  245. if (popup.closed !== false) {
  246. window.clearInterval(pollTimer);
  247. onPopupClosed();
  248. }
  249. }, 200);
  250. }
  251. return popup;
  252. },
  253. /**
  254. * Shows a dialog prompting the user to send an error report.
  255. *
  256. * @param titleKey the title of the message
  257. * @param msgKey the text of the message
  258. * @param error the error that is being reported
  259. */
  260. openReportDialog: function(titleKey, msgKey, error) {
  261. this.openMessageDialog(titleKey, msgKey);
  262. console.log(error);
  263. //FIXME send the error to the server
  264. },
  265. /**
  266. * Shows an error dialog to the user.
  267. * @param titleKey the title of the message.
  268. * @param msgKey the text of the message.
  269. */
  270. showError: function(titleKey, msgKey) {
  271. if (!titleKey) {
  272. titleKey = "dialog.oops";
  273. }
  274. if (!msgKey) {
  275. msgKey = "dialog.defaultError";
  276. }
  277. messageHandler.openMessageDialog(titleKey, msgKey);
  278. },
  279. /**
  280. * Displays a notification.
  281. * @param displayName the display name of the participant that is
  282. * associated with the notification.
  283. * @param displayNameKey the key from the language file for the display
  284. * name. Only used if displayName i not provided.
  285. * @param cls css class for the notification
  286. * @param messageKey the key from the language file for the text of the
  287. * message.
  288. * @param messageArguments object with the arguments for the message.
  289. * @param options object with language options.
  290. */
  291. notify: function(displayName, displayNameKey, cls, messageKey,
  292. messageArguments, options) {
  293. // If we're in ringing state we skip all toaster notifications.
  294. if(!notificationsEnabled || APP.UI.isRingOverlayVisible())
  295. return;
  296. var displayNameSpan = '<span class="nickname" ';
  297. if (displayName) {
  298. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  299. } else {
  300. displayNameSpan += "data-i18n='" + displayNameKey +
  301. "'>" + APP.translation.translateString(displayNameKey);
  302. }
  303. displayNameSpan += "</span>";
  304. return toastr.info(
  305. displayNameSpan + '<br>' +
  306. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  307. (messageArguments?
  308. " data-i18n-options='" + JSON.stringify(messageArguments)
  309. + "'"
  310. : "") + ">" +
  311. APP.translation.translateString(messageKey,
  312. messageArguments) +
  313. '</span>', null, options);
  314. },
  315. /**
  316. * Removes the toaster.
  317. * @param toasterElement
  318. */
  319. remove: function(toasterElement) {
  320. toasterElement.remove();
  321. },
  322. /**
  323. * Enables / disables notifications.
  324. */
  325. enableNotifications: function (enable) {
  326. notificationsEnabled = enable;
  327. },
  328. enablePopups: function (enable) {
  329. popupEnabled = enable;
  330. },
  331. /**
  332. * Returns true if dialog is opened
  333. * false otherwise
  334. * @returns {boolean} isOpened
  335. */
  336. isDialogOpened: function () {
  337. return !!$.prompt.getCurrentStateName();
  338. }
  339. };
  340. module.exports = messageHandler;