forked from CodeSeven/toastr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toastr.js
206 lines (173 loc) · 7.6 KB
/
toastr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// By: Hans Fj�llemark and John Papa
// https://github.com/CodeSeven/toastr
//
// Modified to support css styling instead of inline styling
// Inspired by https://github.com/Srirangan/notifer.js/
; (function (define) {
define(['jquery'], function ($) {
var toastr = (function () {
var
defaults = {
tapToDismiss: true,
toastClass: 'toast',
containerId: 'toast-container',
debug: false,
fadeIn: 300,
fadeOut: 1000,
extendedTimeOut: 1000,
iconClasses: {
error: 'toast-error',
info: 'toast-info',
success: 'toast-success',
warning: 'toast-warning'
},
iconClass: 'toast-info',
positionClass: 'toast-top-right',
timeOut: 5000, // Set timeOut to 0 to make it sticky
titleClass: 'toast-title',
messageClass: 'toast-message'
},
error = function (message, title, optionsOverride) {
return notify({
iconClass: getOptions().iconClasses.error,
message: message,
optionsOverride: optionsOverride,
title: title
})
},
getContainer = function (options) {
var $container = $('#' + options.containerId)
if ($container.length)
return $container
$container = $('<div/>')
.attr('id', options.containerId)
.addClass(options.positionClass)
$container.appendTo($('body'))
return $container
},
getOptions = function () {
return $.extend({}, defaults, toastr.options)
},
info = function (message, title, optionsOverride) {
return notify({
iconClass: getOptions().iconClasses.info,
message: message,
optionsOverride: optionsOverride,
title: title
})
},
notify = function (map) {
var options = $.extend({}, getOptions())
if (typeof (map.optionsOverride) !== 'undefined') {
options = $.extend({}, getOptions(), map.optionsOverride)
}
var
iconClass = map.iconClass || options.iconClass,
intervalId = null,
$container = getContainer(options),
$toastElement = $('<div/>'),
$titleElement = $('<div/>'),
$messageElement = $('<div/>'),
response = { options: options, map: map }
if (map.iconClass) {
$toastElement.addClass(options.toastClass).addClass(iconClass)
}
if (map.title) {
$titleElement.append(map.title).addClass(options.titleClass)
$toastElement.append($titleElement)
}
if (map.message) {
$messageElement.append(map.message).addClass(options.messageClass)
$toastElement.append($messageElement)
}
var fadeAway = function () {
if ($(':focus', $toastElement).length > 0)
return
var fade = function (callback) {
return $toastElement.fadeOut(options.fadeOut, callback)
}
var removeToast = function () {
if ($toastElement.is(':visible')) {
return
}
$toastElement.remove()
if ($container.children().length === 0)
$container.remove()
}
fade(removeToast)
}
var delayedFadeAway = function () {
if (options.timeOut > 0 || options.extendedTimeOut > 0) {
intervalId = setTimeout(fadeAway, options.extendedTimeOut)
}
}
var stickAround = function () {
clearTimeout(intervalId)
$toastElement.stop(true, true)
.fadeIn(options.fadeIn)
}
$toastElement.hide()
$container.prepend($toastElement)
$toastElement.fadeIn(options.fadeIn)
if (options.timeOut > 0) {
intervalId = setTimeout(fadeAway, options.timeOut)
}
$toastElement.hover(stickAround, delayedFadeAway)
if (!options.onclick && options.tapToDismiss) {
$toastElement.click(fadeAway)
}
if (options.onclick) {
$toastElement.click(function () {
options.onclick() && fadeAway()
})
}
if (options.debug) {
console.log(response)
}
return $toastElement
},
success = function (message, title, optionsOverride) {
return notify({
iconClass: getOptions().iconClasses.success,
message: message,
optionsOverride: optionsOverride,
title: title
})
},
warning = function (message, title, optionsOverride) {
return notify({
iconClass: getOptions().iconClasses.warning,
message: message,
optionsOverride: optionsOverride,
title: title
})
},
clear = function () {
var options = getOptions()
var $container = $('#' + options.containerId)
if ($container.length) {
$container.fadeOut(options.fadeOut, function () {
$container.remove()
})
}
}
return {
clear: clear,
error: error,
info: info,
options: {},
success: success,
version: '1.1.1',
warning: warning
}
})()
return toastr
})
}(typeof define === 'function' && define.amd ? define : function (deps, factory) {
if (typeof module !== 'undefined' && module.exports) { //Node
module.exports = factory(require(deps[0]));
} else {
window['toastr'] = factory(window['jQuery']);
}
}));
//} (window, jQuery));