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

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