Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

message_handler.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var messageHandler = (function(my) {
  2. /**
  3. * Shows a message to the user.
  4. *
  5. * @param titleString the title of the message
  6. * @param messageString the text of the message
  7. */
  8. my.openMessageDialog = function(titleString, messageString) {
  9. $.prompt(messageString,
  10. {
  11. title: titleString,
  12. persistent: false
  13. }
  14. );
  15. };
  16. /**
  17. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  18. *
  19. * @param titleString the title of the message
  20. * @param msgString the text of the message
  21. * @param persistent boolean value which determines whether the message is persistent or not
  22. * @param leftButton the fist button's text
  23. * @param submitFunction function to be called on submit
  24. * @param loadedFunction function to be called after the prompt is fully loaded
  25. * @param closeFunction function to be called after the prompt is closed
  26. */
  27. my.openTwoButtonDialog = function(titleString, msgString, persistent, leftButton,
  28. submitFunction, loadedFunction, closeFunction) {
  29. var buttons = {};
  30. buttons[leftButton] = true;
  31. buttons.Cancel = false;
  32. $.prompt(msgString, {
  33. title: titleString,
  34. persistent: false,
  35. buttons: buttons,
  36. defaultButton: 1,
  37. loaded: loadedFunction,
  38. submit: submitFunction,
  39. close: closeFunction
  40. });
  41. };
  42. /**
  43. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  44. *
  45. * @param titleString the title of the message
  46. * @param msgString the text of the message
  47. * @param persistent boolean value which determines whether the message is persistent or not
  48. * @param buttons object with the buttons. The keys must be the name of the button and value is the value
  49. * that will be passed to submitFunction
  50. * @param submitFunction function to be called on submit
  51. * @param loadedFunction function to be called after the prompt is fully loaded
  52. */
  53. my.openDialog = function(titleString, msgString, persistent, buttons, submitFunction, loadedFunction) {
  54. $.prompt(msgString, {
  55. title: titleString,
  56. persistent: false,
  57. buttons: buttons,
  58. defaultButton: 1,
  59. loaded: loadedFunction,
  60. submit: submitFunction
  61. });
  62. };
  63. /**
  64. * Shows a dialog with different states to the user.
  65. *
  66. * @param statesObject object containing all the states of the dialog
  67. * @param loadedFunction function to be called after the prompt is fully loaded
  68. * @param stateChangedFunction function to be called when the state of the dialog is changed
  69. */
  70. my.openDialogWithStates = function(statesObject, loadedFunction, stateChangedFunction) {
  71. var myPrompt = $.prompt(statesObject);
  72. myPrompt.on('impromptu:loaded', loadedFunction);
  73. myPrompt.on('impromptu:statechanged', stateChangedFunction);
  74. };
  75. /**
  76. * Opens new popup window for given <tt>url</tt> centered over current
  77. * window.
  78. *
  79. * @param url the URL to be displayed in the popup window
  80. * @param w the width of the popup window
  81. * @param h the height of the popup window
  82. * @param onPopupClosed optional callback function called when popup window
  83. * has been closed.
  84. */
  85. my.openCenteredPopup = function (url, w, h, onPopupClosed) {
  86. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  87. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  88. var popup = window.open(
  89. url, '_blank',
  90. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  91. if (onPopupClosed) {
  92. var pollTimer = window.setInterval(function () {
  93. if (popup.closed !== false) {
  94. window.clearInterval(pollTimer);
  95. onPopupClosed();
  96. }
  97. }, 200);
  98. }
  99. };
  100. /**
  101. * Shows a dialog prompting the user to send an error report.
  102. *
  103. * @param titleString the title of the message
  104. * @param msgString the text of the message
  105. * @param error the error that is being reported
  106. */
  107. my.openReportDialog = function(titleString, msgString, error) {
  108. my.openMessageDialog(titleString, msgString);
  109. console.log(error);
  110. //FIXME send the error to the server
  111. };
  112. /**
  113. * Shows an error dialog to the user.
  114. * @param title the title of the message
  115. * @param message the text of the messafe
  116. */
  117. my.showError = function(title, message) {
  118. if(!(title || message)) {
  119. title = title || "Oops!";
  120. message = message || "There was some kind of error";
  121. }
  122. messageHandler.openMessageDialog(title, message);
  123. };
  124. my.notify = function(displayName, cls, message) {
  125. toastr.info(
  126. '<span class="nickname">' +
  127. displayName +
  128. '</span><br>' +
  129. '<span class=' + cls + '>' +
  130. message +
  131. '</span>');
  132. };
  133. return my;
  134. }(messageHandler || {}));