freifunkmanager/webroot/js/notify.js

68 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-05-16 16:00:32 +02:00
/* exported notify */
2017-05-09 02:12:10 +02:00
2017-05-16 19:18:35 +02:00
const notify = {};
2017-05-16 19:18:35 +02:00
(function init () {
'use strict';
const DELAY_OF_NOTIFY = 15000,
MAX_MESSAGE_SHOW = 10,
messages = [];
let container = null;
if ('Notification' in window) {
window.Notification.requestPermission();
}
function removeLast () {
messages.splice(0, 1);
if (container && container.firstElementChild) {
container.removeChild(container.firstElementChild);
}
}
function renderMsg (msg) {
const msgBox = document.createElement('div');
msgBox.classList.add('notify', msg.type);
msgBox.innerHTML = msg.text;
container.appendChild(msgBox);
msgBox.addEventListener('click', () => {
container.removeChild(msgBox);
if (messages.indexOf(msg) !== -1) {
messages.splice(messages.indexOf(msg), 1);
}
});
}
window.setInterval(removeLast, DELAY_OF_NOTIFY);
notify.bind = function bind (el) {
container = el;
};
notify.send = function send (type, text) {
if ('Notification' in window &&
window.Notification.permission === 'granted') {
// eslint-disable-next-line no-new
new window.Notification(text, {
'body': type,
'icon': '/img/logo.jpg'
});
return;
}
if (messages.length > MAX_MESSAGE_SHOW) {
removeLast();
}
const msg = {
'text': text,
'type': type
};
messages.push(msg);
renderMsg(msg);
};
2017-05-16 16:00:32 +02:00
})();