Fix desktop notification

use on/off field for enable/disable desktop notification
show only new notifications after enable
use localstorage to save enabled/disalbed and last notification id
hide settings on browsers without desktop notification
This commit is contained in:
Fabrixxm 2015-06-24 09:49:53 +02:00
parent 2112b9cacc
commit 4036ad2554
7 changed files with 113 additions and 26 deletions

View file

@ -75,14 +75,14 @@
setupFieldRichtext();
/* popup menus */
function close_last_popup_menu() {
if(last_popup_menu) {
last_popup_menu.hide();
last_popup_button.removeClass("selected");
last_popup_menu = null;
last_popup_button = null;
}
}
function close_last_popup_menu() {
if(last_popup_menu) {
last_popup_menu.hide();
last_popup_button.removeClass("selected");
last_popup_menu = null;
last_popup_button = null;
}
}
$('a[rel^=#]').click(function(e){
e.preventDefault();
var parent = $(this).parent();
@ -105,7 +105,7 @@
return false;
});
$('html').click(function() {
close_last_popup_menu();
close_last_popup_menu();
});
// fancyboxes
@ -181,24 +181,33 @@
nnm = $("#nav-notifications-menu");
nnm.html(notifications_all + notifications_mark);
//nnm.attr('popup','true');
eNotif.children("note").each(function(){
var notification_lastitem = localStorage.getItem("notification-lastitem");
var notification_id = 0;
eNotif.children("note").each(function(){
e = $(this);
text = e.text().format("<span class='contactname'>"+e.attr('name')+"</span>");
html = notifications_tpl.format(e.attr('href'),e.attr('photo'), text, e.attr('date'), e.attr('seen'));
nnm.append(html);
if(e.text().search('&rarr;') == 0) {
var notification = new Notification(document.title, {
body: e.text().replace('&rarr; ',''),
icon: e.attr('photo')
});
// TODO (yet unsupported by most browsers):
// Implement notification.onclick()
// notifyMarkAll();
}
notification_id = e.attr('href').match(/\d+$/)[0];
if (notification_lastitem!== null && notification_id>notification_lastitem) {
notification_lastitem = notification_id;
if (getNotificationPermission()==="granted") {
var notification = new Notification(document.title, {
body: e.text().replace('&rarr; ','').format(e.attr('name')),
icon: e.attr('photo')
});
// TODO (yet unsupported by most browsers):
// Implement notification.onclick()
}
}
});
if (notification_lastitem===null) notification_lastitem = notification_id;
localStorage.setItem("notification-lastitem", notification_lastitem)
$("img[data-src]", nnm).each(function(i, el){
// Add src attribute for images with a data-src attribute
@ -762,3 +771,16 @@ function previewTheme(elm) {
});
}
// notification permission settings in localstorage
// set by settings page
function getNotificationPermission() {
if (window.Notification === undefined) {
return null;
}
if (Notification.permission === 'granted') {
return localStorage.getItem('notification-permissions');
} else {
return Notification.permission;
}
}