map.js 12 KB

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