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

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