PageRenderTime 62ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/public/assets/theme/assets/global/plugins/bootstrap-toastr/toastr.js

https://gitlab.com/techniconline/kmc
JavaScript | 365 lines | 303 code | 47 blank | 15 comment | 50 complexity | 8f88ffb07642e5b61d63fefc1557ebb6 MD5 | raw file
  1. /*
  2. * Toastr
  3. * Copyright 2012-2014 John Papa and Hans Fjällemark.
  4. * All Rights Reserved.
  5. * Use, reproduction, distribution, and modification of this code is subject to the terms and
  6. * conditions of the MIT license, available at http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * Author: John Papa and Hans Fjällemark
  9. * ARIA Support: Greta Krafsig
  10. * Project: https://github.com/CodeSeven/toastr
  11. */
  12. ;(function (define) {
  13. define(['jquery'], function ($) {
  14. return (function () {
  15. var $container;
  16. var listener;
  17. var toastId = 0;
  18. var toastType = {
  19. error: 'error',
  20. info: 'info',
  21. success: 'success',
  22. warning: 'warning'
  23. };
  24. var toastr = {
  25. clear: clear,
  26. remove: remove,
  27. error: error,
  28. getContainer: getContainer,
  29. info: info,
  30. options: {},
  31. subscribe: subscribe,
  32. success: success,
  33. version: '2.1.0',
  34. warning: warning
  35. };
  36. var previousToast;
  37. return toastr;
  38. //#region Accessible Methods
  39. function error(message, title, optionsOverride) {
  40. return notify({
  41. type: toastType.error,
  42. iconClass: getOptions().iconClasses.error,
  43. message: message,
  44. optionsOverride: optionsOverride,
  45. title: title
  46. });
  47. }
  48. function getContainer(options, create) {
  49. if (!options) {
  50. options = getOptions();
  51. }
  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) {
  92. var options = getOptions();
  93. if (!$container) {
  94. getContainer(options);
  95. }
  96. if (!clearToast($toastElement, options)) {
  97. clearContainer(options);
  98. }
  99. }
  100. function remove($toastElement) {
  101. var options = getOptions();
  102. if (!$container) {
  103. getContainer(options);
  104. }
  105. if ($toastElement && $(':focus', $toastElement).length === 0) {
  106. removeToast($toastElement);
  107. return;
  108. }
  109. if ($container.children().length) {
  110. $container.remove();
  111. }
  112. }
  113. //#endregion
  114. //#region Internal Methods
  115. function clearContainer(options) {
  116. var toastsToClear = $container.children();
  117. for (var i = toastsToClear.length - 1; i >= 0; i--) {
  118. clearToast($(toastsToClear[i]), options);
  119. }
  120. ;
  121. }
  122. function clearToast($toastElement, options) {
  123. if ($toastElement && $(':focus', $toastElement).length === 0) {
  124. $toastElement[options.hideMethod]({
  125. duration: options.hideDuration,
  126. easing: options.hideEasing,
  127. complete: function () {
  128. removeToast($toastElement);
  129. }
  130. });
  131. return true;
  132. }
  133. return false;
  134. }
  135. function createContainer(options) {
  136. $container = $('<div/>')
  137. .attr('id', options.containerId)
  138. .addClass(options.positionClass)
  139. .attr('aria-live', 'polite')
  140. .attr('role', 'alert');
  141. $container.appendTo($(options.target));
  142. return $container;
  143. }
  144. function getDefaults() {
  145. return {
  146. tapToDismiss: true,
  147. toastClass: 'toast',
  148. containerId: 'toast-container',
  149. debug: false,
  150. showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
  151. showDuration: 300,
  152. showEasing: 'swing', //swing and linear are built into jQuery
  153. onShown: undefined,
  154. hideMethod: 'fadeOut',
  155. hideDuration: 1000,
  156. hideEasing: 'swing',
  157. onHidden: undefined,
  158. extendedTimeOut: 1000,
  159. iconClasses: {
  160. error: 'toast-error',
  161. info: 'toast-info',
  162. success: 'toast-success',
  163. warning: 'toast-warning'
  164. },
  165. iconClass: 'toast-info',
  166. positionClass: 'toast-top-right',
  167. timeOut: 5000, // Set timeOut and extendedTimeOut to 0 to make it sticky
  168. titleClass: 'toast-title',
  169. messageClass: 'toast-message',
  170. target: 'body',
  171. closeHtml: '<button>&times;</button>',
  172. newestOnTop: true,
  173. preventDuplicates: false
  174. };
  175. }
  176. function publish(args) {
  177. if (!listener) {
  178. return;
  179. }
  180. listener(args);
  181. }
  182. function notify(map) {
  183. var options = getOptions(),
  184. iconClass = map.iconClass || options.iconClass;
  185. if (options.preventDuplicates) {
  186. if (map.message === previousToast) {
  187. return;
  188. }
  189. else {
  190. previousToast = map.message;
  191. }
  192. }
  193. if (typeof (map.optionsOverride) !== 'undefined') {
  194. options = $.extend(options, map.optionsOverride);
  195. iconClass = map.optionsOverride.iconClass || iconClass;
  196. }
  197. toastId++;
  198. $container = getContainer(options, true);
  199. var intervalId = null,
  200. $toastElement = $('<div/>'),
  201. $titleElement = $('<div/>'),
  202. $messageElement = $('<div/>'),
  203. $closeElement = $(options.closeHtml),
  204. response = {
  205. toastId: toastId,
  206. state: 'visible',
  207. startTime: new Date(),
  208. options: options,
  209. map: map
  210. };
  211. if (map.iconClass) {
  212. $toastElement.addClass(options.toastClass).addClass(iconClass);
  213. }
  214. if (map.title) {
  215. $titleElement.append(map.title).addClass(options.titleClass);
  216. $toastElement.append($titleElement);
  217. }
  218. if (map.message) {
  219. $messageElement.append(map.message).addClass(options.messageClass);
  220. $toastElement.append($messageElement);
  221. }
  222. if (options.closeButton) {
  223. $closeElement.addClass('toast-close-button').attr("role", "button");
  224. $toastElement.prepend($closeElement);
  225. }
  226. $toastElement.hide();
  227. if (options.newestOnTop) {
  228. $container.prepend($toastElement);
  229. } else {
  230. $container.append($toastElement);
  231. }
  232. $toastElement[options.showMethod](
  233. {duration: options.showDuration, easing: options.showEasing, complete: options.onShown}
  234. );
  235. if (options.timeOut > 0) {
  236. intervalId = setTimeout(hideToast, options.timeOut);
  237. }
  238. $toastElement.hover(stickAround, delayedHideToast);
  239. if (!options.onclick && options.tapToDismiss) {
  240. $toastElement.click(hideToast);
  241. }
  242. if (options.closeButton && $closeElement) {
  243. $closeElement.click(function (event) {
  244. if (event.stopPropagation) {
  245. event.stopPropagation();
  246. } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
  247. event.cancelBubble = true;
  248. }
  249. hideToast(true);
  250. });
  251. }
  252. if (options.onclick) {
  253. $toastElement.click(function () {
  254. options.onclick();
  255. hideToast();
  256. });
  257. }
  258. publish(response);
  259. if (options.debug && console) {
  260. console.log(response);
  261. }
  262. return $toastElement;
  263. function hideToast(override) {
  264. if ($(':focus', $toastElement).length && !override) {
  265. return;
  266. }
  267. return $toastElement[options.hideMethod]({
  268. duration: options.hideDuration,
  269. easing: options.hideEasing,
  270. complete: function () {
  271. removeToast($toastElement);
  272. if (options.onHidden && response.state !== 'hidden') {
  273. options.onHidden();
  274. }
  275. response.state = 'hidden';
  276. response.endTime = new Date();
  277. publish(response);
  278. }
  279. });
  280. }
  281. function delayedHideToast() {
  282. if (options.timeOut > 0 || options.extendedTimeOut > 0) {
  283. intervalId = setTimeout(hideToast, options.extendedTimeOut);
  284. }
  285. }
  286. function stickAround() {
  287. clearTimeout(intervalId);
  288. $toastElement.stop(true, true)[options.showMethod](
  289. {duration: options.showDuration, easing: options.showEasing}
  290. );
  291. }
  292. }
  293. function getOptions() {
  294. return $.extend({}, getDefaults(), toastr.options);
  295. }
  296. function removeToast($toastElement) {
  297. if (!$container) {
  298. $container = getContainer();
  299. }
  300. if ($toastElement.is(':visible')) {
  301. return;
  302. }
  303. $toastElement.remove();
  304. $toastElement = null;
  305. if ($container.children().length === 0) {
  306. $container.remove();
  307. }
  308. }
  309. //#endregion
  310. })();
  311. });
  312. }(typeof define === 'function' && define.amd ? define : function (deps, factory) {
  313. if (typeof module !== 'undefined' && module.exports) { //Node
  314. module.exports = factory(require('jquery'));
  315. } else {
  316. window['toastr'] = factory(window['jQuery']);
  317. }
  318. }));