Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MessageHandler.js 8.6KB

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