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

MessageHandler.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 $.prompt(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. * @param loadedFunction function to be called after the prompt is fully loaded
  104. * @param stateChangedFunction function to be called when the state of the dialog is changed
  105. */
  106. my.openDialogWithStates = function(statesObject, loadedFunction, stateChangedFunction) {
  107. var myPrompt = $.prompt(statesObject);
  108. myPrompt.on('impromptu:loaded', loadedFunction);
  109. myPrompt.on('impromptu:statechanged', stateChangedFunction);
  110. };
  111. /**
  112. * Opens new popup window for given <tt>url</tt> centered over current
  113. * window.
  114. *
  115. * @param url the URL to be displayed in the popup window
  116. * @param w the width of the popup window
  117. * @param h the height of the popup window
  118. * @param onPopupClosed optional callback function called when popup window
  119. * has been closed.
  120. *
  121. * @returns popup window object if opened successfully or undefined
  122. * in case we failed to open it(popup blocked)
  123. */
  124. my.openCenteredPopup = function (url, w, h, onPopupClosed) {
  125. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  126. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  127. var popup = window.open(
  128. url, '_blank',
  129. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  130. if (popup && onPopupClosed) {
  131. var pollTimer = window.setInterval(function () {
  132. if (popup.closed !== false) {
  133. window.clearInterval(pollTimer);
  134. onPopupClosed();
  135. }
  136. }, 200);
  137. }
  138. return popup;
  139. };
  140. /**
  141. * Shows a dialog prompting the user to send an error report.
  142. *
  143. * @param titleString the title of the message
  144. * @param msgString the text of the message
  145. * @param error the error that is being reported
  146. */
  147. my.openReportDialog = function(titleKey, msgKey, error) {
  148. my.openMessageDialog(titleKey, msgKey);
  149. console.log(error);
  150. //FIXME send the error to the server
  151. };
  152. /**
  153. * Shows an error dialog to the user.
  154. * @param title the title of the message
  155. * @param message the text of the messafe
  156. */
  157. my.showError = function(titleKey, msgKey) {
  158. if(!titleKey) {
  159. titleKey = "dialog.oops";
  160. }
  161. if(!msgKey)
  162. {
  163. msgKey = "dialog.defaultError";
  164. }
  165. messageHandler.openMessageDialog(titleKey, msgKey);
  166. };
  167. my.notify = function(displayName, displayNameKey,
  168. cls, messageKey, messageArguments) {
  169. var displayNameSpan = '<span class="nickname" ';
  170. if(displayName)
  171. {
  172. displayNameSpan += ">" + displayName;
  173. }
  174. else
  175. {
  176. displayNameSpan += "data-i18n='" + displayNameKey +
  177. "'>" + APP.translation.translateString(displayNameKey);
  178. }
  179. displayNameSpan += "</span>";
  180. toastr.info(
  181. displayNameSpan + '<br>' +
  182. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  183. (messageArguments?
  184. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  185. : "") + ">" +
  186. APP.translation.translateString(messageKey,
  187. messageArguments) +
  188. '</span>');
  189. };
  190. return my;
  191. }(messageHandler || {}));
  192. module.exports = messageHandler;