Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MessageHandler.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. */
  104. openDialog: function (titleString, msgString, persistent, buttons,
  105. submitFunction, loadedFunction) {
  106. if (!popupEnabled)
  107. return;
  108. var args = {
  109. title: titleString,
  110. persistent: persistent,
  111. buttons: buttons,
  112. defaultButton: 1,
  113. loaded: loadedFunction,
  114. submit: submitFunction
  115. };
  116. if (persistent) {
  117. args.closeText = '';
  118. }
  119. return new Impromptu(msgString, args);
  120. },
  121. /**
  122. * Closes currently opened dialog.
  123. */
  124. closeDialog: function () {
  125. $.prompt.close();
  126. },
  127. /**
  128. * Shows a dialog with different states to the user.
  129. *
  130. * @param statesObject object containing all the states of the dialog.
  131. */
  132. openDialogWithStates: function (statesObject, options) {
  133. if (!popupEnabled)
  134. return;
  135. return new Impromptu(statesObject, options);
  136. },
  137. /**
  138. * Opens new popup window for given <tt>url</tt> centered over current
  139. * window.
  140. *
  141. * @param url the URL to be displayed in the popup window
  142. * @param w the width of the popup window
  143. * @param h the height of the popup window
  144. * @param onPopupClosed optional callback function called when popup window
  145. * has been closed.
  146. *
  147. * @returns {object} popup window object if opened successfully or undefined
  148. * in case we failed to open it(popup blocked)
  149. */
  150. openCenteredPopup: function (url, w, h, onPopupClosed) {
  151. if (!popupEnabled)
  152. return;
  153. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  154. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  155. var popup = window.open(
  156. url, '_blank',
  157. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  158. if (popup && onPopupClosed) {
  159. var pollTimer = window.setInterval(function () {
  160. if (popup.closed !== false) {
  161. window.clearInterval(pollTimer);
  162. onPopupClosed();
  163. }
  164. }, 200);
  165. }
  166. return popup;
  167. },
  168. /**
  169. * Shows a dialog prompting the user to send an error report.
  170. *
  171. * @param titleKey the title of the message
  172. * @param msgKey the text of the message
  173. * @param error the error that is being reported
  174. */
  175. openReportDialog: function(titleKey, msgKey, error) {
  176. this.openMessageDialog(titleKey, msgKey);
  177. console.log(error);
  178. //FIXME send the error to the server
  179. },
  180. /**
  181. * Shows an error dialog to the user.
  182. * @param titleKey the title of the message.
  183. * @param msgKey the text of the message.
  184. */
  185. showError: function(titleKey, msgKey) {
  186. if (!titleKey) {
  187. titleKey = "dialog.oops";
  188. }
  189. if (!msgKey) {
  190. msgKey = "dialog.defaultError";
  191. }
  192. messageHandler.openMessageDialog(titleKey, msgKey);
  193. },
  194. /**
  195. * Displays a notification.
  196. * @param displayName the display name of the participant that is
  197. * associated with the notification.
  198. * @param displayNameKey the key from the language file for the display
  199. * name. Only used if displayName i not provided.
  200. * @param cls css class for the notification
  201. * @param messageKey the key from the language file for the text of the
  202. * message.
  203. * @param messageArguments object with the arguments for the message.
  204. * @param options object with language options.
  205. */
  206. notify: function(displayName, displayNameKey, cls, messageKey,
  207. messageArguments, options) {
  208. if(!notificationsEnabled)
  209. return;
  210. var displayNameSpan = '<span class="nickname" ';
  211. if (displayName) {
  212. displayNameSpan += ">" + UIUtil.escapeHtml(displayName);
  213. } else {
  214. displayNameSpan += "data-i18n='" + displayNameKey +
  215. "'>" + APP.translation.translateString(displayNameKey);
  216. }
  217. displayNameSpan += "</span>";
  218. return toastr.info(
  219. displayNameSpan + '<br>' +
  220. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  221. (messageArguments?
  222. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  223. : "") + ">" +
  224. APP.translation.translateString(messageKey,
  225. messageArguments) +
  226. '</span>', null, options);
  227. },
  228. /**
  229. * Removes the toaster.
  230. * @param toasterElement
  231. */
  232. remove: function(toasterElement) {
  233. toasterElement.remove();
  234. },
  235. /**
  236. * Enables / disables notifications.
  237. */
  238. enableNotifications: function (enable) {
  239. notificationsEnabled = enable;
  240. },
  241. enablePopups: function (enable) {
  242. popupEnabled = enable;
  243. }
  244. };
  245. module.exports = messageHandler;