map.js 12 KB

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