Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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