[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 = {};
(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();
})();

View File

@ -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;
}
})()

View File

@ -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!");
}
}