PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/BeyondAdmin/assets/js/toastr/toastr.js

https://gitlab.com/kaouech/theme
JavaScript | 381 lines | 326 code | 40 blank | 15 comment | 55 complexity | b4296ac1edb3510e77b6ca58216cd08f 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. custom: 'custom'
  25. };
  26. var toastr = {
  27. clear: clear,
  28. error: error,
  29. getContainer: getContainer,
  30. info: info,
  31. options: {},
  32. subscribe: subscribe,
  33. success: success,
  34. version: version,
  35. warning: warning,
  36. custom: custom
  37. };
  38. return toastr;
  39. //#region Accessible Methods
  40. function custom(message, title, optionsOverride) {
  41. return notify({
  42. type: toastType.custom,
  43. iconClass: getOptions().iconClass,
  44. message: message,
  45. optionsOverride: optionsOverride,
  46. title: title
  47. });
  48. }
  49. function error(message, title, optionsOverride) {
  50. return notify({
  51. type: toastType.error,
  52. iconClass: getOptions().iconClasses.error,
  53. message: message,
  54. optionsOverride: optionsOverride,
  55. title: title
  56. });
  57. }
  58. function info(message, title, optionsOverride) {
  59. return notify({
  60. type: toastType.info,
  61. iconClass: getOptions().iconClasses.info,
  62. message: message,
  63. optionsOverride: optionsOverride,
  64. title: title
  65. });
  66. }
  67. function subscribe(callback) {
  68. listener = callback;
  69. }
  70. function success(message, title, optionsOverride) {
  71. return notify({
  72. type: toastType.success,
  73. iconClass: getOptions().iconClasses.success,
  74. message: message,
  75. optionsOverride: optionsOverride,
  76. title: title
  77. });
  78. }
  79. function warning(message, title, optionsOverride) {
  80. return notify({
  81. type: toastType.warning,
  82. iconClass: getOptions().iconClasses.warning,
  83. message: message,
  84. optionsOverride: optionsOverride,
  85. title: title
  86. });
  87. }
  88. function clear($toastElement) {
  89. var options = getOptions();
  90. if (!$container) { getContainer(options); }
  91. if ($toastElement && $(':focus', $toastElement).length === 0) {
  92. $toastElement[options.hideMethod]({
  93. duration: options.hideDuration,
  94. easing: options.hideEasing,
  95. complete: function () { removeToast($toastElement); }
  96. });
  97. return;
  98. }
  99. if ($container.children().length) {
  100. $container[options.hideMethod]({
  101. duration: options.hideDuration,
  102. easing: options.hideEasing,
  103. complete: function () { $container.remove(); }
  104. });
  105. }
  106. }
  107. //#endregion
  108. //#region Internal Methods
  109. function getDefaults() {
  110. return {
  111. tapToDismiss: true,
  112. toastClass: 'toast',
  113. containerId: 'toast-container',
  114. debug: false,
  115. showMethod: 'fadeIn', //fadeIn, slideDown, and show are built into jQuery
  116. showDuration: 300,
  117. showEasing: 'swing', //swing and linear are built into jQuery
  118. onShown: undefined,
  119. hideMethod: 'fadeOut',
  120. hideDuration: 1000,
  121. hideEasing: 'swing',
  122. onHidden: undefined,
  123. extendedTimeOut: 1000,
  124. iconClasses: {
  125. error: 'toast-error',
  126. info: 'toast-info',
  127. success: 'toast-success',
  128. warning: 'toast-warning',
  129. custom: 'toast-custom'
  130. },
  131. iconClass: 'toast-info',
  132. positionClass: 'toast-top-right',
  133. timeOut: 5000, // Set timeOut and extendedTimeout to 0 to make it sticky
  134. titleClass: 'toast-title',
  135. messageClass: 'toast-message',
  136. target: 'body',
  137. closeHtml: '<button>&times;</button>',
  138. newestOnTop: true
  139. };
  140. }
  141. function publish(args) {
  142. if (!listener) {
  143. return;
  144. }
  145. listener(args);
  146. }
  147. function notify(map) {
  148. var
  149. options = getOptions(),
  150. iconClass = map.iconClass || options.iconClass;
  151. if (typeof (map.optionsOverride) !== 'undefined') {
  152. options = $.extend(options, map.optionsOverride);
  153. iconClass = map.optionsOverride.iconClass || iconClass;
  154. }
  155. toastId++;
  156. $container = getContainer(options);
  157. var
  158. intervalId = null,
  159. $toastElement = $('<div/>'),
  160. $titleElement = $('<div/>'),
  161. $messageElement = $('<div/>'),
  162. $closeElement = $(options.closeHtml),
  163. response = {
  164. toastId: toastId,
  165. state: 'visible',
  166. startTime: new Date(),
  167. options: options,
  168. map: map
  169. };
  170. if (map.iconClass) {
  171. $toastElement.addClass(options.toastClass).addClass(iconClass);
  172. }
  173. if (map.title) {
  174. $titleElement.append(map.title).addClass(options.titleClass);
  175. $toastElement.append($titleElement);
  176. }
  177. if (map.message) {
  178. $messageElement.append(map.message).addClass(options.messageClass);
  179. $toastElement.append($messageElement);
  180. }
  181. if (options.closeButton) {
  182. $closeElement.addClass('toast-close-button');
  183. $toastElement.prepend($closeElement);
  184. }
  185. if (options.positionClass) {
  186. if ($container.attr("class") != options.positionClass) {
  187. $container.html("");
  188. $container.removeAttr('class');
  189. $container.addClass(options.positionClass);
  190. }
  191. }
  192. $toastElement.hide();
  193. if (options.newestOnTop) {
  194. $container.prepend($toastElement);
  195. } else {
  196. $container.append($toastElement);
  197. }
  198. $toastElement[options.showMethod](
  199. { duration: options.showDuration, easing: options.showEasing, complete: options.onShown }
  200. );
  201. if (options.timeOut > 0) {
  202. intervalId = setTimeout(hideToast, options.timeOut);
  203. }
  204. $toastElement.hover(stickAround, delayedhideToast);
  205. if (!options.onclick && options.tapToDismiss) {
  206. $toastElement.click(hideToast);
  207. }
  208. if (options.closeButton && $closeElement) {
  209. $closeElement.click(function (event) {
  210. if (event.stopPropagation) {
  211. event.stopPropagation();
  212. } else if (event.cancelBubble !== undefined && event.cancelBubble !== true) {
  213. event.cancelBubble = true;
  214. }
  215. hideToast(true);
  216. });
  217. }
  218. if (options.onclick) {
  219. $toastElement.click(function () {
  220. options.onclick();
  221. hideToast();
  222. });
  223. }
  224. publish(response);
  225. var PlaySound = 0;
  226. PlaySound = 1;
  227. if (isIE8orlower() == 0) {
  228. var audioElement = document.createElement("audio");
  229. audioElement.setAttribute("src", "assets/sound/alert.mp3");
  230. $.get();
  231. audioElement.addEventListener("load", function () {
  232. audioElement.play()
  233. }, true);
  234. audioElement.pause();
  235. audioElement.play()
  236. }
  237. if (options.debug && console) {
  238. console.log(response);
  239. }
  240. return $toastElement;
  241. function hideToast(override) {
  242. if ($(':focus', $toastElement).length && !override) {
  243. return;
  244. }
  245. return $toastElement[options.hideMethod]({
  246. duration: options.hideDuration,
  247. easing: options.hideEasing,
  248. complete: function () {
  249. removeToast($toastElement);
  250. if (options.onHidden) {
  251. options.onHidden();
  252. }
  253. response.state = 'hidden';
  254. response.endTime = new Date(),
  255. publish(response);
  256. }
  257. });
  258. }
  259. function delayedhideToast() {
  260. if (options.timeOut > 0 || options.extendedTimeOut > 0) {
  261. intervalId = setTimeout(hideToast, options.extendedTimeOut);
  262. }
  263. }
  264. function stickAround() {
  265. clearTimeout(intervalId);
  266. $toastElement.stop(true, true)[options.showMethod](
  267. { duration: options.showDuration, easing: options.showEasing }
  268. );
  269. }
  270. }
  271. function getContainer(options) {
  272. if (!options) { options = getOptions(); }
  273. $container = $('#' + options.containerId);
  274. if ($container.length) {
  275. return $container;
  276. }
  277. $container = $('<div/>')
  278. .attr('id', options.containerId)
  279. .addClass(options.positionClass);
  280. $container.appendTo($(options.target));
  281. return $container;
  282. }
  283. function getOptions() {
  284. return $.extend({}, getDefaults(), toastr.options);
  285. }
  286. function removeToast($toastElement) {
  287. if (!$container) { $container = getContainer(); }
  288. if ($toastElement.is(':visible')) {
  289. return;
  290. }
  291. $toastElement.remove();
  292. $toastElement = null;
  293. if ($container.children().length === 0) {
  294. $container.remove();
  295. }
  296. }
  297. //#endregion
  298. })();
  299. });
  300. }(typeof define === 'function' && define.amd ? define : function (deps, factory) {
  301. if (typeof module !== 'undefined' && module.exports) { //Node
  302. module.exports = factory(require('jquery'));
  303. } else {
  304. window['toastr'] = factory(window['jQuery']);
  305. }
  306. }));
  307. function getInternetExplorerVersion() {
  308. var rv = -1;
  309. if (navigator.appName == "Microsoft Internet Explorer") {
  310. var ua = navigator.userAgent;
  311. var re = new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})");
  312. if (re.exec(ua) != null) {
  313. rv = parseFloat(RegExp.$1)
  314. }
  315. }
  316. return rv
  317. }
  318. function checkVersion() {
  319. var msg = "You're not using Windows Internet Explorer.";
  320. var ver = getInternetExplorerVersion();
  321. if (ver > -1) {
  322. if (ver >= 8) {
  323. msg = "You're using a recent copy of Windows Internet Explorer."
  324. } else {
  325. msg = "You should upgrade your copy of Windows Internet Explorer."
  326. }
  327. }
  328. alert(msg)
  329. }
  330. function isIE8orlower() {
  331. var msg = "0";
  332. var ver = getInternetExplorerVersion();
  333. if (ver > -1) {
  334. if (ver >= 9) {
  335. msg = 0
  336. } else {
  337. msg = 1
  338. }
  339. }
  340. return msg
  341. };