/
notifications.js.coffee
124 lines (94 loc) · 4.83 KB
/
notifications.js.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
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
class Kandan.Plugins.Notifications
@widget_title: "Notifications"
@widget_icon_url: "/assets/people_icon.png"
@pluginNamespace: "Kandan.Plugins.Notifications"
@popup_notifications_template: _.template '<div class="notification popup-notifications"></div>'
@enable_popup_notifications_template = _.template '<a class="btn enable-popup-notifications" href="#"><i class="icon-check-empty"></i> Notifications</a>'
@disable_popup_notifications_template = _.template '<a class="btn disable-popup-notifications" href="#"><i class="icon-check"></i> Notifications</a>'
@sound_notifications_template: _.template '<div class="notification sound-notifications"></div>'
@enable_sound_notifications_template = _.template '<a class="btn enable-sound-notifications" href="#"><i class="icon-check-empty"></i> Sounds</a>'
@disable_sound_notifications_template = _.template '<a class="btn disable-sound-notifications" href="#"><i class="icon-check"></i> Sounds</a>'
@render: ($el)->
$notifications = $("<div class='notifications_list'></div>")
$el.next().hide();
@initPopupsNotificationsButtons()
$el.html($notifications)
@initWebkitNotifications($notifications)
@initSoundNotifications($notifications)
return
@init: ()->
Kandan.Widgets.register @pluginNamespace
# HTML 5 Popups
@initPopupsNotificationsButtons: ()->
$(document).on 'click', '.enable-popup-notifications', => @enablePopupNotifications()
$(document).on 'click', '.disable-popup-notifications', => @disablePopupNotifications()
return
@initWebkitNotifications: (container)->
if Modernizr.notification
container.append(@popup_notifications_template())
if @webkitNotificationsEnabled()
@enablePopupNotifications()
else
@disablePopupNotifications()
@enablePopupNotifications: ()->
if @webkitNotificationsEnabled()
@popups_notifications_enabled = true
$(".popup-notifications .enable-popup-notifications").remove()
$(".notification.popup-notifications").append(@disable_popup_notifications_template())
else
if @webkitNotificationsDenied()
# If the notifications have been denied we need to let the user know because there is nothing else we can do
alert("It looks like notifications are denied for this page.\n\nUse your browser settings to allow notifications for this page.")
else
window.webkitNotifications.requestPermission(=> @onPopupNotificationsEnabled())
return
@disablePopupNotifications: ()->
@popups_notifications_enabled = false
$(".popup-notifications .disable-popup-notifications").remove()
$(".notification.popup-notifications").append(@enable_popup_notifications_template())
return
# Returns true if notifications are enabled for this page.
@webkitNotificationsEnabled: ()->
window.webkitNotifications.checkPermission() == 0
@webkitNotificationsDenied: ()->
window.webkitNotifications.checkPermission() == 2
# Callback when notifiactions are enabled for the first time
@onPopupNotificationsEnabled: ()->
if @webkitNotificationsEnabled()
@enablePopupNotifications()
return
# If you are wondering why the kandan icon is not displayed on OS X this is the reason:
# If you try notifying users on MacOS Mountain Lion, using a custom notification icon, don't be surprised that the Web browser icon overrides the icon you defined.
# Apple locked notification icons to the app icons (for instance Chrome icon).
@displayNotification: (sender, message)->
if @popups_notifications_enabled && @webkitNotificationsEnabled()
notification = window.webkitNotifications.createNotification('/assets/kandanlogo.png', "#{sender} says:", message);
notification.onclick = ->
window.focus()
@cancel()
return
notification.show();
# HTML 5 sounds
@initSoundNotifications: ($container)->
if Modernizr.audio
$container.append(@sound_notifications_template())
@enableSoundNotifications()
@initSoundNotificationsButtons()
@initSoundNotificationsButtons: ()->
$(document).on 'click', '.enable-sound-notifications', => @enableSoundNotifications()
$(document).on 'click', '.disable-sound-notifications', => @disableSoundNotifications()
return
@enableSoundNotifications: ()->
@sound_notifications_enabled = true
$(".sound-notifications .enable-sound-notifications").remove()
$(".notification.sound-notifications").append(@disable_sound_notifications_template())
return
@disableSoundNotifications: ()->
@sound_notifications_enabled = false
$(".sound-notifications .disable-sound-notifications").remove()
$(".notification.sound-notifications").append(@enable_sound_notifications_template())
return
@playAudioNotification: ()->
if @sound_notifications_enabled
Kandan.Plugins.MusicPlayer.playAudioNotice()
return