2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-03-17 23:06:21 +01:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2015-09-14 05:25:04 +02:00
|
|
|
Whisper.Notifications = new (Backbone.Collection.extend({
|
|
|
|
initialize: function() {
|
2015-11-26 00:11:01 +01:00
|
|
|
this.on('add', this.onAdd);
|
|
|
|
this.on('remove', this.onRemove);
|
2015-09-14 05:25:04 +02:00
|
|
|
},
|
|
|
|
isEnabled: function(callback) {
|
|
|
|
return Notification.permission === 'granted' &&
|
|
|
|
!storage.get('disable-notifications');
|
|
|
|
},
|
|
|
|
enable: function(callback) {
|
|
|
|
storage.remove('disable-notifications');
|
|
|
|
Notification.requestPermission(function(status) {
|
|
|
|
callback(status);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
disable: function() {
|
|
|
|
storage.put('disable-notifications', true);
|
|
|
|
},
|
|
|
|
onclick: function() {
|
|
|
|
var last = this.last();
|
|
|
|
if (!last) {
|
|
|
|
openInbox();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var conversation = ConversationController.create({
|
|
|
|
id: last.get('conversationId')
|
|
|
|
});
|
|
|
|
openConversation(conversation);
|
|
|
|
this.clear();
|
|
|
|
},
|
2015-11-26 00:11:01 +01:00
|
|
|
update: function() {
|
|
|
|
if (this.length === 0) {
|
|
|
|
extension.notification.clear();
|
|
|
|
return;
|
|
|
|
}
|
2015-09-14 05:25:04 +02:00
|
|
|
if (this.length > 1) {
|
|
|
|
var iconUrl = 'images/icon_128.png';
|
|
|
|
var conversationIds = _.uniq(this.map(function(m) {
|
|
|
|
return m.get('conversationId');
|
|
|
|
}));
|
|
|
|
if (conversationIds.length === 1) {
|
|
|
|
iconUrl = this.at(0).get('iconUrl');
|
|
|
|
}
|
2015-11-26 00:11:01 +01:00
|
|
|
extension.notification.update({
|
2015-09-14 05:25:04 +02:00
|
|
|
type : 'list',
|
|
|
|
iconUrl : iconUrl,
|
|
|
|
title : '' + this.length + ' new messages',
|
|
|
|
message : 'Most recent from ' + this.last().get('title'),
|
|
|
|
items : this.map(function(m) {
|
|
|
|
return {
|
|
|
|
title : m.get('title'),
|
|
|
|
message : m.get('message')
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
buttons : [{
|
|
|
|
title : 'Mark all as read',
|
|
|
|
iconUrl : 'images/check.png'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var m = this.at(0);
|
2015-09-14 22:56:02 +02:00
|
|
|
var type = 'basic';
|
|
|
|
if (m.get('imageUrl')) {
|
|
|
|
type = 'image';
|
|
|
|
}
|
2015-11-26 00:11:01 +01:00
|
|
|
extension.notification.update({
|
2015-09-14 22:56:02 +02:00
|
|
|
type : type,
|
2015-09-14 05:25:04 +02:00
|
|
|
title : m.get('title'),
|
|
|
|
message : m.get('message'),
|
|
|
|
iconUrl : m.get('iconUrl'),
|
|
|
|
imageUrl : m.get('imageUrl')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2015-11-26 00:11:01 +01:00
|
|
|
onAdd: function() {
|
|
|
|
extension.notification.clear();
|
|
|
|
this.update();
|
|
|
|
},
|
|
|
|
onRemove: function() {
|
|
|
|
this.update();
|
|
|
|
},
|
2015-09-14 05:25:04 +02:00
|
|
|
clear: function() {
|
|
|
|
this.reset([]);
|
|
|
|
}
|
|
|
|
}))();
|
2015-03-17 23:06:21 +01:00
|
|
|
})();
|