Skip to content

Commit 9bad48d

Browse files
committed
Added clear() and close() functionality
1 parent 24a7f0b commit 9bad48d

2 files changed

Lines changed: 178 additions & 24 deletions

File tree

push.js

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,43 @@ var root = (window !== 'undefined' ? window : self);
7070
/* Whether Push has permission to notify */
7171
hasPermission = false,
7272

73+
/* List of active notifications */
74+
notifications = [],
75+
76+
/**
77+
* Closes a notification
78+
* @param {Notification} notification
79+
* @return {void}
80+
*/
81+
close_notification = function (notification) {
82+
83+
/* Safari 6+, Chrome 23+ */
84+
if (notification.close) {
85+
86+
notification.close();
87+
88+
/* Legacy webkit browsers */
89+
} else if (notification.cancel) {
90+
91+
notification.cancel();
92+
93+
/* IE9+ */
94+
} else if (w.external && w.external.msIsSiteMode) {
95+
96+
w.external.msSiteModeClearIconOverlay();
97+
98+
}
99+
100+
},
101+
102+
/**
103+
* Updates the notification count
104+
* @return {void}
105+
*/
106+
updateCount = function () {
107+
self.count = notifications.length;
108+
},
109+
73110
/**
74111
* Callback function for the 'create' method
75112
* @return {void}
@@ -128,20 +165,7 @@ var root = (window !== 'undefined' ? window : self);
128165
wrapper = {
129166

130167
close: function () {
131-
132-
/* Safari 6+, Chrome 23+ */
133-
if (notification.close) {
134-
notification.close();
135-
136-
/* Legacy webkit browsers */
137-
} else if (notification.cancel) {
138-
notification.cancel();
139-
140-
/* IE9+ */
141-
} else if (w.external && win.external.msIsSiteMode) {
142-
w.external.msSiteModeClearIconOverlay();
143-
}
144-
168+
close_notification(notification);
145169
}
146170

147171
};
@@ -167,6 +191,15 @@ var root = (window !== 'undefined' ? window : self);
167191
notification.addEventListener('close', options.onClose);
168192
notification.addEventListener('cancel', options.onClose);
169193
}
194+
195+
/* Add it to the global array */
196+
notifications.push(notification);
197+
198+
/* Update the notification count */
199+
updateCount();
200+
201+
/* Return the wrapper so the user can call close() */
202+
return wrapper;
170203
},
171204

172205
/**
@@ -184,6 +217,9 @@ var root = (window !== 'undefined' ? window : self);
184217
/* Allow enums to be accessible from Push object */
185218
self.Permission = Permission;
186219

220+
/* Number of open notifications */
221+
self.count = 0;
222+
187223
/*****************
188224
Permissions
189225
/*****************/
@@ -327,18 +363,68 @@ var root = (window !== 'undefined' ? window : self);
327363
throw 'PushError: Title of notification must be a string';
328364
}
329365

330-
console.log(self.Permission.has());
331-
332366
/* Request permission if it isn't granted */
333367
if (!self.Permission.has()) {
334368
self.Permission.request(function () {
335-
create_callback(title, options);
369+
return create_callback(title, options);
336370
});
337371
} else {
338-
create_callback(title, options);
372+
return create_callback(title, options);
373+
}
374+
375+
};
376+
377+
/**
378+
* Closes a notification with the given tag
379+
* @param {String} tag - Tag of the notification to close
380+
* @return {void}
381+
*/
382+
self.close = function (tag) {
383+
384+
var i, notification;
385+
386+
for (i = 0; i < notifications.length; i++) {
387+
388+
notification = notifications[i];
389+
390+
/* Run only if the tags match */
391+
if (notification.tag === tag) {
392+
393+
/* Call the notification's close() method */
394+
close_notification(notification);
395+
396+
/* Remove the notification from the global array */
397+
notifications.splice(i, 1);
398+
399+
/* Update the notification count */
400+
updateCount();
401+
402+
/* Return after the first notification is closed */
403+
return;
404+
405+
}
339406
}
340407

341408
};
409+
410+
/**
411+
* Clears all notifications
412+
* @return {void}
413+
*/
414+
self.clear = function () {
415+
416+
var i;
417+
418+
for (i = 0; i < notifications.length; i++) {
419+
close_notification(notifications[i]);
420+
}
421+
422+
/* Reset the global array */
423+
notifications = [];
424+
425+
/* Update the notification count */
426+
updateCount();
427+
};
342428
};
343429

344430
return Push;

test/push_spec.js

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ describe('permission', function () {
3939
}, 500);
4040
});
4141

42+
it('should request permission if permission is not granted', function () {
43+
44+
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
45+
cb(Push.Permission.DENIED);
46+
});
47+
48+
Push.Permission.request();
49+
50+
Push.create('hello world!');
51+
52+
expect(window.Notification.requestPermission).toHaveBeenCalled();
53+
});
54+
55+
4256
it('should update permission value if permission is granted and execute callback', function (done) {
4357

4458
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
@@ -61,32 +75,50 @@ describe('creating notifications', function () {
6175
var callback;
6276

6377
beforeAll(function () {
78+
6479
jasmine.clock().install();
80+
81+
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
82+
cb(Push.Permission.GRANTED);
83+
});
6584
});
6685

6786
beforeEach(function () {
6887
callback = jasmine.createSpy('callback');
6988
});
7089

90+
7191
it('should throw exception if no title is provided', function () {
7292
expect(function() {
7393
Push.create();
7494
}).toThrow();
7595
});
7696

77-
it('should request permission if permission is not granted', function () {
97+
it('should return wrapper successfully', function () {
7898

79-
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
80-
cb(Push.Permission.DENIED);
81-
});
99+
var wrapper = Push.create('hello world');
82100

83-
Push.Permission.request();
101+
expect(wrapper.close).not.toBe(undefined);
102+
expect(wrapper.close.constructor).toBe(Function);
103+
104+
});
105+
106+
it('should return the correct notification count', function () {
107+
108+
var count;
109+
110+
count = Push.count;
84111

85112
Push.create('hello world!');
86113

87-
expect(window.Notification.requestPermission).toHaveBeenCalled();
114+
expect(Push.count).toBe(count + 1);
115+
88116
});
89117

118+
});
119+
120+
describe('closing notifications', function () {
121+
90122
it('should close notifications if a timeout is specified', function () {
91123

92124
spyOn(window.Notification, 'requestPermission').and.callFake(function (cb) {
@@ -106,4 +138,40 @@ describe('creating notifications', function () {
106138
expect(window.Notification.prototype.close).toHaveBeenCalled();
107139

108140
});
141+
142+
it('should close a notification given a tag', function () {
143+
144+
var count;
145+
146+
spyOn(window.Notification.prototype, 'close');
147+
148+
Push.create('hello world!', {
149+
tag: 'foo'
150+
});
151+
152+
count = Push.count;
153+
154+
Push.close('foo');
155+
156+
expect(window.Notification.prototype.close).toHaveBeenCalled();
157+
expect(Push.count).toBe(count - 1);
158+
159+
});
160+
161+
it('should close all notifications when cleared', function () {
162+
163+
spyOn(window.Notification.prototype, 'close');
164+
165+
Push.create('hello world!', {
166+
tag: 'foo'
167+
});
168+
169+
expect(Push.count).toBeGreaterThan(0);
170+
171+
Push.clear();
172+
173+
expect(window.Notification.prototype.close).toHaveBeenCalled();
174+
expect(Push.count).toBe(0);
175+
176+
});
109177
});

0 commit comments

Comments
 (0)