freifunkmanager/js/helper/storage.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-07-01 08:25:32 +02:00
define(["helper/lib","moment"],function(lib,moment){
return function(config){
2016-07-01 16:01:28 +02:00
var data = {nodes:{},aliases:{},geojson:{}};
var notifies = [], notifiesNew = [];
if(localStorageTest()){
data.nodes = JSON.parse(localStorage.getItem("nodes"));
data.aliases = JSON.parse(localStorage.getItem("aliases"));
}
2016-07-01 08:25:32 +02:00
var notify = function(){
for(var i=0; i<notifies.length;i++)
2016-07-01 16:01:28 +02:00
notifies[i].storageNotify(data);
};
2016-07-01 08:25:32 +02:00
var notifyNew = function(key,data){
for(var i=0; i<notifiesNew.length;i++)
2016-07-01 16:01:28 +02:00
notifiesNew[i].storageNotifyNew(key,data);
};
2016-07-01 08:25:32 +02:00
var refresh = function(){
2016-07-01 16:01:28 +02:00
if(config.map.geojson)
send('GET',config.map.geojson).then(function(d){
data.geojson = d;
notify();
});
2016-07-01 08:25:32 +02:00
send('GET',config.api+"/aliases").then(function(d){
2016-07-01 16:01:28 +02:00
data.aliases = d;
localStorage.setItem("aliases",JSON.stringify(data.aliases));
notify();
});
2016-07-01 08:25:32 +02:00
send('GET',config.api+"/nodes").then(function(d){
Object.keys(d).map(function(key){
2016-07-01 16:01:28 +02:00
if(data.nodes === undefined && data.nodes[key] === undefined){
notifyNew(key,data[key]);
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
data.nodes[key] = d[key];
});
data.lastload = new Date();
localStorage.setItem("nodes",JSON.stringify(data.nodes));
notify();
});
};
2016-07-01 08:25:32 +02:00
return {
setTimeSinceUpdate:function(str){
2016-07-01 16:01:28 +02:00
var interval;
2016-07-01 08:25:32 +02:00
clearInterval(interval);
interval = setInterval(function(){
2016-07-01 16:01:28 +02:00
str.innerHTML = moment().diff(data.lastload,"seconds")+" sec";
2016-07-01 08:25:32 +02:00
}, 100);
},
addNotify: function(handler){
2016-07-01 16:01:28 +02:00
notifies.push(handler);
2016-07-01 08:25:32 +02:00
},
addNotifyNew: function(handler){
2016-07-01 16:01:28 +02:00
notifiesNew.push(handler);
2016-07-01 08:25:32 +02:00
},
autorefresh: function(timer){
2016-07-01 16:01:28 +02:00
var interval;
2016-07-01 08:25:32 +02:00
clearInterval(interval);
interval = setInterval(function(){
2016-07-01 16:01:28 +02:00
refresh();
2016-07-01 08:25:32 +02:00
}, timer);
},
refresh: refresh
2016-07-01 16:01:28 +02:00
};
};
});