|
1 | 1 | //(function ( $ ) {
|
2 | 2 |
|
3 | 3 | var Notifications = (function(options) {
|
| 4 | + |
| 5 | + this.currentNotifications = []; |
4 | 6 |
|
5 | 7 | // function notify(){ //This syntax makes the function callable only from within the class.
|
6 | 8 | this.notify = function(notification){ //This syntax makes the function callable by the object (this)
|
|
62 | 64 | counters: [],
|
63 | 65 | markAllSeenSelector: null,
|
64 | 66 | deleteAllSelector: null,
|
| 67 | + viewAllSelector: null, |
65 | 68 | listSelector: null,
|
66 | 69 | listItemTemplate:
|
67 | 70 | '<div class="notificationRow" id="notification_{id}">' +
|
|
82 | 85 | }
|
83 | 86 | }, options);
|
84 | 87 |
|
85 |
| - this.poll = function(){ |
86 |
| - $.ajax({ |
87 |
| - url: this.opts.pollUrl, |
88 |
| - method: "GET", |
89 |
| - data: {read:0}, |
90 |
| - dataType: "json", |
91 |
| -// complete: setTimeout(function() { |
92 |
| -// self.poll(this.opts) |
| 88 | + this.poll = function(all=0){ |
| 89 | + $.ajax({ |
| 90 | + url: this.opts.pollUrl, |
| 91 | + method: "GET", |
| 92 | + data: {all:all}, |
| 93 | + dataType: "json", |
| 94 | +// complete: setTimeout(function() { |
| 95 | +// self.poll(all) |
93 | 96 | // }, this.opts.pollInterval),
|
94 | 97 | // timeout: opts.xhrTimeout
|
95 |
| - }) |
96 |
| - .complete(function(json){ |
97 |
| - console.log(json.responseJSON); |
98 |
| - var notifications = json.responseJSON; |
99 |
| - var rows = ""; |
100 |
| - |
101 |
| - // Update all counters |
102 |
| - for (var i = 0; i < opts.counters.length; i++) { |
103 |
| - if ($(opts.counters[i]).text() != notifications.length) { |
104 |
| - $(opts.counters[i]).text(notifications.length); |
105 |
| - } |
| 98 | + }) |
| 99 | + .done(function(data, textStatus, jqXHR){ |
| 100 | + var notifications = jqXHR.responseJSON; |
| 101 | + currentNotifications = notifications; |
| 102 | + processNotifications(); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + this.processNotifications = function(){ |
| 107 | + var rows = ""; |
| 108 | + |
| 109 | + updateCounters(); |
| 110 | + |
| 111 | + for(i in currentNotifications){ |
| 112 | + var notification = currentNotifications[i]; |
| 113 | + if(notification.flashed == 0){ |
| 114 | + notify(notification); |
| 115 | + } |
| 116 | + rows += renderRow(notification); |
| 117 | + } |
| 118 | + if(opts.listSelector != null && opts.listSelector != ""){ |
| 119 | + $(opts.listSelector).empty().append(rows); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + this.updateCounters = function(){ |
| 124 | + var unreadCount = countUnread(); |
| 125 | + // Update all counters |
| 126 | + for (var i = 0; i < opts.counters.length; i++) { |
| 127 | + if ($(opts.counters[i]).text() != unreadCount) { |
| 128 | + $(opts.counters[i]).text(unreadCount); |
106 | 129 | }
|
107 |
| - |
108 |
| - for(i in notifications){ |
109 |
| - var notification = notifications[i]; |
110 |
| - if(notification.flashed == 0){ |
111 |
| - notify(notification); |
112 |
| - } |
113 |
| - rows += renderRow(notification); |
114 |
| - } |
115 |
| - if(opts.listSelector != null && opts.listSelector != ""){ |
116 |
| - $(opts.listSelector).empty().append(rows); |
117 |
| - } |
118 |
| - }); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + this.countUnread = function(){ |
| 134 | + var count = 0; |
| 135 | + for(i in currentNotifications){ |
| 136 | + var notification = currentNotifications[i]; |
| 137 | + if(notification.read == 0){ |
| 138 | + count += 1; |
| 139 | + } |
| 140 | + } |
| 141 | + return count; |
| 142 | + } |
| 143 | + |
| 144 | + this.getNotificationIndex = function(id){ |
| 145 | + for(i in currentNotifications){ |
| 146 | + if(currentNotifications[i].id == id){ |
| 147 | + return i; |
| 148 | + } |
| 149 | + } |
119 | 150 | }
|
120 | 151 |
|
121 | 152 | this.markAsRead = function(id){
|
122 |
| - console.log(id); |
123 |
| - $.ajax({ |
124 |
| - url: this.opts.markAsReadUrl, |
125 |
| - method: "GET", |
126 |
| - data: {id:id}, |
127 |
| - dataType: "json" |
128 |
| - }) |
129 |
| - .complete(function(json){ |
130 |
| - if($("#notification_"+id).length){ |
131 |
| - $("#notification_"+id).slideUp(); |
132 |
| - } |
133 |
| - }); |
| 153 | + console.log(id); |
| 154 | + $.ajax({ |
| 155 | + url: this.opts.markAsReadUrl, |
| 156 | + method: "GET", |
| 157 | + data: {id:id}, |
| 158 | + dataType: "json" |
| 159 | + }) |
| 160 | + .done(function(data, textStatus, jqXHR){ |
| 161 | + if($("#notification_"+id).length){ |
| 162 | + $("#notification_"+id).slideUp(); |
| 163 | + } |
| 164 | + //Remove the notification from the currentNotifications array. |
| 165 | + var index = getNotificationIndex(id); |
| 166 | + currentNotifications.splice(index,1); |
| 167 | + updateCounters(); |
| 168 | + }); |
134 | 169 | }
|
135 | 170 |
|
136 | 171 | this.markAsUnread = function(){
|
|
146 | 181 | }
|
147 | 182 |
|
148 | 183 | this.flash = function(notification){
|
149 |
| - $.ajax({ |
150 |
| - url: this.opts.flashUrl, |
151 |
| - method: "GET", |
152 |
| - data: {id:notification.id}, |
153 |
| - dataType: "json" |
154 |
| - }) |
155 |
| - .complete(function(json){ |
156 |
| - |
157 |
| - }); |
| 184 | + $.ajax({ |
| 185 | + url: this.opts.flashUrl, |
| 186 | + method: "GET", |
| 187 | + data: {id:notification.id}, |
| 188 | + dataType: "json" |
| 189 | + }) |
| 190 | + .done(function(data, textStatus, jqXHR){ |
| 191 | + //Update reference in currentNotifications array. |
| 192 | + var index = getNotificationIndex(notification.id); |
| 193 | + currentNotifications[index].flashed = 1; |
| 194 | + }); |
158 | 195 | }
|
159 | 196 |
|
160 | 197 | this.renderRow = function(notification){
|
161 |
| - var html = ""; |
162 |
| - if(notification.url != null && notification.url != ""){ |
163 |
| - html += "<div onclick='window.location=\"" + notification.url + "\"'>"; |
164 |
| - }else{ |
165 |
| - html += "<div>"; |
166 |
| - } |
167 |
| - |
168 |
| - html += self.opts.listItemTemplate; |
169 |
| - html += "</div>"; |
170 |
| - html = html.replace(/\{id}/g, notification.id); |
171 |
| - html = html.replace(/\{title}/g, notification.title); |
172 |
| - html = html.replace(/\{body}/g, notification.body); |
173 |
| - html = html.replace(/\{read}/g, '<span onclick="markAsRead(' + notification.id + ');" class="notification-seen glyphicon glyphicon-ok" data-keepOpenOnClick></span>'); |
174 |
| - html = html.replace(/\{delete}/g, '<span onclick="" class="notification-delete glyphicon glyphicon-remove" data-keepOpenOnClick></span>'); |
| 198 | + var html = ""; |
| 199 | + if(notification.url != null && notification.url != ""){ |
| 200 | + html += "<div >"; |
| 201 | + }else{ |
| 202 | + html += "<div>"; |
| 203 | + } |
| 204 | + |
| 205 | + html += self.opts.listItemTemplate; |
| 206 | + html += "</div>"; |
| 207 | + html = html.replace(/\{id}/g, notification.id); |
| 208 | + html = html.replace(/\{title}/g, notification.title); |
| 209 | + html = html.replace(/\{body}/g, notification.body); |
| 210 | + html = html.replace(/\{read}/g, '<span onclick="markAsRead(' + notification.id + ');" class="notification-seen glyphicon glyphicon-ok" data-keepOpenOnClick></span>'); |
| 211 | + html = html.replace(/\{delete}/g, '<span onclick="" class="notification-delete glyphicon glyphicon-remove" data-keepOpenOnClick></span>'); |
175 | 212 | // html = html.replace(/\{timeago}/g, '<span class="notification-timeago">' + notification.timeago +'</span>');
|
176 | 213 |
|
177 | 214 | return html;
|
|
180 | 217 | // notify();
|
181 | 218 |
|
182 | 219 | $(document).ready(function(){
|
183 |
| - $(document).delegate("ul.dropdown-menu [data-keepOpenOnClick]", "click", function(e) { |
184 |
| - e.stopPropagation(); |
185 |
| - }); |
| 220 | + $(document).delegate("ul.dropdown-menu [data-keepOpenOnClick]", "click", function(e) { |
| 221 | + e.stopPropagation(); |
| 222 | + }); |
| 223 | + |
| 224 | + if(self.opts.viewAllSelector != null && self.opts.viewAllSelector != ""){ |
| 225 | + $(self.opts.viewAllSelector).click(function(){ |
| 226 | + poll(1); //Poll for all notifications. |
| 227 | + }); |
| 228 | + } |
| 229 | + |
| 230 | + if(self.opts.viewUnreadSelector != null && self.opts.viewUnreadSelector != ""){ |
| 231 | + $(self.opts.viewUnreadSelector).click(function(){ |
| 232 | + poll(0); //Poll for unread notifications. |
| 233 | + }); |
| 234 | + } |
186 | 235 | });
|
187 | 236 |
|
188 | 237 | return this;
|
|
0 commit comments