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

MessageHandler.js 7.0KB

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