msgGrowl.js
1.91 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
(function($) {
$.msgGrowl = function(config) {
var defaults, options, container, msgGrowl, content, title, text, close;
defaults = {
type: ''
, title: ''
, text: ''
, lifetime: 6500
, sticky: false
, position: 'bottom-right'
, closeTrigger: true
, onOpen: function () {}
, onClose: function () {}
};
options = $.extend(defaults, config);
container = $('.msgGrowl-container.' + options.position);
if (!container.length) {
container = $('<div>', {
'class': 'msgGrowl-container ' + options.position
}).appendTo ('body');
}
msgGrowl = $('<div>', {
'class': 'msgGrowl ' + options.type
});
content = $('<div>', {
'class': 'msgGrowl-content'
}).appendTo (msgGrowl);
text = $('<span>', {
text: options.text
}).appendTo (content);
if (options.closeTrigger) {
close = $('<div>', {
'class': 'msgGrowl-close'
, 'click': function (e) {
e.preventDefault ();
$(this).parent ().fadeOut ('medium', function () {
$(this).remove ();
if (typeof options.onClose === 'function') {
options.onClose ();
}
});
}
}).appendTo (msgGrowl);
}
if (options.title != '') {
title = $('<h4>', {
text: options.title
}).prependTo (content);
}
if (options.lifetime > 0 && !options.sticky) {
setTimeout (function () {
if (typeof options.onClose === 'function') {
options.onClose ();
}
msgGrowl.fadeOut ('medium', function () { $(this).remove (); });
}, options.lifetime);
}
container.addClass (options.position);
if (options.position.split ('-')[0] == 'top') {
msgGrowl.prependTo (container).hide ().fadeIn ('slow');
} else {
msgGrowl.appendTo (container).hide ().fadeIn ('slow');
}
if (typeof options.onOpen === 'function') {
options.onOpen ();
}
}
})(jQuery);