map.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 map, userLocation
  95. var layerControl
  96. var customLayers = new Set()
  97. var locateUserButton = new LocateButton(function (d) {
  98. if (d)
  99. enableTracking()
  100. else
  101. disableTracking()
  102. })
  103. function enableTracking() {
  104. map.locate({watch: true,
  105. enableHighAccuracy: true,
  106. setView: true
  107. })
  108. locateUserButton.set(true)
  109. }
  110. function disableTracking() {
  111. map.stopLocate()
  112. locationError()
  113. locateUserButton.set(false)
  114. }
  115. function locationFound(e) {
  116. if (!userLocation)
  117. userLocation = new LocationMarker(e.latlng).addTo(map)
  118. userLocation.setLatLng(e.latlng)
  119. userLocation.setAccuracy(e.accuracy)
  120. }
  121. function locationError() {
  122. if (userLocation) {
  123. map.removeLayer(userLocation)
  124. userLocation = null
  125. }
  126. }
  127. function addLayer(layerName) {
  128. if (customLayers.has(layerName))
  129. return
  130. try {
  131. var layer = L.tileLayer.provider(layerName)
  132. layerControl.addBaseLayer(layer, layerName)
  133. customLayers.add(layerName)
  134. if (!layerControl.added) {
  135. layerControl.addTo(map)
  136. layerControl.added = true
  137. }
  138. if (localStorageTest())
  139. localStorage.setItem("map/customLayers", JSON.stringify(Array.from(customLayers)))
  140. } catch (e) {
  141. return
  142. }
  143. }
  144. var el = document.createElement("div")
  145. el.classList.add("map")
  146. self.div = el
  147. map = L.map(el, options)
  148. var baseLayer = L.tileLayer("https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
  149. subdomains: "1234",
  150. type: "osm",
  151. attribution: "Tiles &copy; <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a>, Data CC-BY-SA OpenStreetMap",
  152. maxZoom: 18
  153. }).addTo(map)
  154. map.on("locationfound", locationFound)
  155. map.on("locationerror", locationError)
  156. map.addControl(locateUserButton)
  157. map.addControl(new AddLayerButton(function () {
  158. /*eslint no-alert:0*/
  159. var layerName = prompt("Leaflet Provider:")
  160. addLayer(layerName)
  161. }))
  162. layerControl = L.control.layers({"MapQuest": baseLayer}, [], {position: "bottomright"})
  163. if (localStorageTest()) {
  164. var layers = JSON.parse(localStorage.getItem("map/customLayers"))
  165. if (layers)
  166. layers.forEach(addLayer)
  167. }
  168. var clientLayer = new ClientLayer()
  169. clientLayer.addTo(map)
  170. clientLayer.setZIndex(5)
  171. var labelsLayer = new LabelsLayer({minZoom: 15})
  172. labelsLayer.addTo(map)
  173. labelsLayer.setZIndex(6)
  174. var nodeDict = {}
  175. var linkDict = {}
  176. var highlight
  177. function resetMarkerStyles(nodes, links) {
  178. Object.keys(nodes).forEach( function (d) {
  179. nodes[d].resetStyle()
  180. })
  181. Object.keys(links).forEach( function (d) {
  182. links[d].resetStyle()
  183. })
  184. }
  185. function setView(bounds) {
  186. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  187. }
  188. function resetZoom() {
  189. setView(barycenter.getBounds())
  190. }
  191. function goto(m) {
  192. var bounds
  193. if ("getBounds" in m)
  194. bounds = m.getBounds()
  195. else
  196. bounds = L.latLngBounds([m.getLatLng()])
  197. setView(bounds)
  198. return m
  199. }
  200. function updateView(nopanzoom) {
  201. resetMarkerStyles(nodeDict, linkDict)
  202. var m
  203. if (highlight !== undefined)
  204. if (highlight.type === "node") {
  205. m = nodeDict[highlight.o.nodeinfo.node_id]
  206. if (m)
  207. m.setStyle({ color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7, className: "stroke-first" })
  208. } else if (highlight.type === "link") {
  209. m = linkDict[highlight.o.id]
  210. if (m)
  211. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  212. }
  213. if (!nopanzoom)
  214. if (m)
  215. goto(m)
  216. else
  217. resetZoom()
  218. }
  219. function calcBarycenter(nodes) {
  220. nodes = nodes.map(function (d) { return d.nodeinfo.location })
  221. var lats = nodes.map(function (d) { return d.latitude })
  222. var lngs = nodes.map(function (d) { return d.longitude })
  223. var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
  224. var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
  225. var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
  226. barycenter.lng + barycenterDev[1])
  227. var r = barycenter.distanceTo(barycenterCircle)
  228. return L.circle(barycenter, r * config.mapSigmaScale)
  229. }
  230. function mapRTree(d) {
  231. var o = [ d.nodeinfo.location.latitude, d.nodeinfo.location.longitude,
  232. d.nodeinfo.location.latitude, d.nodeinfo.location.longitude]
  233. o.node = d
  234. return o
  235. }
  236. self.setData = function (data) {
  237. nodeDict = {}
  238. linkDict = {}
  239. if (groupOffline)
  240. groupOffline.clearLayers()
  241. if (groupOnline)
  242. groupOnline.clearLayers()
  243. if (groupNew)
  244. groupNew.clearLayers()
  245. if (groupLost)
  246. groupLost.clearLayers()
  247. if (groupLines)
  248. groupLines.clearLayers()
  249. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  250. groupLines = L.featureGroup(lines).addTo(map)
  251. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  252. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  253. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  254. var markersOnline = nodesOnline.filter(has_location)
  255. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  256. var markersOffline = nodesOffline.filter(has_location)
  257. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  258. var markersNew = data.nodes.new.filter(has_location)
  259. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  260. var markersLost = data.nodes.lost.filter(has_location)
  261. .map(mkMarker(nodeDict, function (d) {
  262. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  263. return iconAlert
  264. return iconLost
  265. }, router))
  266. groupOffline = L.featureGroup(markersOffline).addTo(map)
  267. groupOnline = L.featureGroup(markersOnline).addTo(map)
  268. groupNew = L.featureGroup(markersNew).addTo(map)
  269. groupLost = L.featureGroup(markersLost).addTo(map)
  270. var rtreeOnlineAll = rbush(9)
  271. var rtreeOnline = rbush(9)
  272. var rtreeOffline = rbush(9)
  273. var rtreeNew = rbush(9)
  274. var rtreeLost = rbush(9)
  275. rtreeOnlineAll.load(data.nodes.all.filter(online).filter(has_location).map(mapRTree))
  276. rtreeOnline.load(nodesOnline.filter(has_location).map(mapRTree))
  277. rtreeOffline.load(nodesOffline.filter(has_location).map(mapRTree))
  278. rtreeNew.load(data.nodes.new.filter(has_location).map(mapRTree))
  279. rtreeLost.load(data.nodes.lost.filter(has_location).map(mapRTree))
  280. clientLayer.setData(rtreeOnlineAll)
  281. labelsLayer.setData({online: rtreeOnline,
  282. offline: rtreeOffline,
  283. new: rtreeNew,
  284. lost: rtreeLost
  285. })
  286. updateView(true)
  287. }
  288. self.resetView = function () {
  289. disableTracking()
  290. highlight = undefined
  291. updateView()
  292. }
  293. self.gotoNode = function (d) {
  294. disableTracking()
  295. highlight = {type: "node", o: d}
  296. updateView()
  297. }
  298. self.gotoLink = function (d) {
  299. disableTracking()
  300. highlight = {type: "link", o: d}
  301. updateView()
  302. }
  303. self.destroy = function () {
  304. map.remove()
  305. }
  306. return self
  307. }
  308. })