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

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