freifunkmanager/webroot/js/notify.js

44 lines
1.1 KiB
JavaScript
Raw Normal View History

var notify = {container:{},messages:[]};
2017-05-09 02:12:10 +02:00
(function(){
if ("Notification" in window) {
Notification.requestPermission();
}
function removeLast (){
notify.messages.splice(0, 1);
if(notify.container.firstElementChild)
notify.container.removeChild(notify.container.firstElementChild);
}
2017-05-09 02:12:10 +02:00
function renderMsg(msg){
var msgBox = document.createElement('div');
msgBox.classList.add("notify",msg.type);
2017-05-09 02:12:10 +02:00
msgBox.innerHTML = msg.text;
notify.container.appendChild(msgBox);
2017-05-09 02:12:10 +02:00
msgBox.addEventListener('click', function(){
notify.container.removeChild(msgBox);
if (notify.messages.indexOf(msg) !== -1) {
notify.messages.splice(notify.messages.indexOf(msg), 1);
2017-05-09 02:12:10 +02:00
}
});
}
setInterval(removeLast,15000);
notify.send = function(type, text){
2017-05-09 02:12:10 +02:00
if("Notification" in window && Notification.permission === "granted") {
new Notification(text,{body:type,icon:'/img/logo.jpg'});
2017-05-09 02:12:10 +02:00
return;
}
if(notify.messages.length > 10){
removeLast();
2017-05-09 02:12:10 +02:00
}
var msg = {type:type,text:text};
notify.messages.push(msg);
2017-05-09 02:12:10 +02:00
renderMsg(msg);
};
})()