map.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. define(["map/clientlayer", "map/labelslayer",
  2. "d3", "leaflet", "moment", "locationmarker",
  3. "leaflet.label", "leaflet.providers"],
  4. function (ClientLayer, LabelsLayer, d3, L, moment, LocationMarker) {
  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. self.setData = function (data) {
  231. nodeDict = {}
  232. linkDict = {}
  233. if (groupOffline)
  234. groupOffline.clearLayers()
  235. if (groupOnline)
  236. groupOnline.clearLayers()
  237. if (groupNew)
  238. groupNew.clearLayers()
  239. if (groupLost)
  240. groupLost.clearLayers()
  241. if (groupLines)
  242. groupLines.clearLayers()
  243. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  244. groupLines = L.featureGroup(lines).addTo(map)
  245. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  246. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  247. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  248. var markersOnline = nodesOnline.filter(has_location)
  249. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  250. var markersOffline = nodesOffline.filter(has_location)
  251. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  252. var markersNew = data.nodes.new.filter(has_location)
  253. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  254. var markersLost = data.nodes.lost.filter(has_location)
  255. .map(mkMarker(nodeDict, function (d) {
  256. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  257. return iconAlert
  258. return iconLost
  259. }, router))
  260. groupOffline = L.featureGroup(markersOffline).addTo(map)
  261. groupOnline = L.featureGroup(markersOnline).addTo(map)
  262. groupNew = L.featureGroup(markersNew).addTo(map)
  263. groupLost = L.featureGroup(markersLost).addTo(map)
  264. clientLayer.setData(data.nodes.all.filter(online).filter(has_location))
  265. labelsLayer.setData({online: nodesOnline.filter(has_location),
  266. offline: nodesOffline.filter(has_location),
  267. new: data.nodes.new.filter(has_location),
  268. lost: data.nodes.lost.filter(has_location)
  269. })
  270. updateView(true)
  271. }
  272. self.resetView = function () {
  273. disableTracking()
  274. highlight = undefined
  275. updateView()
  276. }
  277. self.gotoNode = function (d) {
  278. disableTracking()
  279. highlight = {type: "node", o: d}
  280. updateView()
  281. }
  282. self.gotoLink = function (d) {
  283. disableTracking()
  284. highlight = {type: "link", o: d}
  285. updateView()
  286. }
  287. self.destroy = function () {
  288. map.remove()
  289. }
  290. return self
  291. }
  292. })