From 3e1242c5cf50b1296124089d96d6a8b6a5832886 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Tue, 9 May 2017 18:05:55 +0200 Subject: [PATCH] [TASK] frontend improve notify run without gui --- webroot/js/gui.js | 10 +++------- webroot/js/notify.js | 46 ++++++++++++++++++++++++-------------------- webroot/js/socket.js | 8 ++++---- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/webroot/js/gui.js b/webroot/js/gui.js index 6390a8a..c3cb321 100644 --- a/webroot/js/gui.js +++ b/webroot/js/gui.js @@ -1,8 +1,6 @@ var gui = {}; (function(){ - var notify; - gui.render = function render(){ var status = document.getElementsByClassName('status')[0]; if (status === undefined){ @@ -14,13 +12,11 @@ var gui = {}; 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(){ - console.log(store.will()); - }; - gui.notify = function notifyWalker(type, text){ - notify.notify(type, text); + // console.log(store.will()); }; + gui.render(); })(); diff --git a/webroot/js/notify.js b/webroot/js/notify.js index a7f064f..cb1e6b4 100644 --- a/webroot/js/notify.js +++ b/webroot/js/notify.js @@ -1,39 +1,43 @@ -var messages = []; +var notify = {container:{},messages:[]}; -function Notify(container){ - /* - var container = document.createElement('div'); - container.classList.add('messages'); - el.appendChild(container); - */ - console.log("init notify",container); + +(function(){ if ("Notification" in window) { Notification.requestPermission(); } + + function removeLast (){ + notify.messages.splice(0, 1); + if(notify.container.firstElementChild) + notify.container.removeChild(notify.container.firstElementChild); + } + function renderMsg(msg){ var msgBox = document.createElement('div'); msgBox.classList.add("notify",msg.type); msgBox.innerHTML = msg.text; - container.appendChild(msgBox); + notify.container.appendChild(msgBox); msgBox.addEventListener('click', function(){ - container.removeChild(msgBox); - if (messages.indexOf(msg) !== -1) { - messages.splice(messages.indexOf(msg), 1); + notify.container.removeChild(msgBox); + if (notify.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") { - n = new Notification(text,{body:type,icon:'/img/logo.jpg'}); + new Notification(text,{body:type,icon:'/img/logo.jpg'}); return; } - var msg = {type:type,text:text}; - if(messages.length > 10){ - messages.splice(0, 1); - container.removeChild(container.firstElementChild); + if(notify.messages.length > 10){ + removeLast(); } - messages.push(msg); + var msg = {type:type,text:text}; + notify.messages.push(msg); renderMsg(msg); }; - return self; -} + +})() diff --git a/webroot/js/socket.js b/webroot/js/socket.js index 09bf880..9b5ed59 100644 --- a/webroot/js/socket.js +++ b/webroot/js/socket.js @@ -5,7 +5,7 @@ var socket = {readyState:0}; function onerror(err) { console.warn(err); if(socket.readyState !== 3){ - gui.notify("error","Es gibt Übertragungsprobleme!"); + notify.send("error","Es gibt Übertragungsprobleme!"); gui.render(); } } @@ -31,7 +31,7 @@ var socket = {readyState:0}; function onclose(){ console.log("socket closed by server"); - gui.notify("warn","Es besteht ein Verbindungsproblem!"); + notify.send("warn","Es besteht ein Verbindungsproblem!"); gui.render(); setTimeout(connect, 5000); } @@ -40,9 +40,9 @@ var socket = {readyState:0}; var msg = {node:node}; var string = JSON.stringify(msg); 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{ - gui.notify("error","Node '"+node.node_id+"' konnte nicht gespeichert werden!"); + notify.send("error","Node '"+node.node_id+"' konnte nicht gespeichert werden!"); } }