-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdepage-growl.js
195 lines (175 loc) · 5.35 KB
/
depage-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
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
/**
* @file depage-growl.js
*
* depage growl notification jquery-plugin
*
*
* copyright (c) 2009-2015 Frank Hellenkamp [jonas@depage.net]
*
* @todo add different behaviour depending on the page being visible:
* https://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active
*
* @author Frank Hellenkamp [jonas@depage.net]
*/
;(function($){
"use strict";
/*jslint browser: true*/
/*global $:false */
if(!$.depage){
$.depage = {};
}
// {{{ htmlEncode
var htmlEncode = function(value) {
return $('<div/>').text(value).html();
};
// }}}
var InternalNotification = function(title, options) {
this.title = title;
this.options = options;
};
// {{{ handleEvent
InternalNotification.prototype.handleEvent = function(e) {
switch (e.type) {
case 'click':
this.onClick(e);
break;
case 'error':
this.onError(e);
break;
}
};
// }}}
// {{{ onClick
InternalNotification.prototype.onClick = function(e) {
if (typeof this.options.onClick === "function") {
this.options.onClick(e);
}
};
// }}}
// {{{ onError
InternalNotification.prototype.onError = function(e) {
if (typeof this.options.onError === "function") {
this.options.onError(e);
}
};
// }}}
// {{{ testSystemSupport
InternalNotification.prototype.testSystemSupport = function() {
var isSupported = 'Notification' in window;
var needsPermission = !(isSupported && Notification.permission === 'granted');
var permissionLevel = (isSupported ? Notification.permission : null);
if (isSupported && needsPermission) {
Notification.requestPermission(function (perm) {
switch (perm) {
case 'granted':
console.log("granted");
break;
case 'denied':
console.log("denied");
break;
}
});
} else if (isSupported) {
return true;
}
return false;
};
// }}}
// {{{ growl
InternalNotification.prototype.growl = function() {
if (this.testSystemSupport()) {
this.growlSystemNotification();
if (this.options.alwaysShowFallback) {
this.growlHtmlFallback();
}
} else {
this.growlHtmlFallback();
}
};
// }}}
// {{{ growlSystemNotification
InternalNotification.prototype.growlSystemNotification = function() {
var n = new Notification(this.title, {
body: this.options.message,
icon: this.options.icon
});
n.addEventListener("error", this, false);
n.addEventListener("click", this, false);
};
// }}}
// {{{ growlHtmlFallback
InternalNotification.prototype.growlHtmlFallback = function() {
// get or add notification-area
var $notificationArea = $("#depageGrowlArea");
var $notification;
var iconImg = "";
var currentNotification = this;
if ($notificationArea.length === 0) {
$notificationArea = $("<div id=\"depageGrowlArea\"></div>").appendTo("body");
$notificationArea.css({
position: "fixed",
top: 0,
right: 0,
zIndex: 30000
});
}
var closeNotification = function() {
$notification.clearQueue().fadeOut(800, function() {
$(this).remove();
});
};
if (this.options.icon) {
iconImg = "<img src=\"" + this.options.icon + "\" class=\"depage-growl-icon\">";
}
$notification = $("<div class=\"depage-growl-message\">" + iconImg + "<h3>" + htmlEncode(this.title) + "</h3><p>" + htmlEncode(this.options.message) + "</p></div>")
.appendTo($notificationArea)
.hide()
.fadeIn(400)
.animate({
opacity: 1
},{
duration: 3500,
complete: function() {
closeNotification();
}
});
$notification.click( function(e) {
currentNotification.onClick(e);
closeNotification();
});
};
// }}}
var base = {};
// {{{ growl()
base.growl = function(title, options) {
options = $.extend({}, base.defaultOptions, options);
var n = new InternalNotification(title, options);
n.growl();
console.log(n);
};
// }}}
// {{{ defaultOptions
base.defaultOptions = {
message: "",
icon: "",
alwaysShowFallback: false,
onClick: null,
onError: null
};
// }}}
// {{{ $.depage.growl()
/**
* @function growl()
*
* shows a growl-like notification
*
* @param title title of the notification
* @param message bodytext of the notification (optional)
* @param icons icon of the notification (optional)
*/
$.depage.growl = function(title, options) {
base.growl(title, options);
};
// }}}
})(jQuery);
/* vim:set ft=javascript sw=4 sts=4 fdm=marker : */