-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.jwNotify.js
160 lines (128 loc) · 5.44 KB
/
jquery.jwNotify.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
/*
jquery.webkit-notification plugin v 0.1
Developed by Arjun Raj
email: [email protected]
site: http://www.athousandnodes.com
Licences: MIT, GPL
http://www.opensource.org/licenses/mit-license.php
http://www.gnu.org/licenses/gpl.html
*/
//Globals
var jwNotify = {};
jwNotify.status = false;
//Check if the browser supports notifications
if (window.webkitNotifications){
jwNotify.status = true;
}
//Function to request for permission
function requestingPopupPermission(callback){
//Check if the feature is supported by browser and the variables have be saved
if((jwNotify.status) && (jwNotify.options) && (jwNotify.type))
window.webkitNotifications.requestPermission(callback);
else
console.log('Error');
}
//Function to show the actual Notification
function showNotification(){
if(window.webkitNotifications.checkPermission() != 0){
requestingPopupPermission(showNotification);
}
else{
if(jwNotify.type == 'html'){
if(jwNotify.options.url){
var popup = window.webkitNotifications.createHTMLNotification(jwNotify.options.url);
//Invoke the onclick function
if(jwNotify.options.onclick)
popup.onclick = jwNotify.options.onclick;
//Invoke the onshow function
if(jwNotify.options.onshow)
popup.onshow = jwNotify.options.onshow;
//Invoke the onclose function
if(jwNotify.options.onclose)
popup.onclose = jwNotify.options.onclose;
//Invoke the onshow function
if(jwNotify.options.onerror)
popup.onerror = jwNotify.options.onerror;
//replaceId for the notification
if(jwNotify.options.id)
popup.replaceId = jwNotify.options.id;
popup.show();
}
}
else{
if(jwNotify.options.image || jwNotify.options.title || jwNotify.options.body){
var popup = window.webkitNotifications.createNotification(jwNotify.options.image, jwNotify.options.title, jwNotify.options.body);
//Add Directionality for the shown text
if(jwNotify.options.dir)
popup.dir = jwNotify.options.dir;
//Invoke the onclick function
if(jwNotify.options.onclick)
popup.onclick = jwNotify.options.onclick;
//Invoke the onshow function
if(jwNotify.options.onshow)
popup.onshow = jwNotify.options.onshow;
//Invoke the onclose function
if(jwNotify.options.onclose)
popup.onclose = jwNotify.options.onclose;
//Invoke the onshow function
if(jwNotify.options.onerror)
popup.onerror = jwNotify.options.onerror;
//replaceId for the notification
if(jwNotify.options.id)
popup.replaceId = jwNotify.options.id;
popup.show();
}
else
console.log('not enough parameters');
}
if(popup && (jwNotify.options.timeout > 0)){
//Set Timeout for hiding the popup!
setTimeout(function(){
popup.cancel();
}, jwNotify.options.timeout);
//Reset once showing it!
jwNotify.options = null;
jwNotify.type = null;
}
}
}
jQuery.extend({
jwNotify: function(options){
//Defaults
var settings = {
image : null, //Image to be shown in the notification area
title: null, //Title
body: null, //Body of the notification to be shown
// persist : false, // Not required
timeout: 5000, //In milli seconds, if timout is 0 then the message will persist till the user closes it
dir : null, //Direction of the text shown in the notification
onclick: null, //Callback for onclick event on the notification
onshow: null, //Callback for onshow event
onerror: null, //Callback for onerror event
onclose: null, //Callback for onclose event
id: null //Identifier for the notifiction. This is used to identify the notification message shown.
};
if(options)
$.extend(settings, options);
jwNotify.options = settings;
jwNotify.type = 'normal';
showNotification();
},
jwNotifyHTML : function(options){
//Defaults
var settings = {
url: null, //HTML mode only takes 2 options this is for the url of the html page
timeout: 5000, //Timeout for hiding it
onclick: null, //Callback for onclick event on the notification
onshow: null, //Callback for onshow event
onerror: null, //Callback for onerror event
onclose: null, //Callback for onclose event
id: null //Identifier for the notifiction. This is used to identify the notification message shown.
};
if(options)
$.extend(settings, options);
jwNotify.options = settings;
jwNotify.type = 'html';
showNotification();
}
});