1
+ class Kandan.Plugins.Notifications
2
+
3
+ @widget_title : " Notifications"
4
+ @widget_icon_url : " /assets/people_icon.png"
5
+ @pluginNamespace : " Kandan.Plugins.Notifications"
6
+
7
+ @popup_notifications_template : _ .template ' <div class="notification popup-notifications"></div>'
8
+ @enable_popup_notifications_template = _ .template ' <a class="btn enable-popup-notifications" href="#"><i class="icon-check-empty"></i> Notifications</a>'
9
+ @disable_popup_notifications_template = _ .template ' <a class="btn disable-popup-notifications" href="#"><i class="icon-check"></i> Notifications</a>'
10
+
11
+ @sound_notifications_template : _ .template ' <div class="notification sound-notifications"></div>'
12
+ @enable_sound_notifications_template = _ .template ' <a class="btn enable-sound-notifications" href="#"><i class="icon-check-empty"></i> Sounds</a>'
13
+ @disable_sound_notifications_template = _ .template ' <a class="btn disable-sound-notifications" href="#"><i class="icon-check"></i> Sounds</a>'
14
+
15
+ @ render: ($el )->
16
+ $notifications = $ (" <div class='notifications_list'></div>" )
17
+ $el .next ().hide ();
18
+
19
+ @ initPopupsNotificationsButtons ()
20
+
21
+ $el .html ($notifications)
22
+
23
+ @ initWebkitNotifications ($notifications)
24
+ @ initSoundNotifications ($notifications)
25
+
26
+ return
27
+
28
+
29
+ @ init: ()->
30
+ Kandan .Widgets .register @pluginNamespace
31
+
32
+ # HTML 5 Popups
33
+ @ initPopupsNotificationsButtons: ()->
34
+ $ (document ).on ' click' , ' .enable-popup-notifications' , => @ enablePopupNotifications ()
35
+ $ (document ).on ' click' , ' .disable-popup-notifications' , => @ disablePopupNotifications ()
36
+ return
37
+
38
+ @ initWebkitNotifications: (container )->
39
+ if Modernizr .notification
40
+ container .append (@ popup_notifications_template ())
41
+
42
+ if @ webkitNotificationsEnabled ()
43
+ @ enablePopupNotifications ()
44
+ else
45
+ @ disablePopupNotifications ()
46
+
47
+ @ enablePopupNotifications: ()->
48
+ if @ webkitNotificationsEnabled ()
49
+ @popups_notifications_enabled = true
50
+ $ (" .popup-notifications .enable-popup-notifications" ).remove ()
51
+ $ (" .notification.popup-notifications" ).append (@ disable_popup_notifications_template ())
52
+ else
53
+ if @ webkitNotificationsDenied ()
54
+ # If the notifications have been denied we need to let the user know because there is nothing else we can do
55
+ alert (" It looks like notifications are denied for this page.\n\n Use your browser settings to allow notifications for this page." )
56
+ else
57
+ window .webkitNotifications .requestPermission (=> @ onPopupNotificationsEnabled ())
58
+
59
+ return
60
+
61
+ @ disablePopupNotifications: ()->
62
+ @popups_notifications_enabled = false
63
+
64
+ $ (" .popup-notifications .disable-popup-notifications" ).remove ()
65
+ $ (" .notification.popup-notifications" ).append (@ enable_popup_notifications_template ())
66
+ return
67
+
68
+ # Returns true if notifications are enabled for this page.
69
+ @ webkitNotificationsEnabled: ()->
70
+ window .webkitNotifications .checkPermission () == 0
71
+
72
+ @ webkitNotificationsDenied: ()->
73
+ window .webkitNotifications .checkPermission () == 2
74
+
75
+ # Callback when notifiactions are enabled for the first time
76
+ @ onPopupNotificationsEnabled: ()->
77
+ if @ webkitNotificationsEnabled ()
78
+ @ enablePopupNotifications ()
79
+
80
+ return
81
+
82
+ # If you are wondering why the kandan icon is not displayed on OS X this is the reason:
83
+ # 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.
84
+ # Apple locked notification icons to the app icons (for instance Chrome icon).
85
+ @ displayNotification: (sender , message )->
86
+ if @popups_notifications_enabled && @ webkitNotificationsEnabled ()
87
+ notification = window .webkitNotifications .createNotification (' /assets/kandanlogo.png' , " #{ sender} says:" , message);
88
+ notification .onclick = ->
89
+ window .focus ()
90
+ @ cancel ()
91
+ return
92
+
93
+ notification .show ();
94
+
95
+ # HTML 5 sounds
96
+ @ initSoundNotifications: ($container )->
97
+ if Modernizr .audio
98
+ $container .append (@ sound_notifications_template ())
99
+ @ enableSoundNotifications ()
100
+ @ initSoundNotificationsButtons ()
101
+
102
+ @ initSoundNotificationsButtons: ()->
103
+ $ (document ).on ' click' , ' .enable-sound-notifications' , => @ enableSoundNotifications ()
104
+ $ (document ).on ' click' , ' .disable-sound-notifications' , => @ disableSoundNotifications ()
105
+ return
106
+
107
+ @ enableSoundNotifications: ()->
108
+ @sound_notifications_enabled = true
109
+ $ (" .sound-notifications .enable-sound-notifications" ).remove ()
110
+ $ (" .notification.sound-notifications" ).append (@ disable_sound_notifications_template ())
111
+
112
+ return
113
+
114
+ @ disableSoundNotifications: ()->
115
+ @sound_notifications_enabled = false
116
+
117
+ $ (" .sound-notifications .disable-sound-notifications" ).remove ()
118
+ $ (" .notification.sound-notifications" ).append (@ enable_sound_notifications_template ())
119
+ return
120
+
121
+ @ playAudioNotification: ()->
122
+ if @sound_notifications_enabled
123
+ Kandan .Plugins .MusicPlayer .playAudioNotice ()
124
+ return
0 commit comments