map.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. var clientLayer = new ClientLayer()
  175. clientLayer.addTo(map)
  176. clientLayer.setZIndex(5)
  177. var labelsLayer = new LabelsLayer()
  178. labelsLayer.addTo(map)
  179. labelsLayer.setZIndex(6)
  180. var nodeDict = {}
  181. var linkDict = {}
  182. var highlight
  183. function resetMarkerStyles(nodes, links) {
  184. Object.keys(nodes).forEach( function (d) {
  185. nodes[d].resetStyle()
  186. })
  187. Object.keys(links).forEach( function (d) {
  188. links[d].resetStyle()
  189. })
  190. }
  191. function setView(bounds) {
  192. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  193. }
  194. function resetZoom() {
  195. setView(barycenter.getBounds())
  196. }
  197. function goto(m) {
  198. var bounds
  199. if ("getBounds" in m)
  200. bounds = m.getBounds()
  201. else
  202. bounds = L.latLngBounds([m.getLatLng()])
  203. setView(bounds)
  204. return m
  205. }
  206. function updateView(nopanzoom) {
  207. resetMarkerStyles(nodeDict, linkDict)
  208. var m
  209. if (highlight !== undefined)
  210. if (highlight.type === "node") {
  211. m = nodeDict[highlight.o.nodeinfo.node_id]
  212. if (m)
  213. m.setStyle({ color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7, className: "stroke-first" })
  214. } else if (highlight.type === "link") {
  215. m = linkDict[highlight.o.id]
  216. if (m)
  217. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  218. }
  219. if (!nopanzoom)
  220. if (m)
  221. goto(m)
  222. else if (savedView)
  223. map.setView(savedView.center, savedView.zoom)
  224. else
  225. resetZoom()
  226. }
  227. function calcBarycenter(nodes) {
  228. nodes = nodes.map(function (d) { return d.nodeinfo.location })
  229. var lats = nodes.map(function (d) { return d.latitude })
  230. var lngs = nodes.map(function (d) { return d.longitude })
  231. var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
  232. var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
  233. var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
  234. barycenter.lng + barycenterDev[1])
  235. var r = barycenter.distanceTo(barycenterCircle)
  236. return L.circle(barycenter, r * config.mapSigmaScale)
  237. }
  238. function mapRTree(d) {
  239. var o = [ d.nodeinfo.location.latitude, d.nodeinfo.location.longitude,
  240. d.nodeinfo.location.latitude, d.nodeinfo.location.longitude]
  241. o.node = d
  242. return o
  243. }
  244. self.setData = function (data) {
  245. nodeDict = {}
  246. linkDict = {}
  247. if (groupOffline)
  248. groupOffline.clearLayers()
  249. if (groupOnline)
  250. groupOnline.clearLayers()
  251. if (groupNew)
  252. groupNew.clearLayers()
  253. if (groupLost)
  254. groupLost.clearLayers()
  255. if (groupLines)
  256. groupLines.clearLayers()
  257. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  258. groupLines = L.featureGroup(lines).addTo(map)
  259. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  260. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  261. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  262. var markersOnline = nodesOnline.filter(has_location)
  263. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  264. var markersOffline = nodesOffline.filter(has_location)
  265. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  266. var markersNew = data.nodes.new.filter(has_location)
  267. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  268. var markersLost = data.nodes.lost.filter(has_location)
  269. .map(mkMarker(nodeDict, function (d) {
  270. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  271. return iconAlert
  272. return iconLost
  273. }, router))
  274. groupOffline = L.featureGroup(markersOffline).addTo(map)
  275. groupOnline = L.featureGroup(markersOnline).addTo(map)
  276. groupNew = L.featureGroup(markersNew).addTo(map)
  277. groupLost = L.featureGroup(markersLost).addTo(map)
  278. var rtreeOnlineAll = rbush(9)
  279. rtreeOnlineAll.load(data.nodes.all.filter(online).filter(has_location).map(mapRTree))
  280. clientLayer.setData(rtreeOnlineAll)
  281. labelsLayer.setData({online: nodesOnline.filter(has_location),
  282. offline: nodesOffline.filter(has_location),
  283. new: data.nodes.new.filter(has_location),
  284. lost: data.nodes.lost.filter(has_location)
  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. })