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

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