2017-05-09 20:40:18 +02:00
|
|
|
var notify = {};
|
2017-05-09 02:12:10 +02:00
|
|
|
|
2017-05-09 18:05:55 +02:00
|
|
|
|
|
|
|
(function(){
|
2017-05-09 20:40:18 +02:00
|
|
|
var container;
|
|
|
|
var messages = [];
|
|
|
|
|
2017-05-09 17:45:16 +02:00
|
|
|
if ("Notification" in window) {
|
|
|
|
Notification.requestPermission();
|
|
|
|
}
|
2017-05-09 18:05:55 +02:00
|
|
|
|
|
|
|
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 18:05:55 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 02:12:10 +02:00
|
|
|
function renderMsg(msg){
|
|
|
|
var msgBox = document.createElement('div');
|
2017-05-09 17:45:16 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-05-09 18:05:55 +02:00
|
|
|
|
|
|
|
setInterval(removeLast,15000);
|
|
|
|
|
2017-05-09 20:40:18 +02:00
|
|
|
notify.bind = function(el) {
|
|
|
|
container = el;
|
|
|
|
};
|
|
|
|
|
2017-05-09 18:05:55 +02:00
|
|
|
notify.send = function(type, text){
|
2017-05-09 02:12:10 +02:00
|
|
|
if("Notification" in window && Notification.permission === "granted") {
|
2017-05-09 18:05:55 +02:00
|
|
|
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){
|
2017-05-09 18:05:55 +02:00
|
|
|
removeLast();
|
2017-05-09 02:12:10 +02:00
|
|
|
}
|
2017-05-09 18:05:55 +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);
|
|
|
|
};
|
2017-05-09 18:05:55 +02:00
|
|
|
|
|
|
|
})()
|