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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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
  79. * parameter and the second is Cancel.
  80. *
  81. * @param titleString the title of the message
  82. * @param msgString the text of the message
  83. * @param persistent boolean value which determines whether the message is
  84. * persistent or not
  85. * @param buttons object with the buttons. The keys must be the name of the
  86. * button and value is the value that will be passed to
  87. * submitFunction
  88. * @param submitFunction function to be called on submit
  89. * @param loadedFunction function to be called after the prompt is fully
  90. * loaded
  91. */
  92. my.openDialog = function (titleString, msgString, persistent, buttons,
  93. submitFunction, loadedFunction) {
  94. var args = {
  95. title: titleString,
  96. persistent: persistent,
  97. buttons: buttons,
  98. defaultButton: 1,
  99. loaded: loadedFunction,
  100. submit: submitFunction
  101. };
  102. if (persistent) {
  103. args.closeText = '';
  104. }
  105. return new Impromptu(msgString, args);
  106. };
  107. /**
  108. * Closes currently opened dialog.
  109. */
  110. my.closeDialog = function () {
  111. $.prompt.close();
  112. };
  113. /**
  114. * Shows a dialog with different states to the user.
  115. *
  116. * @param statesObject object containing all the states of the dialog.
  117. */
  118. my.openDialogWithStates = function (statesObject, options) {
  119. return new Impromptu(statesObject, options);
  120. };
  121. /**
  122. * Opens new popup window for given <tt>url</tt> centered over current
  123. * window.
  124. *
  125. * @param url the URL to be displayed in the popup window
  126. * @param w the width of the popup window
  127. * @param h the height of the popup window
  128. * @param onPopupClosed optional callback function called when popup window
  129. * has been closed.
  130. *
  131. * @returns {object} popup window object if opened successfully or undefined
  132. * in case we failed to open it(popup blocked)
  133. */
  134. my.openCenteredPopup = function (url, w, h, onPopupClosed) {
  135. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  136. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  137. var popup = window.open(
  138. url, '_blank',
  139. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  140. if (popup && onPopupClosed) {
  141. var pollTimer = window.setInterval(function () {
  142. if (popup.closed !== false) {
  143. window.clearInterval(pollTimer);
  144. onPopupClosed();
  145. }
  146. }, 200);
  147. }
  148. return popup;
  149. };
  150. /**
  151. * Shows a dialog prompting the user to send an error report.
  152. *
  153. * @param titleKey the title of the message
  154. * @param msgKey the text of the message
  155. * @param error the error that is being reported
  156. */
  157. my.openReportDialog = function(titleKey, msgKey, error) {
  158. my.openMessageDialog(titleKey, msgKey);
  159. console.log(error);
  160. //FIXME send the error to the server
  161. };
  162. /**
  163. * Shows an error dialog to the user.
  164. * @param titleKey the title of the message.
  165. * @param msgKey the text of the message.
  166. */
  167. my.showError = function(titleKey, msgKey) {
  168. if (!titleKey) {
  169. titleKey = "dialog.oops";
  170. }
  171. if (!msgKey) {
  172. msgKey = "dialog.defaultError";
  173. }
  174. messageHandler.openMessageDialog(titleKey, msgKey);
  175. };
  176. /**
  177. * Displayes notification.
  178. * @param displayName display name of the participant that is associated with the notification.
  179. * @param displayNameKey the key from the language file for the display name.
  180. * @param cls css class for the notification
  181. * @param messageKey the key from the language file for the text of the message.
  182. * @param messageArguments object with the arguments for the message.
  183. * @param options object with language options.
  184. */
  185. my.notify = function(displayName, displayNameKey,
  186. cls, messageKey, messageArguments, options) {
  187. if(!notificationsEnabled)
  188. return;
  189. var displayNameSpan = '<span class="nickname" ';
  190. if (displayName) {
  191. displayNameSpan += ">" + displayName;
  192. } else {
  193. displayNameSpan += "data-i18n='" + displayNameKey +
  194. "'>" + APP.translation.translateString(displayNameKey);
  195. }
  196. displayNameSpan += "</span>";
  197. return toastr.info(
  198. displayNameSpan + '<br>' +
  199. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  200. (messageArguments?
  201. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  202. : "") + ">" +
  203. APP.translation.translateString(messageKey,
  204. messageArguments) +
  205. '</span>', null, options);
  206. };
  207. /**
  208. * Removes the toaster.
  209. * @param toasterElement
  210. */
  211. my.remove = function(toasterElement) {
  212. toasterElement.remove();
  213. };
  214. /**
  215. * Disables notifications.
  216. */
  217. my.disableNotifications = function () {
  218. notificationsEnabled = false;
  219. };
  220. /**
  221. * Enables notifications.
  222. */
  223. my.enableNotifications = function () {
  224. notificationsEnabled = true;
  225. };
  226. return my;
  227. }(messageHandler || {}));
  228. module.exports = messageHandler;