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

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