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

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