map.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. define(["leaflet", "moment", "leaflet.label"], function (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[linkId(d)] = 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. var groupOnline, groupOffline, groupNew, groupLost
  44. return function (linkScale, sidebar, router) {
  45. var self = this
  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. self.setData = function (data) {
  59. nodeDict = {}
  60. linkDict = {}
  61. var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
  62. L.featureGroup(lines).addTo(map)
  63. var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
  64. var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
  65. var markersOnline = nodesOnline.filter(has_location)
  66. .map(mkMarker(nodeDict, function () { return iconOnline }, router))
  67. var markersOffline = nodesOffline.filter(has_location)
  68. .map(mkMarker(nodeDict, function () { return iconOffline }, router))
  69. var markersNew = data.nodes.new.filter(has_location)
  70. .map(mkMarker(nodeDict, function () { return iconNew }, router))
  71. var markersLost = data.nodes.lost.filter(has_location)
  72. .map(mkMarker(nodeDict, function (d) {
  73. if (d.lastseen.isAfter(moment(data.now).subtract(3, "days")))
  74. return iconAlert
  75. return iconLost
  76. }, router))
  77. groupOffline = L.featureGroup(markersOffline).addTo(map)
  78. groupOnline = L.featureGroup(markersOnline).addTo(map)
  79. groupNew = L.featureGroup(markersNew).addTo(map)
  80. groupLost = L.featureGroup(markersLost).addTo(map)
  81. }
  82. function resetMarkerStyles(nodes, links) {
  83. Object.keys(nodes).forEach( function (d) {
  84. nodes[d].resetStyle()
  85. })
  86. Object.keys(links).forEach( function (d) {
  87. links[d].resetStyle()
  88. })
  89. }
  90. function setView(bounds) {
  91. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  92. }
  93. function resetView() {
  94. resetMarkerStyles(nodeDict, linkDict)
  95. var bounds = L.latLngBounds([])
  96. bounds.extend(groupNew.getBounds())
  97. bounds.extend(groupLost.getBounds())
  98. if (!bounds.isValid())
  99. bounds.extend(groupOnline.getBounds())
  100. if (!bounds.isValid())
  101. bounds.extend(groupOffline.getBounds())
  102. if (bounds.isValid())
  103. setView(bounds)
  104. }
  105. function goto(dict, id) {
  106. var m = dict[id]
  107. if (m === undefined)
  108. return undefined
  109. var bounds
  110. if ("getBounds" in m)
  111. bounds = m.getBounds()
  112. else
  113. bounds = L.latLngBounds([m.getLatLng()])
  114. setView(bounds)
  115. return m
  116. }
  117. self.resetView = resetView
  118. self.gotoNode = function (d) {
  119. resetMarkerStyles(nodeDict, linkDict)
  120. var m = goto(nodeDict, d.nodeinfo.node_id)
  121. if (m)
  122. m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
  123. else
  124. resetView()
  125. }
  126. self.gotoLink = function (d) {
  127. resetMarkerStyles(nodeDict, linkDict)
  128. var m = goto(linkDict, linkId(d))
  129. if (m)
  130. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  131. else
  132. resetView()
  133. }
  134. self.destroy = function () {
  135. map.remove()
  136. }
  137. return self
  138. }
  139. })