meshviewer/lib/map.js

176 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-03-24 23:54:00 +00:00
define(function () {
var options = { worldCopyJump: true,
zoomControl: false
}
2015-03-25 18:45:21 +00:00
function mkMarker(dict, iconFunc, router) {
2015-03-24 23:54:00 +00:00
return function (d) {
2015-03-25 00:27:05 +00:00
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
2015-03-24 23:54:00 +00:00
2015-03-25 01:15:17 +00:00
m.resetStyle = function () {
m.setStyle(iconFunc(d))
}
2015-03-25 19:55:49 +00:00
m.on('click', router.node(d))
2015-03-25 01:14:56 +00:00
m.bindLabel(d.nodeinfo.hostname)
2015-03-24 23:54:00 +00:00
dict[d.nodeinfo.node_id] = m
return m
}
}
2015-03-25 18:45:21 +00:00
function addLinksToMap(dict, linkScale, graph, router) {
2015-03-24 23:54:00 +00:00
graph = graph.filter( function (d) {
return "distance" in d
})
var lines = graph.map( function (d) {
var opts = { color: linkScale(d.tq).hex(),
2015-03-25 01:15:17 +00:00
weight: 4,
opacity: 0.5,
dashArray: "none"
2015-03-24 23:54:00 +00:00
}
var line = L.polyline(d.latlngs, opts)
2015-03-25 01:15:17 +00:00
line.resetStyle = function () {
line.setStyle(opts)
}
2015-03-25 01:14:56 +00:00
line.bindLabel(d.source.node.nodeinfo.hostname + " " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
2015-03-25 19:55:49 +00:00
line.on('click', router.link(d))
2015-03-24 23:54:00 +00:00
dict[linkId(d)] = line
return line
})
return lines
}
2015-03-25 01:15:17 +00:00
var iconOnline = { color: "#1566A9", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
var iconOffline = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
2015-03-25 09:29:41 +00:00
var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
2015-03-25 01:15:17 +00:00
var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
2015-03-24 23:54:00 +00:00
2015-03-25 01:50:28 +00:00
var groupOnline, group
2015-03-25 18:45:21 +00:00
return function (linkScale, sidebar, router) {
2015-03-24 23:54:00 +00:00
var self = this
var el = document.createElement("div")
el.classList.add("map")
self.div = el
var map = L.map(el, options)
L.control.zoom({ position: "topright" }).addTo(map)
L.tileLayer("https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
subdomains: "1234",
type: "osm",
attribution: "Map data Tiles &copy; <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a> <img src=\"https://developer.mapquest.com/content/osm/mq_logo.png\" />, Map data © OpenStreetMap contributors, CC-BY-SA",
maxZoom: 18
}).addTo(map)
2015-03-25 00:16:15 +00:00
var nodeDict = {}
var linkDict = {}
2015-03-25 15:36:24 +00:00
self.setData = function (now, newnodes, lostnodes, onlinenodes, links) {
2015-03-25 00:16:15 +00:00
nodeDict = {}
linkDict = {}
2015-03-24 23:54:00 +00:00
2015-03-25 18:45:21 +00:00
var lines = addLinksToMap(linkDict, linkScale, links, router)
2015-03-24 23:54:00 +00:00
var nodes = newnodes.concat(lostnodes).filter(has_location)
2015-03-25 00:16:15 +00:00
var markers = nodes.map(mkMarker(nodeDict, function (d) {
2015-03-24 23:54:00 +00:00
if (d.flags.online)
return iconNew
2015-03-25 09:29:41 +00:00
if (d.lastseen.isAfter(moment(now).subtract(1, 'days')))
return iconAlert
2015-03-25 00:27:05 +00:00
return iconOffline
2015-03-25 18:45:21 +00:00
}, router))
2015-03-24 23:54:00 +00:00
var onlinemarkers = subtract(onlinenodes.filter(has_location), newnodes)
2015-03-25 18:45:21 +00:00
.map(mkMarker(nodeDict, function (d) { return iconOnline }, router))
2015-03-24 23:54:00 +00:00
var groupLines = L.featureGroup(lines).addTo(map)
2015-03-25 01:50:28 +00:00
groupOnline = L.featureGroup(onlinemarkers).addTo(map)
group = L.featureGroup(markers).addTo(map)
resetView()
}
function resetView() {
resetMarkerStyles(nodeDict, linkDict)
2015-03-24 23:54:00 +00:00
var bounds = group.getBounds()
if (!bounds.isValid())
bounds = groupOnline.getBounds()
if (bounds.isValid())
2015-03-25 00:16:15 +00:00
setView(bounds)
}
function setView(bounds) {
map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
}
2015-03-24 23:54:00 +00:00
2015-03-25 01:15:17 +00:00
function resetMarkerStyles(nodes, links) {
Object.keys(nodes).forEach( function (d) {
nodes[d].resetStyle()
})
Object.keys(links).forEach( function (d) {
links[d].resetStyle()
})
}
2015-03-25 00:16:15 +00:00
function goto(dict, id) {
var m = dict[id]
if (m === undefined)
return
2015-03-24 23:54:00 +00:00
2015-03-25 00:16:15 +00:00
var bounds
2015-03-24 23:54:00 +00:00
2015-03-25 00:16:15 +00:00
if ("getBounds" in m)
bounds = m.getBounds()
else
bounds = L.latLngBounds([m.getLatLng()])
2015-03-24 23:54:00 +00:00
2015-03-25 00:16:15 +00:00
setView(bounds)
2015-03-25 01:15:17 +00:00
return m
2015-03-25 00:16:15 +00:00
}
2015-03-25 01:50:28 +00:00
self.resetView = resetView
2015-03-25 00:16:15 +00:00
self.gotoNode = function (d) {
2015-03-25 01:15:17 +00:00
resetMarkerStyles(nodeDict, linkDict)
var m = goto(nodeDict, d.nodeinfo.node_id)
if (m)
m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
2015-03-25 01:50:28 +00:00
else
resetView()
2015-03-25 00:16:15 +00:00
}
2015-03-24 23:54:00 +00:00
2015-03-25 00:16:15 +00:00
self.gotoLink = function (d) {
2015-03-25 01:15:17 +00:00
resetMarkerStyles(nodeDict, linkDict)
var m = goto(linkDict, linkId(d))
if (m)
m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
2015-03-25 01:50:28 +00:00
else
resetView()
2015-03-24 23:54:00 +00:00
}
return self
}
})