map.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. define(["d3", "leaflet", "moment", "leaflet.label"], function (d3, L, moment) {
  2. var options = { worldCopyJump: true,
  3. zoomControl: false
  4. }
  5. function mkMarker(dict, iconFunc, router) {
  6. return function (d) {
  7. var m = L.circleMarker([d.nodeinfo.location.latitude, d.nodeinfo.location.longitude], iconFunc(d))
  8. m.resetStyle = function () {
  9. m.setStyle(iconFunc(d))
  10. }
  11. m.on("click", router.node(d))
  12. m.bindLabel(d.nodeinfo.hostname)
  13. dict[d.nodeinfo.node_id] = m
  14. return m
  15. }
  16. }
  17. function addLinksToMap(dict, linkScale, graph, router) {
  18. graph = graph.filter( function (d) {
  19. return "distance" in d
  20. })
  21. var lines = graph.map( function (d) {
  22. var opts = { color: linkScale(d.tq).hex(),
  23. weight: 4,
  24. opacity: 0.5,
  25. dashArray: "none"
  26. }
  27. var line = L.polyline(d.latlngs, opts)
  28. line.resetStyle = function () {
  29. line.setStyle(opts)
  30. }
  31. line.bindLabel(d.source.node.nodeinfo.hostname + " – " + d.target.node.nodeinfo.hostname + "<br><strong>" + showDistance(d) + " / " + showTq(d) + "</strong>")
  32. line.on("click", router.link(d))
  33. dict[d.id] = line
  34. return line
  35. })
  36. return lines
  37. }
  38. var iconOnline = { color: "#1566A9", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
  39. var iconOffline = { color: "#D43E2A", radius: 3, fillOpacity: 0.5, weight: 1, className: "stroke-first" }
  40. var iconLost = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 1, className: "stroke-first" }
  41. var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
  42. var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
  43. return function (config, linkScale, sidebar, router) {
  44. var self = this
  45. var barycenter
  46. var el = document.createElement("div")
  47. el.classList.add("map")
  48. self.div = el
  49. var map = L.map(el, options)
  50. L.tileLayer("https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
  51. subdomains: "1234",
  52. type: "osm",
  53. attribution: "Tiles &copy; <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a>, Data CC-BY-SA OpenStreetMap",
  54. maxZoom: 18
  55. }).addTo(map)
  56. var nodeDict = {}
  57. var linkDict = {}
  58. function calcBarycenter(nodes) {
  59. nodes = nodes.map(function (d) { return d.nodeinfo.location })
  60. var lats = nodes.map(function (d) { return d.latitude })
  61. var lngs = nodes.map(function (d) { return d.longitude })
  62. var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
  63. var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
  64. var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
  65. barycenter.lng + barycenterDev[1])
  66. var r = barycenter.distanceTo(barycenterCircle)
  67. return L.circle(barycenter, r * config.mapSigmaScale)
  68. }
  69. self.setData = function (data) {
  70. nodeDict = {}
  71. linkDict = {}
  72. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  73. L.featureGroup(lines).addTo(map)
  74. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  75. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  76. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  77. var markersOnline = nodesOnline.filter(has_location)
  78. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  79. var markersOffline = nodesOffline.filter(has_location)
  80. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  81. var markersNew = data.nodes.new.filter(has_location)
  82. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  83. var markersLost = data.nodes.lost.filter(has_location)
  84. .map(mkMarker(nodeDict, function (d) {
  85. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  86. return iconAlert
  87. return iconLost
  88. }, router))
  89. L.featureGroup(markersOffline).addTo(map)
  90. L.featureGroup(markersOnline).addTo(map)
  91. L.featureGroup(markersNew).addTo(map)
  92. L.featureGroup(markersLost).addTo(map)
  93. }
  94. function resetMarkerStyles(nodes, links) {
  95. Object.keys(nodes).forEach( function (d) {
  96. nodes[d].resetStyle()
  97. })
  98. Object.keys(links).forEach( function (d) {
  99. links[d].resetStyle()
  100. })
  101. }
  102. function setView(bounds) {
  103. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  104. }
  105. function resetView() {
  106. resetMarkerStyles(nodeDict, linkDict)
  107. setView(barycenter.getBounds())
  108. }
  109. function goto(dict, id) {
  110. var m = dict[id]
  111. if (m === undefined)
  112. return undefined
  113. var bounds
  114. if ("getBounds" in m)
  115. bounds = m.getBounds()
  116. else
  117. bounds = L.latLngBounds([m.getLatLng()])
  118. setView(bounds)
  119. return m
  120. }
  121. self.resetView = resetView
  122. self.gotoNode = function (d) {
  123. resetMarkerStyles(nodeDict, linkDict)
  124. var m = goto(nodeDict, d.nodeinfo.node_id)
  125. if (m)
  126. m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
  127. else
  128. resetView()
  129. }
  130. self.gotoLink = function (d) {
  131. resetMarkerStyles(nodeDict, linkDict)
  132. var m = goto(linkDict, d.id)
  133. if (m)
  134. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  135. else
  136. resetView()
  137. }
  138. self.destroy = function () {
  139. map.remove()
  140. }
  141. return self
  142. }
  143. })