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 7.0KB

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