PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs//2.0.1/js/toastr.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 306 lines | 256 code | 35 blank | 15 comment | 36 complexity | 98055b3333f74a68a846a05567e9b0b9 MD5 | raw file
  1. /*
  2. * Toastr
  3. * Version 2.0.1
  4. * Copyright 2012 John Papa and Hans Fjällemark.
  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. * Author: John Papa and Hans Fjällemark
  10. * Project: https://github.com/CodeSeven/toastr
  11. */
  12. ; (function (define) {
  13. define(['jquery'], function ($) {
  14. return (function () {
  15. var version = '2.0.1';
  16. var $container;
  17. var listener;
  18. var toastId = 0;
  19. var toastType = {
  20. error: 'error',
  21. info: 'info',
  22. success: 'success',
  23. warning: 'warning'
  24. };
  25. var toastr = {
  26. clear: clear,
  27. error: error,
  28. getContainer: getContainer,
  29. info: info,
  30. options: {},
  31. subscribe: subscribe,
  32. success: success,
  33. version: version,
  34. warning: warning
  35. };
  36. return toastr;
  37. //#region Accessible Methods
  38. function error(message, title, optionsOverride) {
  39. return notify({
  40. type: toastType.error,
  41. iconClass: getOptions().iconClasses.error,
  42. message: message,
  43. optionsOverride: optionsOverride,
  44. title: title
  45. });
  46. }
  47. function info(message, title, optionsOverride) {
  48. return notify({
  49. type: toastType.info,
  50. iconClass: getOptions().iconClasses.info,
  51. message: message,
  52. optionsOverride: optionsOverride,
  53. title: title
  54. });
  55. }
  56. function subscribe(callback) {
  57. listener = callback;
  58. }
  59. function success(message, title, optionsOverride) {
  60. return notify({
  61. type: toastType.success,
  62. iconClass: getOptions().iconClasses.success,
  63. message: message,
  64. optionsOverride: optionsOverride,
  65. title: title
  66. });
  67. }
  68. function warning(message, title, optionsOverride) {
  69. return notify({
  70. type: toastType.warning,
  71. iconClass: getOptions().iconClasses.warning,
  72. message: message,
  73. optionsOverride: optionsOverride,
  74. title: title
  75. });
  76. }
  77. function clear($toastElement) {
  78. var options = getOptions();
  79. if (!$container) { getContainer(options); }
  80. if ($toastElement && $(':focus', $toastElement).length === 0) {
  81. $toastElement[options.hideMethod]({
  82. duration: options.hideDuration,
  83. easing: options.hideEasing,
  84. complete: function () { removeToast($toastElement); }
  85. });
  86. return;
  87. }
  88. if ($container.children().length) {
  89. $container[options.hideMethod]({
  90. duration: options.hideDuration,
  91. easing: options.hideEasing,
  92. complete: function () { $container.remove(); }
  93. });
  94. }
  95. }
  96. //#endregion
  97. //#region Internal Methods
  98. function getDefaults() {
  99. return {
  100. tapToDismiss: true,
  101. toastClass: 'toast',
  102. containerId: 'toast-container',
  103. debug: false,
  104. showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
  105. showDuration: 300,
  106. showEasing: 'swing', //swing and linear are built into jQuery
  107. onShown: undefined,
  108. hideMethod: 'fadeOut',
  109. hideDuration: 1000,
  110. hideEasing: 'swing',
  111. onHidden: undefined,
  112. extendedTimeOut: 1000,
  113. iconClasses: {
  114. error: 'toast-error',
  115. info: 'toast-info',
  116. success: 'toast-success',
  117. warning: 'toast-warning'
  118. },
  119. iconClass: 'toast-info',
  120. positionClass: 'toast-top-right',
  121. timeOut: 5000, // Set timeOut and extendedTimeout to 0 to make it sticky
  122. titleClass: 'toast-title',
  123. messageClass: 'toast-message',
  124. target: 'body',
  125. closeHtml: '<button>&times;</button>',
  126. newestOnTop: true
  127. };
  128. }
  129. function publish(args) {
  130. if (!listener) {
  131. return;
  132. }
  133. listener(args);
  134. }
  135. function notify(map) {
  136. var
  137. options = getOptions(),
  138. iconClass = map.iconClass || options.iconClass;
  139. if (typeof (map.optionsOverride) !== 'undefined') {
  140. options = $.extend(options, map.optionsOverride);
  141. iconClass = map.optionsOverride.iconClass || iconClass;
  142. }
  143. toastId++;
  144. $container = getContainer(options);
  145. var
  146. intervalId = null,
  147. $toastElement = $('<div/>'),
  148. $titleElement = $('<div/>'),
  149. $messageElement = $('<div/>'),
  150. $closeElement = $(options.closeHtml),
  151. response = {
  152. toastId: toastId,
  153. state: 'visible',
  154. startTime: new Date(),
  155. options: options,
  156. map: map
  157. };
  158. if (map.iconClass) {
  159. $toastElement.addClass(options.toastClass).addClass(iconClass);
  160. }
  161. if (map.title) {
  162. $titleElement.append(map.title).addClass(options.titleClass);
  163. $toastElement.append($titleElement);
  164. }
  165. if (map.message) {
  166. $messageElement.append(map.message).addClass(options.messageClass);
  167. $toastElement.append($messageElement);
  168. }
  169. if (options.closeButton) {
  170. $closeElement.addClass('toast-close-button');
  171. $toastElement.prepend($closeElement);
  172. }
  173. $toastElement.hide();
  174. if (options.newestOnTop) {
  175. $container.prepend($toastElement);
  176. } else {
  177. $container.append($toastElement);
  178. }
  179. $toastElement[options.showMethod](
  180. { duration: options.showDuration, easing: options.showEasing, complete: options.onShown }
  181. );
  182. if (options.timeOut > 0) {
  183. intervalId = setTimeout(hideToast, options.timeOut);
  184. }
  185. $toastElement.hover(stickAround, delayedhideToast);
  186. if (!options.onclick && options.tapToDismiss) {
  187. $toastElement.click(hideToast);
  188. }
  189. if (options.closeButton && $closeElement) {
  190. $closeElement.click(function (event) {
  191. event.stopPropagation();
  192. hideToast(true);
  193. });
  194. }
  195. if (options.onclick) {
  196. $toastElement.click(function () {
  197. options.onclick();
  198. hideToast();
  199. });
  200. }
  201. publish(response);
  202. if (options.debug && console) {
  203. console.log(response);
  204. }
  205. return $toastElement;
  206. function hideToast(override) {
  207. if ($(':focus', $toastElement).length && !override) {
  208. return;
  209. }
  210. return $toastElement[options.hideMethod]({
  211. duration: options.hideDuration,
  212. easing: options.hideEasing,
  213. complete: function () {
  214. removeToast($toastElement);
  215. if (options.onHidden) {
  216. options.onHidden();
  217. }
  218. response.state = 'hidden';
  219. response.endTime = new Date(),
  220. publish(response);
  221. }
  222. });
  223. }
  224. function delayedhideToast() {
  225. if (options.timeOut > 0 || options.extendedTimeOut > 0) {
  226. intervalId = setTimeout(hideToast, options.extendedTimeOut);
  227. }
  228. }
  229. function stickAround() {
  230. clearTimeout(intervalId);
  231. $toastElement.stop(true, true)[options.showMethod](
  232. { duration: options.showDuration, easing: options.showEasing }
  233. );
  234. }
  235. }
  236. function getContainer(options) {
  237. if (!options) { options = getOptions(); }
  238. $container = $('#' + options.containerId);
  239. if ($container.length) {
  240. return $container;
  241. }
  242. $container = $('<div/>')
  243. .attr('id', options.containerId)
  244. .addClass(options.positionClass);
  245. $container.appendTo($(options.target));
  246. return $container;
  247. }
  248. function getOptions() {
  249. return $.extend({}, getDefaults(), toastr.options);
  250. }
  251. function removeToast($toastElement) {
  252. if (!$container) { $container = getContainer(); }
  253. if ($toastElement.is(':visible')) {
  254. return;
  255. }
  256. $toastElement.remove();
  257. $toastElement = null;
  258. if ($container.children().length === 0) {
  259. $container.remove();
  260. }
  261. }
  262. //#endregion
  263. })();
  264. });
  265. }(typeof define === 'function' && define.amd ? define : function (deps, factory) {
  266. if (typeof module !== 'undefined' && module.exports) { //Node
  267. module.exports = factory(require(deps[0]));
  268. } else {
  269. window['toastr'] = factory(window['jQuery']);
  270. }
  271. }));