parent
015bbdacc7
commit
a3c6061480
7 changed files with 198 additions and 28 deletions
|
@ -5,23 +5,17 @@
|
|||
'use strict';
|
||||
window.Whisper = window.Whisper || {};
|
||||
|
||||
var SETTINGS = {
|
||||
OFF : 'off',
|
||||
COUNT : 'count',
|
||||
NAME : 'name',
|
||||
MESSAGE : 'message'
|
||||
};
|
||||
|
||||
Whisper.Notifications = new (Backbone.Collection.extend({
|
||||
initialize: function() {
|
||||
this.on('add', this.onAdd);
|
||||
},
|
||||
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) {
|
||||
|
@ -39,25 +33,53 @@
|
|||
extension.notification.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
var setting = storage.get('notification-setting') || 'message';
|
||||
if (setting === SETTINGS.OFF) {
|
||||
return;
|
||||
}
|
||||
|
||||
var iconUrl = 'images/icon_128.png';
|
||||
var title = [
|
||||
this.length,
|
||||
this.length === 1 ? i18n('newMessage') : i18n('newMessages')
|
||||
].join(' ');
|
||||
|
||||
if (setting === SETTINGS.COUNT) {
|
||||
extension.notification.update({
|
||||
type : 'basic',
|
||||
title : title,
|
||||
iconUrl : iconUrl
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (conversationIds.length === 1 && this.showSender()) {
|
||||
iconUrl = this.at(0).get('iconUrl');
|
||||
}
|
||||
extension.notification.update({
|
||||
type : 'list',
|
||||
iconUrl : iconUrl,
|
||||
title : '' + this.length + ' new messages',
|
||||
title : title,
|
||||
message : 'Most recent from ' + this.last().get('title'),
|
||||
items : this.map(function(m) {
|
||||
return {
|
||||
title : m.get('title'),
|
||||
message : m.get('message')
|
||||
};
|
||||
}),
|
||||
var message, title;
|
||||
if (this.showMessage()) {
|
||||
return {
|
||||
title : m.get('title'),
|
||||
message : m.get('message')
|
||||
};
|
||||
} else if (this.showSender()) {
|
||||
return {
|
||||
title : m.get('title'),
|
||||
message : i18n('newMessage')
|
||||
};
|
||||
}
|
||||
}.bind(this)),
|
||||
buttons : [{
|
||||
title : 'Mark all as read',
|
||||
iconUrl : 'images/check.png'
|
||||
|
@ -66,18 +88,38 @@
|
|||
} else {
|
||||
var m = this.at(0);
|
||||
var type = 'basic';
|
||||
if (m.get('imageUrl')) {
|
||||
type = 'image';
|
||||
var message = i18n('newMessage');
|
||||
var imageUrl;
|
||||
if (this.showMessage()) {
|
||||
message = m.get('message');
|
||||
if (m.get('imageUrl')) {
|
||||
type = 'image';
|
||||
imageUrl = m.get('imageUrl');
|
||||
}
|
||||
}
|
||||
if (this.showSender()) {
|
||||
title = m.get('title');
|
||||
iconUrl = m.get('iconUrl');
|
||||
}
|
||||
extension.notification.update({
|
||||
type : type,
|
||||
title : m.get('title'),
|
||||
message : m.get('message'),
|
||||
iconUrl : m.get('iconUrl'),
|
||||
imageUrl : m.get('imageUrl')
|
||||
title : title,
|
||||
message : message,
|
||||
iconUrl : iconUrl,
|
||||
imageUrl : imageUrl
|
||||
});
|
||||
}
|
||||
},
|
||||
getSetting: function() {
|
||||
return storage.get('notification-setting') || 'message';
|
||||
},
|
||||
showMessage: function() {
|
||||
return this.getSetting() === SETTINGS.MESSAGE;
|
||||
},
|
||||
showSender: function() {
|
||||
var setting = this.getSetting();
|
||||
return (setting === SETTINGS.MESSAGE || setting === SETTINGS.NAME);
|
||||
},
|
||||
onAdd: function() {
|
||||
extension.notification.clear();
|
||||
this.update();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue