toastr.js

toastr

当前为 2022-06-27 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/447104/1064956/toastrjs.js

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