map.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. define(["map/clientlayer", "map/labelslayer",
  2. "d3", "leaflet", "moment", "locationmarker", "rbush",
  3. "leaflet.label", "leaflet.providers"],
  4. function (ClientLayer, LabelsLayer, d3, L, moment, LocationMarker, rbush) {
  5. var options = { worldCopyJump: true,
  6. zoomControl: false
  7. }
  8. var AddLayerButton = L.Control.extend({
  9. options: {
  10. position: "bottomright"
  11. },
  12. initialize: function (f, options) {
  13. L.Util.setOptions(this, options)
  14. this.f = f
  15. },
  16. onAdd: function () {
  17. var button = L.DomUtil.create("button", "add-layer")
  18. button.textContent = ""
  19. L.DomEvent.disableClickPropagation(button)
  20. L.DomEvent.addListener(button, "click", this.f, this)
  21. this.button = button
  22. return button
  23. }
  24. })
  25. var CoordsButton = L.Control.extend({
  26. options: {
  27. position: "bottomright"
  28. },
  29. active: false,
  30. button: undefined,
  31. initialize: function (f, options) {
  32. L.Util.setOptions(this, options)
  33. this.f = f
  34. },
  35. onAdd: function () {
  36. var button = L.DomUtil.create("button", "show-coords")
  37. L.DomEvent.disableClickPropagation(button)
  38. L.DomEvent.addListener(button, "click", this.onClick, this)
  39. this.button = button
  40. return button
  41. },
  42. update: function() {
  43. this.button.classList.toggle("active", this.active)
  44. },
  45. set: function(v) {
  46. this.active = v
  47. this.update()
  48. },
  49. onClick: function () {
  50. this.f(!this.active)
  51. }
  52. })
  53. var LocateButton = L.Control.extend({
  54. options: {
  55. position: "bottomright"
  56. },
  57. active: false,
  58. button: undefined,
  59. initialize: function (f, options) {
  60. L.Util.setOptions(this, options)
  61. this.f = f
  62. },
  63. onAdd: function () {
  64. var button = L.DomUtil.create("button", "locate-user")
  65. button.textContent = ""
  66. L.DomEvent.disableClickPropagation(button)
  67. L.DomEvent.addListener(button, "click", this.onClick, this)
  68. this.button = button
  69. return button
  70. },
  71. update: function() {
  72. this.button.classList.toggle("active", this.active)
  73. },
  74. set: function(v) {
  75. this.active = v
  76. this.update()
  77. },
  78. onClick: function () {
  79. this.f(!this.active)
  80. }
  81. })
  82. function mkMarker(dict, iconFunc, router) {
  83. return function (d) {
  84. var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
  85. m.resetStyle = function () {
  86. m.setStyle(iconFunc(d))
  87. }
  88. m.on("click", router.node(d))
  89. m.bindLabel(d.nodeinfo.hostname)
  90. dict[d.nodeinfo.node_id] = m
  91. return m
  92. }
  93. }
  94. function addLinksToMap(dict, linkScale, graph, router) {
  95. graph = graph.filter( function (d) {
  96. return "distance" in d && !d.vpn
  97. })
  98. var lines = graph.map( function (d) {
  99. var opts = { color: linkScale(d.tq).hex(),
  100. weight: 4,
  101. opacity: 0.5,
  102. dashArray: "none"
  103. }
  104. var line = L.polyline(d.latlngs, opts)
  105. line.resetStyle = function () {
  106. line.setStyle(opts)
  107. }
  108. line.bindLabel(d.source.node.nodeinfo.hostname + " – " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
  109. line.on("click", router.link(d))
  110. dict[d.id] = line
  111. return line
  112. })
  113. return lines
  114. }
  115. var iconOnline = { color: "#1566A9", fillColor: "#1566A9", radius: 6, fillOpacity: 0.5, opacity: 0.5, weight: 2, className: "stroke-first" }
  116. var iconOffline = { color: "#D43E2A", fillColor: "#D43E2A", radius: 3, fillOpacity: 0.5, opacity: 0.5, weight: 1, className: "stroke-first" }
  117. var iconLost = { color: "#D43E2A", fillColor: "#D43E2A", radius: 6, fillOpacity: 0.8, opacity: 0.8, weight: 1, className: "stroke-first" }
  118. var iconAlert = { color: "#D43E2A", fillColor: "#D43E2A", radius: 6, fillOpacity: 0.8, opacity: 0.8, weight: 2, className: "stroke-first node-alert" }
  119. var iconNew = { color: "#1566A9", fillColor: "#93E929", radius: 6, fillOpacity: 1.0, opacity: 0.5, weight: 2 }
  120. return function (config, linkScale, sidebar, router, buttons) {
  121. var self = this
  122. var barycenter
  123. var groupOnline, groupOffline, groupNew, groupLost, groupLines
  124. var savedView
  125. var map, userLocation
  126. var layerControl
  127. var customLayers = new Set()
  128. var baseLayers = {}
  129. var locateUserButton = new LocateButton(function (d) {
  130. if (d)
  131. enableTracking()
  132. else
  133. disableTracking()
  134. })
  135. var mybuttons = []
  136. function addButton(button) {
  137. var el = button.onAdd()
  138. mybuttons.push(el)
  139. buttons.appendChild(el)
  140. }
  141. function clearButtons() {
  142. mybuttons.forEach( function (d) {
  143. buttons.removeChild(d)
  144. })
  145. }
  146. var showCoordsButton = new CoordsButton(function (d) {
  147. if (d)
  148. enableCoords()
  149. else
  150. disableCoords()
  151. })
  152. function saveView() {
  153. savedView = {center: map.getCenter(),
  154. zoom: map.getZoom()}
  155. }
  156. function enableTracking() {
  157. map.locate({watch: true,
  158. enableHighAccuracy: true,
  159. setView: true
  160. })
  161. locateUserButton.set(true)
  162. }
  163. function disableTracking() {
  164. map.stopLocate()
  165. locationError()
  166. locateUserButton.set(false)
  167. }
  168. function enableCoords() {
  169. map.getContainer().classList.add("pick-coordinates")
  170. map.on("click", showCoordinates)
  171. showCoordsButton.set(true)
  172. }
  173. function disableCoords() {
  174. map.getContainer().classList.remove("pick-coordinates")
  175. map.off("click", showCoordinates)
  176. showCoordsButton.set(false)
  177. }
  178. function locationFound(e) {
  179. if (!userLocation)
  180. userLocation = new LocationMarker(e.latlng).addTo(map)
  181. userLocation.setLatLng(e.latlng)
  182. userLocation.setAccuracy(e.accuracy)
  183. }
  184. function showCoordinates(e) {
  185. window.alert("Koordinaten: " + e.latlng.lat.toFixed(6) + ", " + e.latlng.lng.toFixed(6))
  186. disableCoords()
  187. }
  188. function locationError() {
  189. if (userLocation) {
  190. map.removeLayer(userLocation)
  191. userLocation = null
  192. }
  193. }
  194. function addLayer(layerName) {
  195. if (layerName in baseLayers)
  196. return
  197. if (customLayers.has(layerName))
  198. return
  199. try {
  200. var layer = L.tileLayer.provider(layerName)
  201. layerControl.addBaseLayer(layer, layerName)
  202. customLayers.add(layerName)
  203. if (localStorageTest())
  204. localStorage.setItem("map/customLayers", JSON.stringify(Array.from(customLayers)))
  205. } catch (e) {
  206. return
  207. }
  208. }
  209. var el = document.createElement("div")
  210. el.classList.add("map")
  211. map = L.map(el, options)
  212. var layers = config.mapLayers.map( function (d) {
  213. return {
  214. "name": d.name,
  215. "layer": "url" in d ? L.tileLayer(d.url, d.config) : L.tileLayer.provider(d.name)
  216. }
  217. })
  218. layers[0].layer.addTo(map)
  219. layers.forEach( function (d) {
  220. baseLayers[d.name] = d.layer
  221. })
  222. map.on("locationfound", locationFound)
  223. map.on("locationerror", locationError)
  224. map.on("dragend", saveView)
  225. addButton(locateUserButton)
  226. addButton(showCoordsButton)
  227. addButton(new AddLayerButton(function () {
  228. /*eslint no-alert:0*/
  229. var layerName = prompt("Leaflet Provider:")
  230. addLayer(layerName)
  231. }))
  232. layerControl = L.control.layers(baseLayers, [], {position: "bottomright"})
  233. layerControl.addTo(map)
  234. if (localStorageTest()) {
  235. var d = JSON.parse(localStorage.getItem("map/customLayers"))
  236. if (d)
  237. d.forEach(addLayer)
  238. }
  239. var clientLayer = new ClientLayer({minZoom: 15})
  240. clientLayer.addTo(map)
  241. clientLayer.setZIndex(5)
  242. var labelsLayer = new LabelsLayer()
  243. labelsLayer.addTo(map)
  244. labelsLayer.setZIndex(6)
  245. var nodeDict = {}
  246. var linkDict = {}
  247. var highlight
  248. function resetMarkerStyles(nodes, links) {
  249. Object.keys(nodes).forEach( function (d) {
  250. nodes[d].resetStyle()
  251. })
  252. Object.keys(links).forEach( function (d) {
  253. links[d].resetStyle()
  254. })
  255. }
  256. function setView(bounds) {
  257. map.fitBounds(bounds, {paddingTopLeft: [sidebar(), 0]})
  258. }
  259. function resetZoom() {
  260. if (barycenter)
  261. setView(barycenter.getBounds())
  262. }
  263. function goto(m) {
  264. var bounds
  265. if ("getBounds" in m)
  266. bounds = m.getBounds()
  267. else
  268. bounds = L.latLngBounds([m.getLatLng()])
  269. setView(bounds)
  270. return m
  271. }
  272. function updateView(nopanzoom) {
  273. resetMarkerStyles(nodeDict, linkDict)
  274. var m
  275. if (highlight !== undefined)
  276. if (highlight.type === "node") {
  277. m = nodeDict[highlight.o.nodeinfo.node_id]
  278. if (m)
  279. m.setStyle({ color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7, className: "stroke-first" })
  280. } else if (highlight.type === "link") {
  281. m = linkDict[highlight.o.id]
  282. if (m)
  283. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  284. }
  285. if (!nopanzoom)
  286. if (m)
  287. goto(m)
  288. else if (savedView)
  289. map.setView(savedView.center, savedView.zoom)
  290. else
  291. resetZoom()
  292. }
  293. function calcBarycenter(nodes) {
  294. nodes = nodes.map(function (d) { return d.nodeinfo.location })
  295. if (nodes.length === 0)
  296. return undefined
  297. var lats = nodes.map(function (d) { return d.latitude })
  298. var lngs = nodes.map(function (d) { return d.longitude })
  299. var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
  300. var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
  301. if (barycenterDev[0] === undefined)
  302. barycenterDev[0] = 0
  303. if (barycenterDev[1] === undefined)
  304. barycenterDev[1] = 0
  305. var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
  306. barycenter.lng + barycenterDev[1])
  307. var r = barycenter.distanceTo(barycenterCircle)
  308. return L.circle(barycenter, r * config.mapSigmaScale)
  309. }
  310. function mapRTree(d) {
  311. var o = [ d.nodeinfo.location.latitude, d.nodeinfo.location.longitude,
  312. d.nodeinfo.location.latitude, d.nodeinfo.location.longitude]
  313. o.node = d
  314. return o
  315. }
  316. self.setData = function (data) {
  317. nodeDict = {}
  318. linkDict = {}
  319. if (groupOffline)
  320. groupOffline.clearLayers()
  321. if (groupOnline)
  322. groupOnline.clearLayers()
  323. if (groupNew)
  324. groupNew.clearLayers()
  325. if (groupLost)
  326. groupLost.clearLayers()
  327. if (groupLines)
  328. groupLines.clearLayers()
  329. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  330. groupLines = L.featureGroup(lines).addTo(map)
  331. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  332. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  333. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  334. var markersOnline = nodesOnline.filter(has_location)
  335. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  336. var markersOffline = nodesOffline.filter(has_location)
  337. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  338. var markersNew = data.nodes.new.filter(has_location)
  339. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  340. var markersLost = data.nodes.lost.filter(has_location)
  341. .map(mkMarker(nodeDict, function (d) {
  342. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  343. return iconAlert
  344. return iconLost
  345. }, router))
  346. groupOffline = L.featureGroup(markersOffline).addTo(map)
  347. groupLost = L.featureGroup(markersLost).addTo(map)
  348. groupOnline = L.featureGroup(markersOnline).addTo(map)
  349. groupNew = L.featureGroup(markersNew).addTo(map)
  350. var rtreeOnlineAll = rbush(9)
  351. rtreeOnlineAll.load(data.nodes.all.filter(online).filter(has_location).map(mapRTree))
  352. clientLayer.setData(rtreeOnlineAll)
  353. labelsLayer.setData({online: nodesOnline.filter(has_location),
  354. offline: nodesOffline.filter(has_location),
  355. new: data.nodes.new.filter(has_location),
  356. lost: data.nodes.lost.filter(has_location)
  357. })
  358. updateView(true)
  359. }
  360. self.resetView = function () {
  361. disableTracking()
  362. highlight = undefined
  363. updateView()
  364. }
  365. self.gotoNode = function (d) {
  366. disableTracking()
  367. highlight = {type: "node", o: d}
  368. updateView()
  369. }
  370. self.gotoLink = function (d) {
  371. disableTracking()
  372. highlight = {type: "link", o: d}
  373. updateView()
  374. }
  375. self.destroy = function () {
  376. clearButtons()
  377. map.remove()
  378. if (el.parentNode)
  379. el.parentNode.removeChild(el)
  380. }
  381. self.render = function (d) {
  382. d.appendChild(el)
  383. map.invalidateSize()
  384. }
  385. return self
  386. }
  387. })