-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhtml5-notifications.js
86 lines (70 loc) · 2.18 KB
/
html5-notifications.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
// Generated by CoffeeScript 1.6.2
/*
HTML5 Notifications
- dependencies: jQuery
@djalmaaraujo
*/
(function() {
var $;
$ = jQuery;
this.Notifications = (function() {
function Notifications(authorize) {
if (authorize) {
this.checkForPermission();
}
}
Notifications.prototype.isAuthorized = function() {
if (!this.apiAvailable()) {
return false;
}
return window.webkitNotifications.checkPermission() === 0;
};
Notifications.prototype.apiAvailable = function() {
return window.webkitNotifications;
};
Notifications.prototype.authorize = function(cb) {
if (!this.apiAvailable()) {
return false;
}
return webkitNotifications.requestPermission(cb);
};
Notifications.prototype.show = function(url, title, body) {
var popup,
_this = this;
if (!this.apiAvailable()) {
return false;
}
if (this.isAuthorized()) {
popup = window.webkitNotifications.createNotification(url, title, body);
popup.show();
setTimeout((function() {
return popup.cancel();
}), 5000);
return true;
} else {
return this.callForPermission(function() {
return _this.show(url, title, body);
});
}
};
Notifications.prototype.checkForPermission = function() {
if (!this.isAuthorized()) {
return this.callForPermission();
}
};
Notifications.prototype.callForPermission = function(cb) {
var authorizeBox,
_this = this;
if (!this.apiAvailable()) {
return false;
}
authorizeBox = $('<div />').addClass('notifications-authorize').html('<p>Seu navegador possui suporte a notificações. Para solicitar uma permissão de notificação, clique no botão abaixo. Aperte "ALLOW" ou "PERMITIR" para a janela de notificação que irá aparecer. <input type="button" value="Ativar notificações" /></p>');
$('body').append(authorizeBox);
return authorizeBox.find('input').on('click', function() {
authorizeBox.remove();
return _this.authorize(cb);
});
};
return Notifications;
})();
}).call(this);