map.js 13 KB

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