您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MessageHandler.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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: title,
  48. persistent: false,
  49. close: function (e, v, m, f) {
  50. if(closeFunction)
  51. closeFunction(e, v, m, f);
  52. }
  53. });
  54. },
  55. /**
  56. * Shows a message to the user with two buttons: first is given as a
  57. * parameter and the second is Cancel.
  58. *
  59. * @param titleString the title of the message
  60. * @param msgString the text of the message
  61. * @param persistent boolean value which determines whether the message is
  62. * persistent or not
  63. * @param leftButton the fist button's text
  64. * @param submitFunction function to be called on submit
  65. * @param loadedFunction function to be called after the prompt is fully
  66. * loaded
  67. * @param closeFunction function to be called after the prompt is closed
  68. * @param focus optional focus selector or button index to be focused after
  69. * the dialog is opened
  70. * @param defaultButton index of default button which will be activated when
  71. * the user press 'enter'. Indexed from 0.
  72. * @return the prompt that was created, or null
  73. */
  74. openTwoButtonDialog: function(titleKey, titleString, msgKey, msgString,
  75. persistent, leftButtonKey, submitFunction, loadedFunction,
  76. closeFunction, focus, defaultButton) {
  77. if (!popupEnabled || twoButtonDialog)
  78. return null;
  79. var buttons = [];
  80. var leftButton = APP.translation.generateTranslationHTML(leftButtonKey);
  81. buttons.push({ title: leftButton, value: true});
  82. var cancelButton
  83. = APP.translation.generateTranslationHTML("dialog.Cancel");
  84. buttons.push({title: cancelButton, value: false});
  85. var message = msgString, title = titleString;
  86. if (titleKey) {
  87. title = APP.translation.generateTranslationHTML(titleKey);
  88. }
  89. if (msgKey) {
  90. message = APP.translation.generateTranslationHTML(msgKey);
  91. }
  92. twoButtonDialog = $.prompt(message, {
  93. title: title,
  94. persistent: false,
  95. buttons: buttons,
  96. defaultButton: defaultButton,
  97. focus: focus,
  98. loaded: loadedFunction,
  99. submit: function (e, v, m, f) {
  100. twoButtonDialog = null;
  101. if (submitFunction)
  102. submitFunction(e, v, m, f);
  103. },
  104. close: function (e, v, m, f) {
  105. twoButtonDialog = null;
  106. if (closeFunction)
  107. closeFunction(e, v, m, f);
  108. }
  109. });
  110. return twoButtonDialog;
  111. },
  112. /**
  113. * Shows a message to the user with two buttons: first is given as a
  114. * parameter and the second is Cancel.
  115. *
  116. * @param titleString the title of the message
  117. * @param msgString the text of the message
  118. * @param persistent boolean value which determines whether the message is
  119. * persistent or not
  120. * @param buttons object with the buttons. The keys must be the name of the
  121. * button and value is the value that will be passed to
  122. * submitFunction
  123. * @param submitFunction function to be called on submit
  124. * @param loadedFunction function to be called after the prompt is fully
  125. * loaded
  126. * @param closeFunction function to be called on dialog close
  127. */
  128. openDialog: function (titleString, msgString, persistent, buttons,
  129. submitFunction, loadedFunction, closeFunction) {
  130. if (!popupEnabled)
  131. return;
  132. var args = {
  133. title: titleString,
  134. persistent: persistent,
  135. buttons: buttons,
  136. defaultButton: 1,
  137. loaded: loadedFunction,
  138. submit: submitFunction,
  139. close: closeFunction
  140. };
  141. if (persistent) {
  142. args.closeText = '';
  143. }
  144. return new Impromptu(msgString, args);
  145. },
  146. /**
  147. * Closes currently opened dialog.
  148. */
  149. closeDialog: function () {
  150. $.prompt.close();
  151. },
  152. /**
  153. * Shows a dialog with different states to the user.
  154. *
  155. * @param statesObject object containing all the states of the dialog.
  156. */
  157. openDialogWithStates: function (statesObject, options) {
  158. if (!popupEnabled)
  159. return;
  160. return new Impromptu(statesObject, options);
  161. },
  162. /**
  163. * Opens new popup window for given <tt>url</tt> centered over current
  164. * window.
  165. *
  166. * @param url the URL to be displayed in the popup window
  167. * @param w the width of the popup window
  168. * @param h the height of the popup window
  169. * @param onPopupClosed optional callback function called when popup window
  170. * has been closed.
  171. *
  172. * @returns {object} popup window object if opened successfully or undefined
  173. * in case we failed to open it(popup blocked)
  174. */
  175. openCenteredPopup: function (url, w, h, onPopupClosed) {
  176. if (!popupEnabled)
  177. return;
  178. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  179. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  180. var popup = window.open(
  181. url, '_blank',
  182. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  183. if (popup && onPopupClosed) {
  184. var pollTimer = window.setInterval(function () {
  185. if (popup.closed !== false) {
  186. window.clearInterval(pollTimer);
  187. onPopupClosed();
  188. }
  189. }, 200);
  190. }
  191. return popup;
  192. },
  193. /**
  194. * Shows a dialog prompting the user to send an error report.
  195. *
  196. * @param titleKey the title of the message
  197. * @param msgKey the text of the message
  198. * @param error the error that is being reported
  199. */
  200. openReportDialog: function(titleKey, msgKey, error) {
  201. this.openMessageDialog(titleKey, msgKey);
  202. console.log(error);
  203. //FIXME send the error to the server
  204. },
  205. /**
  206. * Shows an error dialog to the user.
  207. * @param titleKey the title of the message.
  208. * @param msgKey the text of the message.
  209. */
  210. showError: function(titleKey, msgKey) {
  211. if (!titleKey) {
  212. titleKey = "dialog.oops";
  213. }
  214. if (!msgKey) {
  215. msgKey = "dialog.defaultError";
  216. }
  217. messageHandler.openMessageDialog(titleKey, msgKey);
  218. },
  219. /**
  220. * Displays a notification.
  221. * @param displayName the display name of the participant that is
  222. * associated with the notification.
  223. * @param displayNameKey the key from the language file for the display
  224. * name. Only used if displayName i not provided.
  225. * @param cls css class for the notification
  226. * @param messageKey the key from the language file for the text of the
  227. * message.
  228. * @param messageArguments object with the arguments for the message.
  229. * @param options object with language options.
  230. */
  231. notify: function(displayName, displayNameKey, cls, messageKey,
  232. messageArguments, options) {
  233. // If we're in ringing state we skip all toaster notifications.
  234. if(!notificationsEnabled || APP.UI.isRingOverlayVisible())
  235. return;
  236. var displayNameSpan = '<span class="nickname" ';
  237. if (displayName) {
  238. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  239. } else {
  240. displayNameSpan += "data-i18n='" + displayNameKey +
  241. "'>" + APP.translation.translateString(displayNameKey);
  242. }
  243. displayNameSpan += "</span>";
  244. return toastr.info(
  245. displayNameSpan + '<br>' +
  246. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  247. (messageArguments?
  248. " data-i18n-options='" + JSON.stringify(messageArguments)
  249. + "'"
  250. : "") + ">" +
  251. APP.translation.translateString(messageKey,
  252. messageArguments) +
  253. '</span>', null, options);
  254. },
  255. /**
  256. * Removes the toaster.
  257. * @param toasterElement
  258. */
  259. remove: function(toasterElement) {
  260. toasterElement.remove();
  261. },
  262. /**
  263. * Enables / disables notifications.
  264. */
  265. enableNotifications: function (enable) {
  266. notificationsEnabled = enable;
  267. },
  268. enablePopups: function (enable) {
  269. popupEnabled = enable;
  270. }
  271. };
  272. module.exports = messageHandler;