meshviewer/lib/map.js

515 lines
14 KiB
JavaScript
Raw Normal View History

2015-04-17 21:54:19 +00:00
define(["map/clientlayer", "map/labelslayer",
"d3", "leaflet", "moment", "locationmarker", "rbush",
2015-04-17 20:27:24 +00:00
"leaflet.label", "leaflet.providers"],
function (ClientLayer, LabelsLayer, d3, L, moment, LocationMarker, rbush) {
2015-04-17 20:10:47 +00:00
var options = { worldCopyJump: true,
zoomControl: false
}
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
var AddLayerButton = L.Control.extend({
options: {
position: "bottomright"
},
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
initialize: function (f, options) {
L.Util.setOptions(this, options)
this.f = f
},
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
onAdd: function () {
var button = L.DomUtil.create("button", "add-layer")
button.textContent = ""
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
L.DomEvent.disableClickPropagation(button)
L.DomEvent.addListener(button, "click", this.f, this)
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
this.button = button
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
return button
}
})
2015-04-12 01:24:46 +00:00
2015-04-17 20:10:47 +00:00
var LocateButton = L.Control.extend({
options: {
position: "bottomright"
},
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
active: false,
button: undefined,
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
initialize: function (f, options) {
L.Util.setOptions(this, options)
this.f = f
},
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
onAdd: function () {
var button = L.DomUtil.create("button", "locate-user")
button.textContent = ""
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
L.DomEvent.disableClickPropagation(button)
L.DomEvent.addListener(button, "click", this.onClick, this)
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
this.button = button
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
return button
},
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
update: function() {
this.button.classList.toggle("active", this.active)
},
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
set: function(v) {
this.active = v
this.update()
},
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
onClick: function () {
this.f(!this.active)
}
})
2015-04-04 17:08:28 +00:00
2015-11-09 09:01:20 +00:00
var CoordsPickerButton = L.Control.extend({
options: {
position: "bottomright"
},
active: false,
button: undefined,
initialize: function (f, options) {
L.Util.setOptions(this, options)
this.f = f
},
onAdd: function () {
var button = L.DomUtil.create("button", "coord-picker")
button.textContent = ""
L.DomEvent.disableClickPropagation(button)
L.DomEvent.addListener(button, "click", this.onClick, this)
this.button = button
return button
},
update: function() {
this.button.classList.toggle("active", this.active)
},
set: function(v) {
this.active = v
this.update()
},
onClick: function () {
this.f(!this.active)
}
})
2015-04-17 20:10:47 +00:00
function mkMarker(dict, iconFunc, router) {
return function (d) {
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
m.resetStyle = function () {
m.setStyle(iconFunc(d))
}
2015-03-25 01:15:17 +00:00
2015-04-17 20:10:47 +00:00
m.on("click", router.node(d))
m.bindLabel(d.nodeinfo.hostname)
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
dict[d.nodeinfo.node_id] = m
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
return m
}
}
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
function addLinksToMap(dict, linkScale, graph, router) {
graph = graph.filter( function (d) {
return "distance" in d && !d.vpn
})
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var lines = graph.map( function (d) {
var opts = { color: linkScale(d.tq).hex(),
weight: 4,
opacity: 0.5,
dashArray: "none"
}
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var line = L.polyline(d.latlngs, opts)
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
line.resetStyle = function () {
line.setStyle(opts)
}
2015-03-25 01:15:17 +00:00
2015-04-17 20:10:47 +00:00
line.bindLabel(d.source.node.nodeinfo.hostname + " " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
line.on("click", router.link(d))
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
dict[d.id] = line
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
return line
})
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
return lines
}
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var iconOnline = { color: "#1566A9", fillColor: "#1566A9", radius: 6, fillOpacity: 0.5, opacity: 0.5, weight: 2, className: "stroke-first" }
var iconOffline = { color: "#D43E2A", fillColor: "#D43E2A", radius: 3, fillOpacity: 0.5, opacity: 0.5, weight: 1, className: "stroke-first" }
var iconLost = { color: "#D43E2A", fillColor: "#D43E2A", radius: 6, fillOpacity: 0.8, opacity: 0.8, weight: 1, className: "stroke-first" }
var iconAlert = { color: "#D43E2A", fillColor: "#D43E2A", radius: 6, fillOpacity: 0.8, opacity: 0.8, weight: 2, className: "stroke-first node-alert" }
var iconNew = { color: "#1566A9", fillColor: "#93E929", radius: 6, fillOpacity: 1.0, opacity: 0.5, weight: 2 }
2015-03-24 23:54:00 +00:00
return function (config, linkScale, sidebar, router, buttons) {
2015-04-17 20:10:47 +00:00
var self = this
var barycenter
var groupOnline, groupOffline, groupNew, groupLost, groupLines
var savedView
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var map, userLocation
var layerControl
var customLayers = new Set()
2015-07-07 14:36:19 +00:00
var baseLayers = {}
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
var locateUserButton = new LocateButton(function (d) {
if (d)
enableTracking()
else
disableTracking()
})
2015-04-04 17:08:28 +00:00
var mybuttons = []
function addButton(button) {
var el = button.onAdd()
mybuttons.push(el)
buttons.appendChild(el)
}
function clearButtons() {
mybuttons.forEach( function (d) {
buttons.removeChild(d)
})
}
2015-11-09 09:01:20 +00:00
var showCoordsPickerButton = new CoordsPickerButton(function (d) {
if (d)
enableCoords()
else
disableCoords()
})
function saveView() {
savedView = {center: map.getCenter(),
zoom: map.getZoom()}
}
2015-04-17 20:10:47 +00:00
function enableTracking() {
map.locate({watch: true,
enableHighAccuracy: true,
setView: true
})
locateUserButton.set(true)
}
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
function disableTracking() {
map.stopLocate()
locationError()
locateUserButton.set(false)
}
2015-04-04 17:08:28 +00:00
2015-11-09 09:01:20 +00:00
function enableCoords() {
map.getContainer().classList.add("pick-coordinates")
map.on("click", showCoordinates)
showCoordsPickerButton.set(true)
}
function disableCoords() {
map.getContainer().classList.remove("pick-coordinates")
map.off("click", showCoordinates)
showCoordsPickerButton.set(false)
}
function showCoordinates(e) {
window.prompt("Koordinaten (Lat, Lng)", e.latlng.lat.toFixed(6) + ", " + e.latlng.lng.toFixed(6))
disableCoords()
}
2015-04-17 20:10:47 +00:00
function locationFound(e) {
if (!userLocation)
userLocation = new LocationMarker(e.latlng).addTo(map)
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
userLocation.setLatLng(e.latlng)
userLocation.setAccuracy(e.accuracy)
}
2015-04-04 17:08:28 +00:00
2015-04-17 20:10:47 +00:00
function locationError() {
if (userLocation) {
map.removeLayer(userLocation)
userLocation = null
}
2015-04-04 17:08:28 +00:00
}
2015-04-17 20:10:47 +00:00
function addLayer(layerName) {
2015-07-07 14:36:19 +00:00
if (layerName in baseLayers)
return
2015-04-17 20:10:47 +00:00
if (customLayers.has(layerName))
return
2015-04-15 20:25:22 +00:00
2015-04-17 20:10:47 +00:00
try {
var layer = L.tileLayer.provider(layerName)
layerControl.addBaseLayer(layer, layerName)
customLayers.add(layerName)
2015-04-15 20:25:22 +00:00
2015-04-17 20:10:47 +00:00
if (localStorageTest())
localStorage.setItem("map/customLayers", JSON.stringify(Array.from(customLayers)))
} catch (e) {
return
}
2015-04-15 20:25:22 +00:00
}
2015-04-17 20:10:47 +00:00
var el = document.createElement("div")
el.classList.add("map")
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
map = L.map(el, options)
2015-03-24 23:54:00 +00:00
2015-07-07 14:36:19 +00:00
var layers = config.mapLayers.map( function (d) {
return {
"name": d.name,
"layer": "url" in d ? L.tileLayer(d.url, d.config) : L.tileLayer.provider(d.name)
}
})
layers[0].layer.addTo(map)
layers.forEach( function (d) {
baseLayers[d.name] = d.layer
})
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
map.on("locationfound", locationFound)
map.on("locationerror", locationError)
map.on("dragend", saveView)
2015-04-04 17:08:28 +00:00
addButton(locateUserButton)
2015-11-09 09:01:20 +00:00
addButton(showCoordsPickerButton)
2015-04-04 17:08:28 +00:00
addButton(new AddLayerButton(function () {
2015-04-17 20:10:47 +00:00
/*eslint no-alert:0*/
var layerName = prompt("Leaflet Provider:")
addLayer(layerName)
}))
2015-04-12 01:24:46 +00:00
2015-07-07 14:36:19 +00:00
layerControl = L.control.layers(baseLayers, [], {position: "bottomright"})
2015-07-07 19:42:55 +00:00
layerControl.addTo(map)
2015-04-15 20:25:22 +00:00
2015-04-17 20:10:47 +00:00
if (localStorageTest()) {
2015-07-07 14:36:19 +00:00
var d = JSON.parse(localStorage.getItem("map/customLayers"))
2015-04-15 20:25:22 +00:00
2015-07-07 14:36:19 +00:00
if (d)
d.forEach(addLayer)
2015-04-17 20:10:47 +00:00
}
2015-04-15 20:25:22 +00:00
var clientLayer = new ClientLayer({minZoom: 15})
2015-04-17 20:10:47 +00:00
clientLayer.addTo(map)
clientLayer.setZIndex(5)
2015-04-16 23:02:56 +00:00
2015-04-20 00:51:27 +00:00
var labelsLayer = new LabelsLayer()
2015-04-17 21:54:19 +00:00
labelsLayer.addTo(map)
labelsLayer.setZIndex(6)
2015-04-17 20:10:47 +00:00
var nodeDict = {}
var linkDict = {}
var highlight
2015-04-17 20:10:47 +00:00
function resetMarkerStyles(nodes, links) {
Object.keys(nodes).forEach( function (d) {
nodes[d].resetStyle()
})
2015-04-17 20:10:47 +00:00
Object.keys(links).forEach( function (d) {
links[d].resetStyle()
})
}
2015-04-17 20:10:47 +00:00
function setView(bounds) {
map.fitBounds(bounds, {paddingTopLeft: [sidebar(), 0]})
2015-04-17 20:10:47 +00:00
}
2015-04-17 20:10:47 +00:00
function resetZoom() {
2015-07-07 19:42:55 +00:00
if (barycenter)
setView(barycenter.getBounds())
2015-04-17 20:10:47 +00:00
}
2015-04-17 20:10:47 +00:00
function goto(m) {
var bounds
2015-04-17 20:10:47 +00:00
if ("getBounds" in m)
bounds = m.getBounds()
else
bounds = L.latLngBounds([m.getLatLng()])
2015-04-17 20:10:47 +00:00
setView(bounds)
2015-04-17 20:10:47 +00:00
return m
}
2015-04-17 20:10:47 +00:00
function updateView(nopanzoom) {
resetMarkerStyles(nodeDict, linkDict)
var m
2015-04-17 20:10:47 +00:00
if (highlight !== undefined)
if (highlight.type === "node") {
m = nodeDict[highlight.o.nodeinfo.node_id]
2015-04-17 20:10:47 +00:00
if (m)
m.setStyle({ color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7, className: "stroke-first" })
} else if (highlight.type === "link") {
m = linkDict[highlight.o.id]
2015-04-17 20:10:47 +00:00
if (m)
m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
}
if (!nopanzoom)
if (m)
2015-04-17 20:10:47 +00:00
goto(m)
else if (savedView)
map.setView(savedView.center, savedView.zoom)
2015-04-17 20:10:47 +00:00
else
resetZoom()
}
2015-04-17 20:10:47 +00:00
function calcBarycenter(nodes) {
nodes = nodes.map(function (d) { return d.nodeinfo.location })
2015-07-07 19:42:55 +00:00
if (nodes.length === 0)
return undefined
2015-04-17 20:10:47 +00:00
var lats = nodes.map(function (d) { return d.latitude })
var lngs = nodes.map(function (d) { return d.longitude })
2015-03-25 00:16:15 +00:00
2015-04-17 20:10:47 +00:00
var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
2015-07-07 19:28:44 +00:00
if (barycenterDev[0] === undefined)
barycenterDev[0] = 0
if (barycenterDev[1] === undefined)
barycenterDev[1] = 0
2015-04-17 20:10:47 +00:00
var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
barycenter.lng + barycenterDev[1])
2015-04-17 20:10:47 +00:00
var r = barycenter.distanceTo(barycenterCircle)
2015-04-17 20:10:47 +00:00
return L.circle(barycenter, r * config.mapSigmaScale)
}
function mapRTree(d) {
var o = [ d.nodeinfo.location.latitude, d.nodeinfo.location.longitude,
d.nodeinfo.location.latitude, d.nodeinfo.location.longitude]
o.node = d
return o
}
2015-04-17 20:10:47 +00:00
self.setData = function (data) {
nodeDict = {}
linkDict = {}
2015-04-17 20:10:47 +00:00
if (groupOffline)
groupOffline.clearLayers()
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
if (groupOnline)
groupOnline.clearLayers()
2015-04-02 22:02:00 +00:00
2015-04-17 20:10:47 +00:00
if (groupNew)
groupNew.clearLayers()
2015-04-02 22:02:00 +00:00
2015-04-17 20:10:47 +00:00
if (groupLost)
groupLost.clearLayers()
2015-04-02 22:02:00 +00:00
2015-04-17 20:10:47 +00:00
if (groupLines)
groupLines.clearLayers()
2015-04-02 22:02:00 +00:00
2015-04-17 20:10:47 +00:00
var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
groupLines = L.featureGroup(lines).addTo(map)
2015-04-02 22:02:00 +00:00
2015-04-17 20:10:47 +00:00
barycenter = calcBarycenter(data.nodes.all.filter(has_location))
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
2015-04-17 20:10:47 +00:00
var markersOnline = nodesOnline.filter(has_location)
.map(mkMarker(nodeDict, function () { return iconOnline }, router))
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var markersOffline = nodesOffline.filter(has_location)
.map(mkMarker(nodeDict, function () { return iconOffline }, router))
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
var markersNew = data.nodes.new.filter(has_location)
.map(mkMarker(nodeDict, function () { return iconNew }, router))
2015-03-25 09:29:41 +00:00
2015-04-17 20:10:47 +00:00
var markersLost = data.nodes.lost.filter(has_location)
.map(mkMarker(nodeDict, function (d) {
if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
return iconAlert
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
return iconLost
}, router))
2015-04-17 20:10:47 +00:00
groupOffline = L.featureGroup(markersOffline).addTo(map)
groupOnline = L.featureGroup(markersOnline).addTo(map)
groupLost = L.featureGroup(markersLost).addTo(map)
groupNew = L.featureGroup(markersNew).addTo(map)
var rtreeOnlineAll = rbush(9)
rtreeOnlineAll.load(data.nodes.all.filter(online).filter(has_location).map(mapRTree))
clientLayer.setData(rtreeOnlineAll)
2015-04-20 00:51:27 +00:00
labelsLayer.setData({online: nodesOnline.filter(has_location),
offline: nodesOffline.filter(has_location),
new: data.nodes.new.filter(has_location),
lost: data.nodes.lost.filter(has_location)
2015-04-17 21:54:19 +00:00
})
2015-03-29 14:14:10 +00:00
2015-04-17 20:10:47 +00:00
updateView(true)
}
2015-04-16 23:02:56 +00:00
2015-04-17 20:10:47 +00:00
self.resetView = function () {
disableTracking()
highlight = undefined
updateView()
}
2015-03-29 14:14:10 +00:00
2015-04-17 20:10:47 +00:00
self.gotoNode = function (d) {
disableTracking()
highlight = {type: "node", o: d}
updateView()
}
2015-03-25 00:16:15 +00:00
2015-04-17 20:10:47 +00:00
self.gotoLink = function (d) {
disableTracking()
highlight = {type: "link", o: d}
updateView()
}
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
self.destroy = function () {
clearButtons()
2015-04-17 20:10:47 +00:00
map.remove()
2015-07-11 22:11:18 +00:00
if (el.parentNode)
el.parentNode.removeChild(el)
}
self.render = function (d) {
d.appendChild(el)
2015-07-11 22:42:24 +00:00
map.invalidateSize()
2015-04-17 20:10:47 +00:00
}
2015-03-24 23:54:00 +00:00
2015-04-17 20:10:47 +00:00
return self
}
2015-03-24 23:54:00 +00:00
})