freifunkmanager/webroot/js/notify.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-05-09 20:40:18 +02:00
var notify = {};
2017-05-09 02:12:10 +02:00
(function(){
2017-05-09 20:40:18 +02:00
var container;
var messages = [];
if ("Notification" in window) {
Notification.requestPermission();
}
function removeLast (){
2017-05-09 20:40:18 +02:00
messages.splice(0, 1);
if(container!==undefined && container.firstElementChild)
container.removeChild(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;
2017-05-09 20:40:18 +02:00
container.appendChild(msgBox);
2017-05-09 02:12:10 +02:00
msgBox.addEventListener('click', function(){
2017-05-09 20:40:18 +02:00
container.removeChild(msgBox);
if (messages.indexOf(msg) !== -1) {
messages.splice(messages.indexOf(msg), 1);
2017-05-09 02:12:10 +02:00
}
});
}
setInterval(removeLast,15000);
2017-05-09 20:40:18 +02:00
notify.bind = function(el) {
container = el;
};
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;
}
2017-05-09 20:40:18 +02:00
if(messages.length > 10){
removeLast();
2017-05-09 02:12:10 +02:00
}
var msg = {type:type,text:text};
2017-05-09 20:40:18 +02:00
messages.push(msg);
2017-05-09 02:12:10 +02:00
renderMsg(msg);
};
})()