map.js 10.0 KB

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