Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 /*
A 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
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             };
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 info(message, title, optionsOverride) {
52                 return notify({
53                     type: toastType.info,
54                     iconClass: getOptions().iconClasses.info,
55                     message: message,
56                     optionsOverride: optionsOverride,
57                     title: title
58                 });
59             }
60
61             function subscribe(callback) {
62                 listener = callback;
63             }
64
65             function success(message, title, optionsOverride) {
66                 return notify({
67                     type: toastType.success,
68                     iconClass: getOptions().iconClasses.success,
69                     message: message,
70                     optionsOverride: optionsOverride,
71                     title: title
72                 });
73             }
74
75             function warning(message, title, optionsOverride) {
76                 return notify({
77                     type: toastType.warning,
78                     iconClass: getOptions().iconClasses.warning,
79                     message: message,
80                     optionsOverride: optionsOverride,
81                     title: title
82                 });
83             }
84
85             function clear($toastElement) {
86                 var options = getOptions();
87                 if (!$container) { getContainer(options); }
88                 if ($toastElement && $(':focus', $toastElement).length === 0) {
89                     $toastElement[options.hideMethod]({
90                         duration: options.hideDuration,
91                         easing: options.hideEasing,
92                         complete: function () { removeToast($toastElement); }
93                     });
94                     return;
95                 }
96                 if ($container.children().length) {
97                     $container[options.hideMethod]({
98                         duration: options.hideDuration,
99                         easing: options.hideEasing,
100                         complete: function () { $container.remove(); }
101                     });
102                 }
103             }
104             //#endregion
105
106             //#region Internal Methods
107
108             function getDefaults() {
109                 return {
110                     tapToDismiss: true,
111                     toastClass: 'toast',
112                     containerId: 'toast-container',
113                     debug: false,
114
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
124                     extendedTimeOut: 1000,
125                     iconClasses: {
126                         error: 'toast-error',
127                         info: 'toast-info',
128                         success: 'toast-success',
129                         warning: 'toast-warning'
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
142             function publish(args) {
143                 if (!listener) {
144                     return;
145                 }
146                 listener(args);
147             }
148
149             function notify(map) {
150                 var
151                     options = getOptions(),
152                     iconClass = map.iconClass || options.iconClass;
153
154                 if (typeof (map.optionsOverride) !== 'undefined') {
155                     options = $.extend(options, map.optionsOverride);
156                     iconClass = map.optionsOverride.iconClass || iconClass;
157                 }
158
159                 toastId++;
160
161                 $container = getContainer(options);
162                 var
163                     intervalId = null,
164                     $toastElement = $('<div/>'),
165                     $titleElement = $('<div/>'),
166                     $messageElement = $('<div/>'),
167                     $closeElement = $(options.closeHtml),
168                     response = {
169                         toastId: toastId,
170                         state: 'visible',
171                         startTime: new Date(),
172                         options: options,
173                         map: map
174                     };
175
176                 if (map.iconClass) {
177                     $toastElement.addClass(options.toastClass).addClass(iconClass);
178                 }
179
180                 if (map.title) {
181                     $titleElement.append(map.title).addClass(options.titleClass);
182                     $toastElement.append($titleElement);
183                 }
184
185                 if (map.message) {
186                     $messageElement.append(map.message).addClass(options.messageClass);
187                     $toastElement.append($messageElement);
188                 }
189
190                 if (options.closeButton) {
191                     $closeElement.addClass('toast-close-button');
192                     $toastElement.prepend($closeElement);
193                 }
194
195                 $toastElement.hide();
196                 if (options.newestOnTop) {
197                     $container.prepend($toastElement);
198                 } else {
199                     $container.append($toastElement);
200                 }
201
202
203                 $toastElement[options.showMethod](
204                     { duration: options.showDuration, easing: options.showEasing, complete: options.onShown }
205                 );
206                 if (options.timeOut > 0) {
207                     intervalId = setTimeout(hideToast, options.timeOut);
208                 }
209
210                 $toastElement.hover(stickAround, delayedhideToast);
211                 if (!options.onclick && options.tapToDismiss) {
212                     $toastElement.click(hideToast);
213                 }
214                 if (options.closeButton && $closeElement) {
215                     $closeElement.click(function (event) {
216                         event.stopPropagation();
217                         hideToast(true);
218                     });
219                 }
220
221                 if (options.onclick) {
222                     $toastElement.click(function () {
223                         options.onclick();
224                         hideToast();
225                     });
226                 }
227
228                 publish(response);
229
230                 if (options.debug && console) {
231                     console.log(response);
232                 }
233
234                 return $toastElement;
235
236                 function hideToast(override) {
237                     if ($(':focus', $toastElement).length && !override) {
238                         return;
239                     }
240                     return $toastElement[options.hideMethod]({
241                         duration: options.hideDuration,
242                         easing: options.hideEasing,
243                         complete: function () {
244                             removeToast($toastElement);
245                             if (options.onHidden) {
246                                 options.onHidden();
247                             }
248                             response.state = 'hidden';
249                             response.endTime = new Date(),
250                             publish(response);
251                         }
252                     });
253                 }
254
255                 function delayedhideToast() {
256                     if (options.timeOut > 0 || options.extendedTimeOut > 0) {
257                         intervalId = setTimeout(hideToast, options.extendedTimeOut);
258                     }
259                 }
260
261                 function stickAround() {
262                     clearTimeout(intervalId);
263                     $toastElement.stop(true, true)[options.showMethod](
264                         { duration: options.showDuration, easing: options.showEasing }
265                     );
266                 }
267             }
268             function getContainer(options) {
269                 if (!options) { options = getOptions(); }
270                 $container = $('#' + options.containerId);
271                 if ($container.length) {
272                     return $container;
273                 }
274                 $container = $('<div/>')
275                     .attr('id', options.containerId)
276                     .addClass(options.positionClass);
277                 $container.appendTo($(options.target));
278                 return $container;
279             }
280
281             function getOptions() {
282                 return $.extend({}, getDefaults(), toastr.options);
283             }
284
285             function removeToast($toastElement) {
286                 if (!$container) { $container = getContainer(); }
287                 if ($toastElement.is(':visible')) {
288                     return;
289                 }
290                 $toastElement.remove();
291                 $toastElement = null;
292                 if ($container.children().length === 0) {
293                     $container.remove();
294                 }
295             }
296             //#endregion
297
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(deps[0]));
303     } else {
304         window['toastr'] = factory(window['jQuery']);
305     }
306 }));