map.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 groupOnline, groupOffline, groupNew, groupLost, groupLines
  47. var el = document.createElement("div")
  48. el.classList.add("map")
  49. self.div = el
  50. var map = L.map(el, options)
  51. L.tileLayer("https://otile{s}-s.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg", {
  52. subdomains: "1234",
  53. type: "osm",
  54. attribution: "Tiles &copy; <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a>, Data CC-BY-SA OpenStreetMap",
  55. maxZoom: 18
  56. }).addTo(map)
  57. var nodeDict = {}
  58. var linkDict = {}
  59. function calcBarycenter(nodes) {
  60. nodes = nodes.map(function (d) { return d.nodeinfo.location })
  61. var lats = nodes.map(function (d) { return d.latitude })
  62. var lngs = nodes.map(function (d) { return d.longitude })
  63. var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
  64. var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
  65. var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
  66. barycenter.lng + barycenterDev[1])
  67. var r = barycenter.distanceTo(barycenterCircle)
  68. return L.circle(barycenter, r * config.mapSigmaScale)
  69. }
  70. self.setData = function (data) {
  71. nodeDict = {}
  72. linkDict = {}
  73. if (groupOffline)
  74. groupOffline.clearLayers()
  75. if (groupOnline)
  76. groupOnline.clearLayers()
  77. if (groupNew)
  78. groupNew.clearLayers()
  79. if (groupLost)
  80. groupLost.clearLayers()
  81. if (groupLines)
  82. groupLines.clearLayers()
  83. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  84. groupLines = L.featureGroup(lines).addTo(map)
  85. barycenter = calcBarycenter(data.nodes.all.filter(has_location))
  86. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  87. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  88. var markersOnline = nodesOnline.filter(has_location)
  89. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  90. var markersOffline = nodesOffline.filter(has_location)
  91. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  92. var markersNew = data.nodes.new.filter(has_location)
  93. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  94. var markersLost = data.nodes.lost.filter(has_location)
  95. .map(mkMarker(nodeDict, function (d) {
  96. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  97. return iconAlert
  98. return iconLost
  99. }, router))
  100. L.featureGroup(markersOffline).addTo(map)
  101. L.featureGroup(markersOnline).addTo(map)
  102. L.featureGroup(markersNew).addTo(map)
  103. L.featureGroup(markersLost).addTo(map)
  104. }
  105. function resetMarkerStyles(nodes, links) {
  106. Object.keys(nodes).forEach( function (d) {
  107. nodes[d].resetStyle()
  108. })
  109. Object.keys(links).forEach( function (d) {
  110. links[d].resetStyle()
  111. })
  112. }
  113. function setView(bounds) {
  114. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  115. }
  116. function resetView() {
  117. resetMarkerStyles(nodeDict, linkDict)
  118. setView(barycenter.getBounds())
  119. }
  120. function goto(dict, id) {
  121. var m = dict[id]
  122. if (m === undefined)
  123. return undefined
  124. var bounds
  125. if ("getBounds" in m)
  126. bounds = m.getBounds()
  127. else
  128. bounds = L.latLngBounds([m.getLatLng()])
  129. setView(bounds)
  130. return m
  131. }
  132. self.resetView = resetView
  133. self.gotoNode = function (d) {
  134. resetMarkerStyles(nodeDict, linkDict)
  135. var m = goto(nodeDict, d.nodeinfo.node_id)
  136. if (m)
  137. m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
  138. else
  139. resetView()
  140. }
  141. self.gotoLink = function (d) {
  142. resetMarkerStyles(nodeDict, linkDict)
  143. var m = goto(linkDict, d.id)
  144. if (m)
  145. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  146. else
  147. resetView()
  148. }
  149. self.destroy = function () {
  150. map.remove()
  151. }
  152. return self
  153. }
  154. })