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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* global $, APP, jQuery, toastr */
  17. var messageHandler = (function(my) {
  18. /**
  19. * Shows a message to the user.
  20. *
  21. * @param titleString the title of the message
  22. * @param messageString the text of the message
  23. */
  24. my.openMessageDialog = function(titleKey, messageKey) {
  25. var title = null;
  26. if(titleKey)
  27. {
  28. title = APP.translation.generateTranslatonHTML(titleKey);
  29. }
  30. var message = APP.translation.generateTranslatonHTML(messageKey);
  31. $.prompt(message,
  32. {
  33. title: title,
  34. persistent: false
  35. }
  36. );
  37. };
  38. /**
  39. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  40. *
  41. * @param titleString the title of the message
  42. * @param msgString the text of the message
  43. * @param persistent boolean value which determines whether the message is persistent or not
  44. * @param leftButton the fist button's text
  45. * @param submitFunction function to be called on submit
  46. * @param loadedFunction function to be called after the prompt is fully loaded
  47. * @param closeFunction function to be called after the prompt is closed
  48. * @param focus optional focus selector or button index to be focused after
  49. * the dialog is opened
  50. * @param defaultButton index of default button which will be activated when
  51. * the user press 'enter'. Indexed from 0.
  52. */
  53. my.openTwoButtonDialog = function(titleKey, titleString, msgKey, msgString,
  54. persistent, leftButtonKey, submitFunction, loadedFunction,
  55. closeFunction, focus, defaultButton)
  56. {
  57. var buttons = [];
  58. var leftButton = APP.translation.generateTranslatonHTML(leftButtonKey);
  59. buttons.push({ title: leftButton, value: true});
  60. var cancelButton
  61. = APP.translation.generateTranslatonHTML("dialog.Cancel");
  62. buttons.push({title: cancelButton, value: false});
  63. var message = msgString, title = titleString;
  64. if (titleKey)
  65. {
  66. title = APP.translation.generateTranslatonHTML(titleKey);
  67. }
  68. if (msgKey) {
  69. message = APP.translation.generateTranslatonHTML(msgKey);
  70. }
  71. $.prompt(message, {
  72. title: title,
  73. persistent: false,
  74. buttons: buttons,
  75. defaultButton: defaultButton,
  76. focus: focus,
  77. loaded: loadedFunction,
  78. submit: submitFunction,
  79. close: closeFunction
  80. });
  81. };
  82. /**
  83. * Shows a message to the user with two buttons: first is given as a parameter and the second is Cancel.
  84. *
  85. * @param titleString the title of the message
  86. * @param msgString the text of the message
  87. * @param persistent boolean value which determines whether the message is persistent or not
  88. * @param buttons object with the buttons. The keys must be the name of the button and value is the value
  89. * that will be passed to submitFunction
  90. * @param submitFunction function to be called on submit
  91. * @param loadedFunction function to be called after the prompt is fully loaded
  92. */
  93. my.openDialog = function (titleString, msgString, persistent, buttons,
  94. submitFunction, loadedFunction) {
  95. var args = {
  96. title: titleString,
  97. persistent: persistent,
  98. buttons: buttons,
  99. defaultButton: 1,
  100. loaded: loadedFunction,
  101. submit: submitFunction
  102. };
  103. if (persistent) {
  104. args.closeText = '';
  105. }
  106. return new Impromptu(msgString, args);
  107. };
  108. /**
  109. * Closes currently opened dialog.
  110. */
  111. my.closeDialog = function () {
  112. $.prompt.close();
  113. };
  114. /**
  115. * Shows a dialog with different states to the user.
  116. *
  117. * @param statesObject object containing all the states of the dialog
  118. */
  119. my.openDialogWithStates = function (statesObject, options) {
  120. return new Impromptu(statesObject, options);
  121. };
  122. /**
  123. * Opens new popup window for given <tt>url</tt> centered over current
  124. * window.
  125. *
  126. * @param url the URL to be displayed in the popup window
  127. * @param w the width of the popup window
  128. * @param h the height of the popup window
  129. * @param onPopupClosed optional callback function called when popup window
  130. * has been closed.
  131. *
  132. * @returns popup window object if opened successfully or undefined
  133. * in case we failed to open it(popup blocked)
  134. */
  135. my.openCenteredPopup = function (url, w, h, onPopupClosed) {
  136. var l = window.screenX + (window.innerWidth / 2) - (w / 2);
  137. var t = window.screenY + (window.innerHeight / 2) - (h / 2);
  138. var popup = window.open(
  139. url, '_blank',
  140. 'top=' + t + ', left=' + l + ', width=' + w + ', height=' + h + '');
  141. if (popup && onPopupClosed) {
  142. var pollTimer = window.setInterval(function () {
  143. if (popup.closed !== false) {
  144. window.clearInterval(pollTimer);
  145. onPopupClosed();
  146. }
  147. }, 200);
  148. }
  149. return popup;
  150. };
  151. /**
  152. * Shows a dialog prompting the user to send an error report.
  153. *
  154. * @param titleString the title of the message
  155. * @param msgString the text of the message
  156. * @param error the error that is being reported
  157. */
  158. my.openReportDialog = function(titleKey, msgKey, error) {
  159. my.openMessageDialog(titleKey, msgKey);
  160. console.log(error);
  161. //FIXME send the error to the server
  162. };
  163. /**
  164. * Shows an error dialog to the user.
  165. * @param title the title of the message
  166. * @param message the text of the messafe
  167. */
  168. my.showError = function(titleKey, msgKey) {
  169. if(!titleKey) {
  170. titleKey = "dialog.oops";
  171. }
  172. if(!msgKey)
  173. {
  174. msgKey = "dialog.defaultError";
  175. }
  176. messageHandler.openMessageDialog(titleKey, msgKey);
  177. };
  178. my.notify = function(displayName, displayNameKey,
  179. cls, messageKey, messageArguments, options) {
  180. var displayNameSpan = '<span class="nickname" ';
  181. if(displayName)
  182. {
  183. displayNameSpan += ">" + displayName;
  184. }
  185. else
  186. {
  187. displayNameSpan += "data-i18n='" + displayNameKey +
  188. "'>" + APP.translation.translateString(displayNameKey);
  189. }
  190. displayNameSpan += "</span>";
  191. toastr.info(
  192. displayNameSpan + '<br>' +
  193. '<span class=' + cls + ' data-i18n="' + messageKey + '"' +
  194. (messageArguments?
  195. " data-i18n-options='" + JSON.stringify(messageArguments) + "'"
  196. : "") + ">" +
  197. APP.translation.translateString(messageKey,
  198. messageArguments) +
  199. '</span>', null, options);
  200. };
  201. return my;
  202. }(messageHandler || {}));
  203. module.exports = messageHandler;