map.js 8.3 KB

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