Subversion Repositories Integrator Subversion

Rev

Details | Last modification | View Log | RSS feed

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