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.9KB

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