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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. closedFunction) {
  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 () {
  51. if(closedFunction) closedFunction();
  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 () {
  105. twoButtonDialog = null;
  106. if (closeFunction)
  107. closeFunction();
  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(!notificationsEnabled)
  234. return;
  235. var displayNameSpan = '<span class="nickname" ';
  236. if (displayName) {
  237. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  238. } else {
  239. displayNameSpan += "data-i18n='" + displayNameKey +
  240. "'>" + APP.translation.translateString(displayNameKey);
  241. }
  242. displayNameSpan += "</span>";
  243. return toastr.info(
  244. displayNameSpan + '<br>' +
  245. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  246. (messageArguments?
  247. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  248. : "") + ">" +
  249. APP.translation.translateString(messageKey,
  250. messageArguments) +
  251. '</span>', null, options);
  252. },
  253. /**
  254. * Removes the toaster.
  255. * @param toasterElement
  256. */
  257. remove: function(toasterElement) {
  258. toasterElement.remove();
  259. },
  260. /**
  261. * Enables / disables notifications.
  262. */
  263. enableNotifications: function (enable) {
  264. notificationsEnabled = enable;
  265. },
  266. enablePopups: function (enable) {
  267. popupEnabled = enable;
  268. }
  269. };
  270. module.exports = messageHandler;