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.

415 lines
15 KiB

  1. /*
  2. * Toastr
  3. * Copyright 2012-2015
  4. * Authors: John Papa, Hans Fjällemark, and Tim Ferrell.
  5. * All Rights Reserved.
  6. * Use, reproduction, distribution, and modification of this code is subject to the terms and
  7. * conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
  8. *
  9. * ARIA Support: Greta Krafsig
  10. *
  11. * Project: https://github.com/CodeSeven/toastr
  12. */
  13. /* global define */
  14. ; (function (define) {
  15. define(['jquery'], function ($) {
  16. return (function () {
  17. var $container;
  18. var listener;
  19. var toastId = 0;
  20. var toastType = {
  21. error: 'error',
  22. info: 'info',
  23. success: 'success',
  24. warning: 'warning'
  25. };
  26. var toastr = {
  27. clear: clear,
  28. remove: remove,
  29. error: error,
  30. getContainer: getContainer,
  31. info: info,
  32. options: {},
  33. subscribe: subscribe,
  34. success: success,
  35. version: '2.1.1',
  36. warning: warning
  37. };
  38. var previousToast;
  39. return toastr;
  40. ////////////////
  41. function error(message, title, optionsOverride) {
  42. return notify({
  43. type: toastType.error,
  44. iconClass: getOptions().iconClasses.error,
  45. message: message,
  46. optionsOverride: optionsOverride,
  47. title: title
  48. });
  49. }
  50. function getContainer(options, create) {
  51. if (!options) { options = getOptions(); }
  52. $container = $('#' + options.containerId);
  53. if ($container.length) {
  54. return $container;
  55. }
  56. if (create) {
  57. $container = createContainer(options);
  58. }
  59. return $container;
  60. }
  61. function info(message, title, optionsOverride) {
  62. return notify({
  63. type: toastType.info,
  64. iconClass: getOptions().iconClasses.info,
  65. message: message,
  66. optionsOverride: optionsOverride,
  67. title: title
  68. });
  69. }
  70. function subscribe(callback) {
  71. listener = callback;
  72. }
  73. function success(message, title, optionsOverride) {
  74. return notify({
  75. type: toastType.success,
  76. iconClass: getOptions().iconClasses.success,
  77. message: message,
  78. optionsOverride: optionsOverride,
  79. title: title
  80. });
  81. }
  82. function warning(message, title, optionsOverride) {
  83. return notify({
  84. type: toastType.warning,
  85. iconClass: getOptions().iconClasses.warning,
  86. message: message,
  87. optionsOverride: optionsOverride,
  88. title: title
  89. });
  90. }
  91. function clear($toastElement, clearOptions) {
  92. var options = getOptions();
  93. if (!$container) { getContainer(options); }
  94. if (!clearToast($toastElement, options, clearOptions)) {
  95. clearContainer(options);
  96. }
  97. }
  98. function remove($toastElement) {
  99. var options = getOptions();
  100. if (!$container) { getContainer(options); }
  101. if ($toastElement && $(':focus', $toastElement).length === 0) {
  102. removeToast($toastElement);
  103. return;
  104. }
  105. if ($container.children().length) {
  106. $container.remove();
  107. }
  108. }
  109. // internal functions
  110. function clearContainer (options) {
  111. var toastsToClear = $container.children();
  112. for (var i = toastsToClear.length - 1; i >= 0; i--) {
  113. clearToast($(toastsToClear[i]), options);
  114. }
  115. }
  116. function clearToast ($toastElement, options, clearOptions) {
  117. var force = clearOptions && clearOptions.force ? clearOptions.force : false;
  118. if ($toastElement && (force || $(':focus', $toastElement).length === 0)) {
  119. $toastElement[options.hideMethod]({
  120. duration: options.hideDuration,
  121. easing: options.hideEasing,
  122. complete: function () { removeToast($toastElement); }
  123. });
  124. return true;
  125. }
  126. return false;
  127. }
  128. function createContainer(options) {
  129. $container = $('<div/>')
  130. .attr('id', options.containerId)
  131. .addClass(options.positionClass)
  132. .attr('aria-live', 'polite')
  133. .attr('role', 'alert');
  134. $container.appendTo($(options.target));
  135. return $container;
  136. }
  137. function getDefaults() {
  138. return {
  139. tapToDismiss: true,
  140. toastClass: 'toast',
  141. containerId: 'toast-container',
  142. debug: false,
  143. showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
  144. showDuration: 300,
  145. showEasing: 'swing', //swing and linear are built into jQuery
  146. onShown: undefined,
  147. hideMethod: 'fadeOut',
  148. hideDuration: 1000,
  149. hideEasing: 'swing',
  150. onHidden: undefined,
  151. extendedTimeOut: 1000,
  152. iconClasses: {
  153. error: 'toast-error',
  154. info: 'toast-info',
  155. success: 'toast-success',
  156. warning: 'toast-warning'
  157. },
  158. iconClass: 'toast-info',
  159. positionClass: 'toast-bottom-right',
  160. timeOut: 7000, // Set timeOut and extendedTimeOut to 0 to make it sticky
  161. titleClass: 'toast-title',
  162. messageClass: 'toast-message',
  163. target: 'body',
  164. closeHtml: '<button type="button">&times;</button>',
  165. newestOnTop: false,
  166. preventDuplicates: false,
  167. progressBar: true
  168. };
  169. }
  170. function publish(args) {
  171. if (!listener) { return; }
  172. listener(args);
  173. }
  174. function notify(map) {
  175. var options = getOptions();
  176. var iconClass = map.iconClass || options.iconClass;
  177. if (typeof (map.optionsOverride) !== 'undefined') {
  178. options = $.extend(options, map.optionsOverride);
  179. iconClass = map.optionsOverride.iconClass || iconClass;
  180. }
  181. if (shouldExit(options, map)) { return; }
  182. toastId++;
  183. $container = getContainer(options, true);
  184. var intervalId = null;
  185. var $toastElement = $('<div/>');
  186. var $titleElement = $('<div/>');
  187. var $messageElement = $('<div/>');
  188. var $progressElement = $('<div/>');
  189. var $closeElement = $(options.closeHtml);
  190. var progressBar = {
  191. intervalId: null,
  192. hideEta: null,
  193. maxHideTime: null
  194. };
  195. var response = {
  196. toastId: toastId,
  197. state: 'visible',
  198. startTime: new Date(),
  199. options: options,
  200. map: map
  201. };
  202. personalizeToast();
  203. displayToast();
  204. handleEvents();
  205. publish(response);
  206. if (options.debug && console) {
  207. console.log(response);
  208. }
  209. return $toastElement;
  210. function personalizeToast() {
  211. setIcon();
  212. setTitle();
  213. setMessage();
  214. setCloseButton();
  215. setProgressBar();
  216. setSequence();
  217. }
  218. function handleEvents() {
  219. $toastElement.hover(stickAround, delayedHideToast);
  220. if (!options.onclick && options.tapToDismiss) {
  221. $toastElement.click(hideToast);
  222. }
  223. if (options.closeButton && $closeElement) {
  224. $closeElement.click(function (event) {
  225. if (event.stopPropagation) {
  226. event.stopPropagation();
  227. } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
  228. event.cancelBubble = true;
  229. }
  230. hideToast(true);
  231. });
  232. }
  233. if (options.onclick) {
  234. $toastElement.click(function () {
  235. options.onclick();
  236. hideToast();
  237. });
  238. }
  239. }
  240. function displayToast() {
  241. $toastElement.hide();
  242. $toastElement[options.showMethod](
  243. {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
  244. );
  245. if (options.timeOut > 0) {
  246. intervalId = setTimeout(hideToast, options.timeOut);
  247. progressBar.maxHideTime = parseFloat(options.timeOut);
  248. progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
  249. if (options.progressBar) {
  250. progressBar.intervalId = setInterval(updateProgress, 10);
  251. }
  252. }
  253. }
  254. function setIcon() {
  255. if (map.iconClass) {
  256. $toastElement.addClass(options.toastClass).addClass(iconClass);
  257. }
  258. }
  259. function setSequence() {
  260. if (options.newestOnTop) {
  261. $container.prepend($toastElement);
  262. } else {
  263. $container.append($toastElement);
  264. }
  265. }
  266. function setTitle() {
  267. if (map.title) {
  268. $titleElement.append(map.title).addClass(options.titleClass);
  269. $toastElement.append($titleElement);
  270. }
  271. }
  272. function setMessage() {
  273. if (map.message) {
  274. $messageElement.append(map.message).addClass(options.messageClass);
  275. $toastElement.append($messageElement);
  276. }
  277. }
  278. function setCloseButton() {
  279. if (options.closeButton) {
  280. $closeElement.addClass('toast-close-button').attr('role', 'button');
  281. $toastElement.prepend($closeElement);
  282. }
  283. }
  284. function setProgressBar() {
  285. if (options.progressBar) {
  286. $progressElement.addClass('toast-progress');
  287. $toastElement.prepend($progressElement);
  288. }
  289. }
  290. function shouldExit(options, map) {
  291. if (options.preventDuplicates) {
  292. if (map.message === previousToast) {
  293. return true;
  294. } else {
  295. previousToast = map.message;
  296. }
  297. }
  298. return false;
  299. }
  300. function hideToast(override) {
  301. if ($(':focus', $toastElement).length && !override) {
  302. return;
  303. }
  304. clearTimeout(progressBar.intervalId);
  305. return $toastElement[options.hideMethod]({
  306. duration: options.hideDuration,
  307. easing: options.hideEasing,
  308. complete: function () {
  309. removeToast($toastElement);
  310. if (options.onHidden && response.state !== 'hidden') {
  311. options.onHidden();
  312. }
  313. response.state = 'hidden';
  314. response.endTime = new Date();
  315. publish(response);
  316. }
  317. });
  318. }
  319. function delayedHideToast() {
  320. if (options.timeOut > 0 || options.extendedTimeOut > 0) {
  321. intervalId = setTimeout(hideToast, options.extendedTimeOut);
  322. progressBar.maxHideTime = parseFloat(options.extendedTimeOut);
  323. progressBar.hideEta = new Date().getTime() + progressBar.maxHideTime;
  324. }
  325. }
  326. function stickAround() {
  327. clearTimeout(intervalId);
  328. progressBar.hideEta = 0;
  329. $toastElement.stop(true, true)[options.showMethod](
  330. {duration: options.showDuration, easing: options.showEasing}
  331. );
  332. }
  333. function updateProgress() {
  334. var percentage = ((progressBar.hideEta - (new Date().getTime())) / progressBar.maxHideTime) * 100;
  335. $progressElement.width(percentage + '%');
  336. }
  337. }
  338. function getOptions() {
  339. return $.extend({}, getDefaults(), toastr.options);
  340. }
  341. function removeToast($toastElement) {
  342. if (!$container) { $container = getContainer(); }
  343. if ($toastElement.is(':visible')) {
  344. return;
  345. }
  346. $toastElement.remove();
  347. $toastElement = null;
  348. if ($container.children().length === 0) {
  349. $container.remove();
  350. previousToast = undefined;
  351. }
  352. }
  353. })();
  354. });
  355. }(typeof define === 'function' && define.amd ? define : function (deps, factory) {
  356. if (typeof module !== 'undefined' && module.exports) { //Node
  357. module.exports = factory(require('jquery'));
  358. } else {
  359. window['toastr'] = factory(window['jQuery']);
  360. }
  361. }));