hopglass/lib/map.js

128 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-03-25 00:54:00 +01:00
define(function () {
var options = { worldCopyJump: true,
zoomControl: false
}
function mkMarker(dict, iconFunc, gotoAnything) {
return function (d) {
2015-03-25 01:27:05 +01:00
var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
2015-03-25 00:54:00 +01:00
m.on('click', gotoAnything.node(d, false))
m.bindPopup(d.nodeinfo.hostname)
dict[d.nodeinfo.node_id] = m
return m
}
}
function addLinksToMap(dict, linkScale, graph, gotoAnything) {
graph = graph.filter( function (d) {
return "distance" in d
})
var lines = graph.map( function (d) {
var opts = { color: linkScale(d.tq).hex(),
weight: 4
}
var line = L.polyline(d.latlngs, opts)
line.bindPopup(d.source.node.nodeinfo.hostname + " " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
line.on('click', gotoAnything.link(d, false))
dict[linkId(d)] = line
return line
})
return lines
}
2015-03-25 01:27:05 +01:00
var iconOnline = { color: "#1566A9", radius: 6, fillOpacity: 0.5 }
var iconOffline = { color: "#D43E2A", radius: 6, fillOpacity: 0.5 }
var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5 }
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
return function (sidebar) {
2015-03-25 00:54:00 +01: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 01:16:15 +01:00
var nodeDict = {}
var linkDict = {}
2015-03-25 00:54:00 +01:00
self.setData = function (linkScale, sidebar, now, newnodes, lostnodes, onlinenodes, links, gotoAnything) {
2015-03-25 01:16:15 +01:00
nodeDict = {}
linkDict = {}
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
var lines = addLinksToMap(linkDict, linkScale, links, gotoAnything)
2015-03-25 00:54:00 +01:00
var nodes = newnodes.concat(lostnodes).filter(has_location)
2015-03-25 01:16:15 +01:00
var markers = nodes.map(mkMarker(nodeDict, function (d) {
2015-03-25 00:54:00 +01:00
if (d.flags.online)
return iconNew
2015-03-25 01:27:05 +01:00
return iconOffline
2015-03-25 00:54:00 +01:00
}, gotoAnything))
var onlinemarkers = subtract(onlinenodes.filter(has_location), newnodes)
2015-03-25 01:16:15 +01:00
.map(mkMarker(nodeDict, function (d) { return iconOnline }, gotoAnything))
2015-03-25 00:54:00 +01:00
var groupLines = L.featureGroup(lines).addTo(map)
var groupOnline = L.featureGroup(onlinemarkers).addTo(map)
var group = L.featureGroup(markers).addTo(map)
var bounds = group.getBounds()
if (!bounds.isValid())
bounds = groupOnline.getBounds()
if (bounds.isValid())
2015-03-25 01:16:15 +01:00
setView(bounds)
}
function setView(bounds) {
map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
}
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
function goto(dict, id) {
var m = dict[id]
if (m === undefined)
return
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
var bounds
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
if ("getBounds" in m)
bounds = m.getBounds()
else
bounds = L.latLngBounds([m.getLatLng()])
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
setView(bounds)
m.openPopup(bounds.getCenter())
}
self.gotoNode = function (d) {
goto(nodeDict, d.nodeinfo.node_id)
}
2015-03-25 00:54:00 +01:00
2015-03-25 01:16:15 +01:00
self.gotoLink = function (d) {
goto(linkDict, linkId(d))
2015-03-25 00:54:00 +01:00
}
return self
}
})