map.js 8.3 KB

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