[TASK] frontend improve notify run without gui

This commit is contained in:
Martin Geno 2017-05-09 18:05:55 +02:00
parent 85ba20ba4f
commit 3e1242c5cf
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
3 changed files with 32 additions and 32 deletions

View File

@ -1,8 +1,6 @@
var gui = {}; var gui = {};
(function(){ (function(){
var notify;
gui.render = function render(){ gui.render = function render(){
var status = document.getElementsByClassName('status')[0]; var status = document.getElementsByClassName('status')[0];
if (status === undefined){ if (status === undefined){
@ -14,13 +12,11 @@ var gui = {};
status.classList.add(((socket.readyState===0 || socket.readyState===2)?'connecting':(socket.readyState===1)?'':'offline')); status.classList.add(((socket.readyState===0 || socket.readyState===2)?'connecting':(socket.readyState===1)?'':'offline'));
} }
notify = Notify(document.getElementsByClassName('notifications')[0]); notify.container = document.getElementsByClassName('notifications')[0];
}; };
gui.update = function update(){ gui.update = function update(){
console.log(store.will()); // console.log(store.will());
};
gui.notify = function notifyWalker(type, text){
notify.notify(type, text);
}; };
gui.render(); gui.render();
})(); })();

View File

@ -1,39 +1,43 @@
var messages = []; var notify = {container:{},messages:[]};
function Notify(container){
/* (function(){
var container = document.createElement('div');
container.classList.add('messages');
el.appendChild(container);
*/
console.log("init notify",container);
if ("Notification" in window) { if ("Notification" in window) {
Notification.requestPermission(); Notification.requestPermission();
} }
function removeLast (){
notify.messages.splice(0, 1);
if(notify.container.firstElementChild)
notify.container.removeChild(notify.container.firstElementChild);
}
function renderMsg(msg){ function renderMsg(msg){
var msgBox = document.createElement('div'); var msgBox = document.createElement('div');
msgBox.classList.add("notify",msg.type); msgBox.classList.add("notify",msg.type);
msgBox.innerHTML = msg.text; msgBox.innerHTML = msg.text;
container.appendChild(msgBox); notify.container.appendChild(msgBox);
msgBox.addEventListener('click', function(){ msgBox.addEventListener('click', function(){
container.removeChild(msgBox); notify.container.removeChild(msgBox);
if (messages.indexOf(msg) !== -1) { if (notify.messages.indexOf(msg) !== -1) {
messages.splice(messages.indexOf(msg), 1); notify.messages.splice(notify.messages.indexOf(msg), 1);
} }
}); });
} }
self.notify = function(type, text){
setInterval(removeLast,15000);
notify.send = function(type, text){
if("Notification" in window && Notification.permission === "granted") { if("Notification" in window && Notification.permission === "granted") {
n = new Notification(text,{body:type,icon:'/img/logo.jpg'}); new Notification(text,{body:type,icon:'/img/logo.jpg'});
return; return;
} }
var msg = {type:type,text:text}; if(notify.messages.length > 10){
if(messages.length > 10){ removeLast();
messages.splice(0, 1);
container.removeChild(container.firstElementChild);
} }
messages.push(msg); var msg = {type:type,text:text};
notify.messages.push(msg);
renderMsg(msg); renderMsg(msg);
}; };
return self;
} })()

View File

@ -5,7 +5,7 @@ var socket = {readyState:0};
function onerror(err) { function onerror(err) {
console.warn(err); console.warn(err);
if(socket.readyState !== 3){ if(socket.readyState !== 3){
gui.notify("error","Es gibt Übertragungsprobleme!"); notify.send("error","Es gibt Übertragungsprobleme!");
gui.render(); gui.render();
} }
} }
@ -31,7 +31,7 @@ var socket = {readyState:0};
function onclose(){ function onclose(){
console.log("socket closed by server"); console.log("socket closed by server");
gui.notify("warn","Es besteht ein Verbindungsproblem!"); notify.send("warn","Es besteht ein Verbindungsproblem!");
gui.render(); gui.render();
setTimeout(connect, 5000); setTimeout(connect, 5000);
} }
@ -40,9 +40,9 @@ var socket = {readyState:0};
var msg = {node:node}; var msg = {node:node};
var string = JSON.stringify(msg); var string = JSON.stringify(msg);
if(socket.send(string)){ if(socket.send(string)){
gui.notify("success","Node '"+node.node_id+"' mit neuen Werten wurde gespeichert."); notify.send("success","Node '"+node.node_id+"' mit neuen Werten wurde gespeichert.");
}else{ }else{
gui.notify("error","Node '"+node.node_id+"' konnte nicht gespeichert werden!"); notify.send("error","Node '"+node.node_id+"' konnte nicht gespeichert werden!");
} }
} }