freifunkmanager/js/controller/map.js

145 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-07-01 16:01:28 +02:00
define(['leaflet','controller/sidebar','leaflet.label'],function(L,Sidebar){
var data,currentNode;
2016-07-01 08:25:32 +02:00
return function(el,config){
2016-07-01 16:01:28 +02:00
var mapEl = document.createElement("div");
mapEl.classList.add("map");
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
var sideBarEl = document.createElement("div");
var bar = Sidebar(sideBarEl,config);
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
var map = L.map(mapEl,{zoomControl:false}).setView([51.505, -0.09], 13);
var layergeojson,layernodes;
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
L.tileLayer(
2016-07-01 11:16:39 +02:00
config.map.tiles.url,
2016-07-01 16:01:28 +02:00
config.map.tiles.option).addTo(map);
map.setView(config.map.view,config.map.zoom);
2016-07-01 08:25:32 +02:00
var geoJsonOption = {
2016-07-01 11:16:39 +02:00
pointToLayer: function (feature, latlng){
2016-07-01 16:01:28 +02:00
feature.properties.radius = 10;
return L.circleMarker(latlng, feature.properties);
2016-07-01 08:25:32 +02:00
},
onEachFeature: function(feature, layer) {
2016-07-01 11:16:39 +02:00
if(feature.properties.name.length >0)
2016-07-01 16:01:28 +02:00
layer.bindLabel(feature.properties.name);
2016-07-01 08:25:32 +02:00
},
style: function(feature){
if(feature.geometry.type === "LineString" || feature.geometry.type === "Polygon")
return {color: feature.properties.stroke,
opacity:feature.properties["stroke-opacity"],
fillColor: feature.properties.fill,
fillOpacity:feature.properties["fill-opacity"],
stroke: true,
weight: feature.properties["stroke-width"],
lineCap: "round",
2016-07-01 16:01:28 +02:00
lineJoin: "round"};
2016-07-01 08:25:32 +02:00
return {
color: feature.properties["marker-color"],
fillColor: feature.properties["marker-color"],
fillOpacity: 0.2,
weight: 2,
stroke: true
2016-07-01 16:01:28 +02:00
};
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
};
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
nodeIcon = L.divIcon({className: "nodeicon"});
2016-07-01 08:25:32 +02:00
render = function(){
if(layernodes)
2016-07-01 16:01:28 +02:00
layernodes.clearLayers();
2016-07-01 08:25:32 +02:00
if(layergeojson)
2016-07-01 16:01:28 +02:00
layergeojson.clearLayers();
if(data.geojson && Object.keys(data.geojson).length > 0){
layergeojson = L.geoJson(data.geojson,geoJsonOption).addTo(map);
map._onResize();
2016-07-01 08:25:32 +02:00
}
var nodes = Object.keys(data.nodes).filter(function(key){
2016-07-01 16:01:28 +02:00
return data.nodes[key].nodeinfo && data.nodes[key].nodeinfo.location;
2016-07-01 08:25:32 +02:00
}).map(function(key){
2016-07-01 16:01:28 +02:00
var node = data.nodes[key];
var alias = data.aliases[key];
if(alias && alias.location)
pos = [alias.location.latitude,alias.location.longitude];
else
pos = [node.nodeinfo.location.latitude,node.nodeinfo.location.longitude];
var m = L.marker(pos,{
2016-07-01 08:25:32 +02:00
icon: nodeIcon
2016-07-01 16:01:28 +02:00
});
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
var className = "nodeicon";
if(node.flags && !node.flags.online)
className += " offline";
var wifi24="-",wifi5="-",ch24="-",ch5="-",tx24="-",tx5="-";
if(node.statistics && node.statistics.clients){
wifi24 = node.statistics.clients.wifi24;
2016-07-01 11:16:39 +02:00
if(wifi24 < config.map.icon.warn.wifi24 && wifi24 > 0)
2016-07-01 16:01:28 +02:00
className += " client24";
2016-07-01 11:16:39 +02:00
else if(wifi24 < config.map.icon.crit.wifi24 && wifi24 >= config.map.icon.warn.wifi24)
2016-07-01 16:01:28 +02:00
className += ' client24-warn';
2016-07-01 11:16:39 +02:00
else if(wifi24 >= config.map.icon.crit.wifi24)
2016-07-01 16:01:28 +02:00
className += ' client24-crit';
2016-07-01 08:25:32 +02:00
2016-07-01 16:01:28 +02:00
wifi5 = node.statistics.clients.wifi5;
2016-07-01 11:16:39 +02:00
if(config.map.icon.warn.wifi5 < 20 && wifi5 > 0)
2016-07-01 16:01:28 +02:00
className += ' client5';
2016-07-01 11:16:39 +02:00
else if(wifi5 < config.map.icon.crit.wifi5 && wifi5 >= config.map.icon.warn.wifi5)
2016-07-01 16:01:28 +02:00
className += ' client5-warn';
2016-07-01 11:16:39 +02:00
else if(wifi5 >= config.map.icon.crit.wifi5)
2016-07-01 16:01:28 +02:00
className += ' client5-crit';
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
if(node.nodeinfo.wireless){
ch24 = (alias && alias.wireless && alias.wireless.channel24)?alias.wireless.channel24:((node.nodeinfo.wireless.channel24)?node.nodeinfo.wireless.channel24:'-');
ch5 = (alias && alias.wireless && alias.wireless.channel5)?alias.wireless.channel5:((node.nodeinfo.wireless.channel5)?node.nodeinfo.wireless.channel5:'-');
tx24 = (alias && alias.wireless && alias.wireless.txpower24)?alias.wireless.txpower24:((node.nodeinfo.wireless.txpower24)?node.nodeinfo.wireless.txpower24:'-');
tx5 = (alias && alias.wireless && alias.wireless.txpower5)?alias.wireless.txpower5:((node.nodeinfo.wireless.txpower5)?node.nodeinfo.wireless.txpower5:'-');
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
m.bindLabel(((alias && alias.hostname)?alias.hostname:node.nodeinfo.hostname)+" <div class=\"nodeicon-label\">("+key+")"+
2016-07-01 11:16:39 +02:00
"<table><tr><th></th><th>Cl</th><th>Ch</th><th>Tx</th></tr>"+
"<tr><td>2.4G</td><td>"+wifi24+"</td><td>"+ch24+"</td><td>"+tx24+"</td></tr>"+
"<tr><td>5G</td><td>"+wifi5+"</td><td>"+ch5+"</td><td>"+tx5+"</td></tr>"+
2016-07-01 08:25:32 +02:00
"</table>"+
"</div>"
2016-07-01 16:01:28 +02:00
);
2016-07-01 08:25:32 +02:00
if(currentNode && currentNode == key){
2016-07-01 16:01:28 +02:00
className += ' select';
map.setView(m.getLatLng(),config.map.zoom);
m.options.draggable = true;
bar.setSelected(key,m,function(){
m.dragging.disable();
m._icon.classList.remove("select");
});
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
m.setIcon(L.divIcon({className: className}));
2016-07-01 08:25:32 +02:00
m.on("click", function(){
2016-07-01 16:01:28 +02:00
window.location.href = "#map/"+key;
currentNode = key;
render();
});
return m;
});
layernodes = L.featureGroup(nodes).addTo(map);
};
2016-07-01 08:25:32 +02:00
return {
storageNotify: function(d){
2016-07-01 16:01:28 +02:00
data = d;
bar.storageNotify(d);
render();
2016-07-01 08:25:32 +02:00
},
controller: function(){
2016-07-01 16:01:28 +02:00
currentNode = arguments[0];
el.innerHTML = "";
el.appendChild(sideBarEl);
el.appendChild(mapEl);
2016-07-01 08:25:32 +02:00
}
2016-07-01 16:01:28 +02:00
};
};
});