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.

message_handler.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * @returns popup window object if opened successfully or undefined
  86. * in case we failed to open it(popup blocked)
  87. */
  88. my.openCenteredPopup = function (url, w, h, onPopupClosed) {
  89. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  90. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  91. var popup = window.open(
  92. url, '_blank',
  93. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  94. if (popup && onPopupClosed) {
  95. var pollTimer = window.setInterval(function () {
  96. if (popup.closed !== false) {
  97. window.clearInterval(pollTimer);
  98. onPopupClosed();
  99. }
  100. }, 200);
  101. }
  102. return popup;
  103. };
  104. /**
  105. * Shows a dialog prompting the user to send an error report.
  106. *
  107. * @param titleString the title of the message
  108. * @param msgString the text of the message
  109. * @param error the error that is being reported
  110. */
  111. my.openReportDialog = function(titleString, msgString, error) {
  112. my.openMessageDialog(titleString, msgString);
  113. console.log(error);
  114. //FIXME send the error to the server
  115. };
  116. /**
  117. * Shows an error dialog to the user.
  118. * @param title the title of the message
  119. * @param message the text of the messafe
  120. */
  121. my.showError = function(title, message) {
  122. if(!(title || message)) {
  123. title = title || "Oops!";
  124. message = message || "There was some kind of error";
  125. }
  126. messageHandler.openMessageDialog(title, message);
  127. };
  128. my.notify = function(displayName, cls, message) {
  129. toastr.info(
  130. '<span class="nickname">' +
  131. displayName +
  132. '</span><br>' +
  133. '<span class=' + cls + '>' +
  134. message +
  135. '</span>');
  136. };
  137. return my;
  138. }(messageHandler || {}));