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.4KB

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