您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MessageHandler.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* global $, APP, jQuery, toastr */
  2. var messageHandler = (function(my) {
  3. /**
  4. * Shows a message to the user.
  5. *
  6. * @param titleString the title of the message
  7. * @param messageString the text of the message
  8. */
  9. my.openMessageDialog = function(titleKey, messageKey) {
  10. var title = null;
  11. if(titleKey)
  12. {
  13. title = APP.translation.generateTranslatonHTML(titleKey);
  14. }
  15. var message = APP.translation.generateTranslatonHTML(messageKey);
  16. $.prompt(message,
  17. {
  18. title: title,
  19. persistent: false
  20. }
  21. );
  22. };
  23. /**
  24. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  25. *
  26. * @param titleString the title of the message
  27. * @param msgString the text of the message
  28. * @param persistent boolean value which determines whether the message is persistent or not
  29. * @param leftButton the fist button's text
  30. * @param submitFunction function to be called on submit
  31. * @param loadedFunction function to be called after the prompt is fully loaded
  32. * @param closeFunction function to be called after the prompt is closed
  33. * @param focus optional focus selector or button index to be focused after
  34. * the dialog is opened
  35. * @param defaultButton index of default button which will be activated when
  36. * the user press 'enter'. Indexed from 0.
  37. */
  38. my.openTwoButtonDialog = function(titleKey, titleString, msgKey, msgString,
  39. persistent, leftButtonKey, submitFunction, loadedFunction,
  40. closeFunction, focus, defaultButton)
  41. {
  42. var buttons = [];
  43. var leftButton = APP.translation.generateTranslatonHTML(leftButtonKey);
  44. buttons.push({ title: leftButton, value: true});
  45. var cancelButton
  46. = APP.translation.generateTranslatonHTML("dialog.Cancel");
  47. buttons.push({title: cancelButton, value: false});
  48. var message = msgString, title = titleString;
  49. if (titleKey)
  50. {
  51. title = APP.translation.generateTranslatonHTML(titleKey);
  52. }
  53. if (msgKey) {
  54. message = APP.translation.generateTranslatonHTML(msgKey);
  55. }
  56. $.prompt(message, {
  57. title: title,
  58. persistent: false,
  59. buttons: buttons,
  60. defaultButton: defaultButton,
  61. focus: focus,
  62. loaded: loadedFunction,
  63. submit: submitFunction,
  64. close: closeFunction
  65. });
  66. };
  67. /**
  68. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  69. *
  70. * @param titleString the title of the message
  71. * @param msgString the text of the message
  72. * @param persistent boolean value which determines whether the message is persistent or not
  73. * @param buttons object with the buttons. The keys must be the name of the button and value is the value
  74. * that will be passed to submitFunction
  75. * @param submitFunction function to be called on submit
  76. * @param loadedFunction function to be called after the prompt is fully 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 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 titleString the title of the message
  140. * @param msgString 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 title the title of the message
  151. * @param message the text of the messafe
  152. */
  153. my.showError = function(titleKey, msgKey) {
  154. if(!titleKey) {
  155. titleKey = "dialog.oops";
  156. }
  157. if(!msgKey)
  158. {
  159. msgKey = "dialog.defaultError";
  160. }
  161. messageHandler.openMessageDialog(titleKey, msgKey);
  162. };
  163. my.notify = function(displayName, displayNameKey,
  164. cls, messageKey, messageArguments, options) {
  165. var displayNameSpan = '<span class="nickname" ';
  166. if(displayName)
  167. {
  168. displayNameSpan += ">" + displayName;
  169. }
  170. else
  171. {
  172. displayNameSpan += "data-i18n='" + displayNameKey +
  173. "'>" + APP.translation.translateString(displayNameKey);
  174. }
  175. displayNameSpan += "</span>";
  176. toastr.info(
  177. displayNameSpan + '<br>' +
  178. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  179. (messageArguments?
  180. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  181. : "") + ">" +
  182. APP.translation.translateString(messageKey,
  183. messageArguments) +
  184. '</span>', null, options);
  185. };
  186. return my;
  187. }(messageHandler || {}));
  188. module.exports = messageHandler;