123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- define(function () {
- var options = { worldCopyJump: true,
- zoomControl: false
- }
- function mkMarker(dict, iconFunc, router) {
- return function (d) {
- var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
- m.resetStyle = function () {
- m.setStyle(iconFunc(d))
- }
- m.on('click', router.node(d))
- m.bindLabel(d.nodeinfo.hostname)
- dict[d.nodeinfo.node_id] = m
- return m
- }
- }
- function addLinksToMap(dict, linkScale, graph, router) {
- graph = graph.filter( function (d) {
- return "distance" in d
- })
- var lines = graph.map( function (d) {
- var opts = { color: linkScale(d.tq).hex(),
- weight: 4,
- opacity: 0.5,
- dashArray: "none"
- }
- var line = L.polyline(d.latlngs, opts)
- line.resetStyle = function () {
- line.setStyle(opts)
- }
- 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))
- dict[linkId(d)] = line
- return line
- })
- return lines
- }
- 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" }
- var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
- var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
- var groupOnline, group
- return function (linkScale, sidebar, router) {
- 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 © <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)
- var nodeDict = {}
- var linkDict = {}
- self.setData = function (now, newnodes, lostnodes, onlinenodes, links) {
- nodeDict = {}
- linkDict = {}
- var lines = addLinksToMap(linkDict, linkScale, links, router)
- var nodes = newnodes.concat(lostnodes).filter(has_location)
- var markers = nodes.map(mkMarker(nodeDict, function (d) {
- if (d.flags.online)
- return iconNew
- if (d.lastseen.isAfter(moment(now).subtract(1, 'days')))
- return iconAlert
- return iconOffline
- }, router))
- var onlinemarkers = subtract(onlinenodes.filter(has_location), newnodes)
- .map(mkMarker(nodeDict, function (d) { return iconOnline }, router))
- var groupLines = L.featureGroup(lines).addTo(map)
- groupOnline = L.featureGroup(onlinemarkers).addTo(map)
- group = L.featureGroup(markers).addTo(map)
- resetView()
- }
- function resetView() {
- resetMarkerStyles(nodeDict, linkDict)
- var bounds = group.getBounds()
- if (!bounds.isValid())
- bounds = groupOnline.getBounds()
- if (bounds.isValid())
- setView(bounds)
- }
- function setView(bounds) {
- map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
- }
- function resetMarkerStyles(nodes, links) {
- Object.keys(nodes).forEach( function (d) {
- nodes[d].resetStyle()
- })
- Object.keys(links).forEach( function (d) {
- links[d].resetStyle()
- })
- }
- function goto(dict, id) {
- var m = dict[id]
- if (m === undefined)
- return
- var bounds
- if ("getBounds" in m)
- bounds = m.getBounds()
- else
- bounds = L.latLngBounds([m.getLatLng()])
- setView(bounds)
- return m
- }
- self.resetView = resetView
- self.gotoNode = function (d) {
- 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 })
- else
- resetView()
- }
- self.gotoLink = function (d) {
- resetMarkerStyles(nodeDict, linkDict)
- var m = goto(linkDict, linkId(d))
- if (m)
- m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
- else
- resetView()
- }
- return self
- }
- })
|