map.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. define(function () {
  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: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
  40. var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
  41. var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
  42. var groupOnline, group
  43. return function (linkScale, sidebar, router) {
  44. var self = this
  45. var el = document.createElement("div")
  46. el.classList.add("map")
  47. self.div = el
  48. var map = L.map(el, options)
  49. L.control.zoom({ position: "topright" }).addTo(map)
  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: "Map data Tiles &copy; <a href=\"https://www.mapquest.com/\" target=\"_blank\">MapQuest</a> <img src=\"https://developer.mapquest.com/content/osm/mq_logo.png\" />, Map data © OpenStreetMap contributors, CC-BY-SA",
  54. maxZoom: 18
  55. }).addTo(map)
  56. var nodeDict = {}
  57. var linkDict = {}
  58. self.setData = function (now, newnodes, lostnodes, onlinenodes, links) {
  59. nodeDict = {}
  60. linkDict = {}
  61. var lines = addLinksToMap(linkDict, linkScale, links, router)
  62. var nodes = newnodes.concat(lostnodes).filter(has_location)
  63. var markers = nodes.map(mkMarker(nodeDict, function (d) {
  64. if (d.flags.online)
  65. return iconNew
  66. if (d.lastseen.isAfter(moment(now).subtract(1, 'days')))
  67. return iconAlert
  68. return iconOffline
  69. }, router))
  70. var onlinemarkers = subtract(onlinenodes.filter(has_location), newnodes)
  71. .map(mkMarker(nodeDict, function (d) { return iconOnline }, router))
  72. var groupLines = L.featureGroup(lines).addTo(map)
  73. groupOnline = L.featureGroup(onlinemarkers).addTo(map)
  74. group = L.featureGroup(markers).addTo(map)
  75. resetView()
  76. }
  77. function resetView() {
  78. resetMarkerStyles(nodeDict, linkDict)
  79. var bounds = group.getBounds()
  80. if (!bounds.isValid())
  81. bounds = groupOnline.getBounds()
  82. if (bounds.isValid())
  83. setView(bounds)
  84. }
  85. function setView(bounds) {
  86. map.fitBounds(bounds, {paddingTopLeft: [sidebar.getWidth(), 0]})
  87. }
  88. function resetMarkerStyles(nodes, links) {
  89. Object.keys(nodes).forEach( function (d) {
  90. nodes[d].resetStyle()
  91. })
  92. Object.keys(links).forEach( function (d) {
  93. links[d].resetStyle()
  94. })
  95. }
  96. function goto(dict, id) {
  97. var m = dict[id]
  98. if (m === undefined)
  99. return
  100. var bounds
  101. if ("getBounds" in m)
  102. bounds = m.getBounds()
  103. else
  104. bounds = L.latLngBounds([m.getLatLng()])
  105. setView(bounds)
  106. return m
  107. }
  108. self.resetView = resetView
  109. self.gotoNode = function (d) {
  110. resetMarkerStyles(nodeDict, linkDict)
  111. var m = goto(nodeDict, d.nodeinfo.node_id)
  112. if (m)
  113. m.setStyle({ fillColor: m.options.color, color: "orange", weight: 20, fillOpacity: 1, opacity: 0.7 })
  114. else
  115. resetView()
  116. }
  117. self.gotoLink = function (d) {
  118. resetMarkerStyles(nodeDict, linkDict)
  119. var m = goto(linkDict, linkId(d))
  120. if (m)
  121. m.setStyle({ weight: 7, opacity: 1, dashArray: "10, 10" })
  122. else
  123. resetView()
  124. }
  125. return self
  126. }
  127. })