-
Notifications
You must be signed in to change notification settings - Fork 3
/
html5-notifications.coffee
55 lines (38 loc) · 1.38 KB
/
html5-notifications.coffee
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
###
HTML5 Notifications
- dependencies: jQuery
@djalmaaraujo
###
$ = jQuery
class @Notifications
constructor: (@authorize) ->
@checkForPermission() if @authorize
isAuthorized: ->
return false unless @apiAvailable()
window.webkitNotifications.checkPermission() == 0
apiAvailable: ->
window.webkitNotifications
authorize: (cb) ->
return false unless @apiAvailable()
webkitNotifications.requestPermission(cb)
show: (url, title, body) ->
return false unless @apiAvailable()
if @isAuthorized()
popup = window.webkitNotifications.createNotification(url, title, body)
popup.show()
setTimeout (->
popup.cancel()
), 5000
true
else
@callForPermission => @show(url, title, body)
checkForPermission: ->
@callForPermission() unless @isAuthorized()
callForPermission: (cb) ->
return false unless @apiAvailable()
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);
authorizeBox.find('input').on 'click', =>
authorizeBox.remove()
@authorize(cb)