-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.growl.js
133 lines (125 loc) · 3.88 KB
/
jquery.growl.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
/*
* jQuery Growl plugin
* Version 2.0.4
* Last Updated 2014-02-08
* @requires jQuery v1.11.0 or later (untested on previous version)
*
* Examples at: http://fragmentedcode.com/jquery-growl
* Copyright (c) 2008-2014 David Higgins
*
* Special thanks to Daniel Mota for inspiration:
* http://icebeat.bitacoras.com/mootools/growl/
*
* Released under MIT License.
*/
(function() {
var growl = {
getInstance: function(rebuild) {
var instance = $('#' + settings.dockId);
if(instance.length < 1 || rebuild === true) {
instance = $(settings.dockTemplate).attr('id', settings.dockId).css(settings.dockCss).addClass(settings.dockClass);
if(settings.defaultStylesheet) {
$('head').appendTo('<link rel="stylesheet" type="text/css" href="' + settings.defaultStylesheet + '" />');
}
$(settings.dockContainerSelector).append(instance);
}
return instance;
},
notify: function(title, message, priority) {
var instance = this.getInstance();
var notice = $(settings.noticeTemplate).hide().css(settings.noticeCss).addClass(settings.noticeClass);
if(!priority) { priority = 'primary'; }
switch(priority) {
case 'info': notice.addClass('panel-info'); break;
case 'success': notice.addClass('panel-success'); break;
case 'warning': notice.addClass('panel-warning'); break;
case 'danger': notice.addClass('panel-danger'); break;
default: notice.addClass('panel-primary'); break;
}
$('.title', notice).css(settings.noticeTitleCss).html(title);
$('.close', notice).click(function(evt) {
evt.preventDefault();
$(this).closest('.notice').remove();
})
$('.message', notice)
.html(message);
console.log(notice);
instance.append(settings.noticeDisplay(notice));
if(settings.displayTimeout > 0 && settings.doNotClose.indexOf(priority) < 0) {
setTimeout(function() {
settings.noticeRemove(notice, function() {
notice.remove();
});
}, settings.displayTimeout);
}
},
r: function(text, expr, val) {
while(expr.test(text)) {
text = text.replace(expr, val);
}
return text;
},
}
var settings = {
dockId: 'jqueryGrowlDock',
dockClass: 'growl',
dockTemplate: '<div></div>',
dockContainerSelector: 'body',
dockCss: {
position: 'fixed',
top: '10px',
right: '10px',
width: '300px',
zIndex: 50000
},
noticeTemplate:
'<div class="notice panel">' +
'<div class="panel-heading ">' +
'<h3>' +
'<a class="title"></a>' +
'<button type="button" class="close">x</button>' +
'</h3>' +
'</div>' +
'<div class="message panel-body">%message%</div>' +
'</div>',
noticeCss: {
},
noticeTitleCss: {
color: '#fff',
textDecoration: 'none',
},
noticeDisplay: function(notice) {
return notice.fadeIn(settings.noticeFadeTimeout);
},
noticeRemove: function(notice, callback) {
return notice.animate({opacity: '0', height: '0px'}, {duration: settings.noticeFadeTimeout, complete: callback });
},
doNotClose: ['danger', ],
noticeFadeTimeout: 'slow',
displayTimeout: 3500,
defaultStylesheet: null,
noticeElement: function(el) {
this.noticeTemplate = $(el);
}
}
$.growl = function(options) {
if(typeof options === 'object') {
if('settings' in options) {
settings = $.extend(settings, options.settings);
}
var title = 'Notice', message = null, priority = 'primary';
if('title' in options) {
title = options.title;
}
if('message' in options) {
message = options.message;
}
if('priority' in options) {
priority = options.priority;
}
if(message != null) {
growl.notify(title, message, priority);
}
}
}
})();