-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjquery.toaster.js
More file actions
189 lines (152 loc) · 6 KB
/
jquery.toaster.js
File metadata and controls
189 lines (152 loc) · 6 KB
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
(function($) {
// --------------------------------------------------------------------
// Base toaster impl
var defaults = {
toaster: {
position: { top: 0, right: 0 }
},
toast: {
width: 200,
selector: '.toaster',
type: 'info',
text: 'none',
closeText: 'Close',
sticky: false,
duration: 6000,
close: null
}
}
// --------------------------------------------------------------------
// jQuery toaster plugins
function cssTRBL($el, property, values) {
var dirs = ['top','right','bottom','left'],
prefix = property ? property + '-' : '',
props = {};
if (typeof(values) == 'string') {
values = [values, values, values, values];
} else if (values.length == 2) {
values = [values[0], values[1], values[0], values[1]];
}
$.each(dirs, function(i, d) {
if (values[i] !== undefined) {
props[prefix + dirs[i]] = values[i];
}
});
$el.css(props);
}
(function($) {
// ========
// TODO: add events
// ========
$.fn.toaster = toaster;
var methods = {
init: function(options) {
var defaults = toaster.defaults,
opts = $.extend({}, defaults, options),
$this = this.first();
opts.position = opts.position
|| defaults.position
|| { top: 0, right: 0 };
$this.first()
.addClass('toaster')
.css('position', 'absolute')
.appendTo('body');
cssTRBL($this, '', [
opts.position.top,
opts.position.right,
opts.position.bottom,
opts.position.left
]);
cssTRBL($this, 'margin', '1em');
cssTRBL($this, 'padding', '1em');
return this;
},
clear: function() { $(this).data('toaster').clear() }
};
function toaster(method) {
if ((! method) || typeof(method) === 'object') {
return methods.init.apply(this, arguments);
} else if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
$.error('Method ' + method + ' does not exist on jQuery.toaster');
}
};
toaster.methods = methods;
toaster.defaults = defaults.toaster;
}($));
(function() {
// ========
// TODO: add events
// ========
$.fn.toast = toast;
var methods = {
init: function(options) {
var defaults = toast.defaults,
opts = $.extend({}, defaults, options),
sel = opts.selector || defaults.selector,
type = opts.type = opts.type || defaults.type || 'none',
closeTxt = opts.closeText || 'Close Notice',
toaster = $(sel).first(),
data = { opts: opts },
that = this,
$this;
// bail if already initialized
if (this.data('toast')) return;
this.data('toast', data);
// create container if it doesn't exist
if (! toaster.length) {
toaster = $('<div>').toaster({ cls: sel });
}
$this = $('<div>')
.addClass('toast')
.appendTo(toaster)
.append('<a href="#close" title="'+closeTxt+'">X</a>')
.append(this);
data.wrapper = $this;
if (opts.type && opts.type != 'none') {
$this.addClass('toast-' + opts.type);
}
if (opts.width) {
$this.css('width', opts.width);
}
$('>a', $this).click(function(e) {
e.preventDefault();
this.toast('close');
});
if (! opts.sticky) {
setTimeout(function() {
that.toast('close');
}, opts.duration);
}
return this;
},
close: function() {
// console.log('close', this, this.data('toast'));
var data = this.data('toast'),
$this = data.wrapper,
opts = data.opts;
$this.animate({ opacity: '0' }, 600, function() {
$this.animate({ height: '0', margin: 0, padding: 0 }, 600, function() {
$this.remove();
if (typeof(opts.close) == 'function') {
opts.close();
}
})
});
}
};
function toast(method) {
if ((! method) || typeof(method) === 'object') {
return methods.init.apply(this, arguments);
} else if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else {
$.error('Method ' + method + ' does not exist on jQuery.toast');
}
};
toast.methods = methods;
toast.defaults = defaults.toast;
}());
// --------------------------------------------------------------------
}($));