您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MessageHandler.js 8.5KB

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