map.js 12 KB

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