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

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