main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. require(["map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"],
  2. function (Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) {
  3. main()
  4. function main() {
  5. getJSON("config.json").then( function (config) {
  6. moment.locale("de")
  7. var linkScale = chroma.scale(chroma.interpolate.bezier(['green', 'yellow', 'red'])).domain([1, 5])
  8. var sidebar = new Sidebar(document.body)
  9. var router = new Router(config)
  10. var infobox = new Infobox(config, sidebar, router)
  11. router.addTarget(infobox)
  12. var map = new Map(linkScale, sidebar, router)
  13. document.body.insertBefore(map.div, document.body.firstChild)
  14. router.addTarget(map)
  15. var meshstats = new Meshstats()
  16. sidebar.add(meshstats)
  17. var newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten")
  18. sidebar.add(newnodeslist)
  19. var lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten")
  20. sidebar.add(lostnodeslist)
  21. var linklist = new Linklist(linkScale, router)
  22. sidebar.add(linklist)
  23. var urls = [ config.dataPath + 'nodes.json',
  24. config.dataPath + 'graph.json'
  25. ]
  26. var p = Promise.all(urls.map(getJSON))
  27. p.then(handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router))
  28. })
  29. }
  30. function handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router) {
  31. return function (data) {
  32. var nodedict = data[0]
  33. var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
  34. nodes = nodes.filter( function (d) {
  35. return "firstseen" in d && "lastseen" in d
  36. })
  37. nodes.forEach( function(node) {
  38. node.firstseen = moment.utc(node.firstseen)
  39. node.lastseen = moment.utc(node.lastseen)
  40. })
  41. var now = moment()
  42. var age = moment(now).subtract(14, 'days')
  43. var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
  44. var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
  45. var onlinenodes = nodes.filter(online)
  46. var graph = data[1].batadv
  47. var graphnodes = data[0].nodes
  48. graph.nodes.forEach( function (d) {
  49. if (d.node_id in graphnodes)
  50. d.node = graphnodes[d.node_id]
  51. })
  52. graph.links.forEach( function (d) {
  53. if (graph.nodes[d.source].node)
  54. d.source = graph.nodes[d.source]
  55. else
  56. d.source = undefined
  57. if (graph.nodes[d.target].node)
  58. d.target = graph.nodes[d.target]
  59. else
  60. d.target = undefined
  61. })
  62. var links = graph.links.filter( function (d) {
  63. return d.source !== undefined && d.target !== undefined
  64. })
  65. links.forEach( function (d) {
  66. if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
  67. return
  68. d.latlngs = []
  69. d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
  70. d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
  71. d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
  72. })
  73. nodes.forEach( function (d) {
  74. d.neighbours = []
  75. })
  76. links.forEach( function (d) {
  77. d.source.node.neighbours.push({ node: d.target.node, link: d })
  78. d.target.node.neighbours.push({ node: d.source.node, link: d })
  79. })
  80. map.setData(now, newnodes, lostnodes, onlinenodes, links)
  81. meshstats.setData(nodes)
  82. linklist.setData(links)
  83. newnodeslist.setData(newnodes)
  84. lostnodeslist.setData(lostnodes)
  85. var historyDict = { nodes: {}, links: {} }
  86. nodes.forEach( function (d) {
  87. historyDict.nodes[d.nodeinfo.node_id] = d
  88. })
  89. links.forEach( function (d) {
  90. historyDict.links[linkId(d)] = d
  91. })
  92. router.reset(false)
  93. gotoHistory(router, historyDict, window.location.hash)
  94. window.onpopstate = function (d) {
  95. gotoHistory(router, historyDict, d.state)
  96. }
  97. }
  98. }
  99. function pushHistory(d) {
  100. var s = "#!"
  101. if (d) {
  102. if ("node" in d)
  103. s += "n:" + d.node.nodeinfo.node_id
  104. if ("link" in d)
  105. s += "l:" + linkId(d.link)
  106. }
  107. window.history.pushState(s, undefined, s)
  108. }
  109. function gotoHistory(router, dict, s) {
  110. if (!s.startsWith("#!"))
  111. return
  112. s = s.slice(2)
  113. var args = s.split(":")
  114. if (args[0] === "n") {
  115. var id = args[1]
  116. if (id in dict.nodes)
  117. router.node(dict.nodes[id], true, false)()
  118. }
  119. if (args[0] === "l") {
  120. var id = args[1]
  121. if (id in dict.links)
  122. router.link(dict.links[id], true, false)()
  123. }
  124. }
  125. function Router(config, nodes) {
  126. var targets = []
  127. var self = this
  128. var infobox
  129. function resetView(push) {
  130. push = trueDefault(push)
  131. targets.forEach( function (t) {
  132. t.resetView()
  133. })
  134. if (push)
  135. pushHistory()
  136. }
  137. function gotoNode(d, showMap, push) {
  138. showMap = trueDefault(showMap)
  139. push = trueDefault(push)
  140. targets.forEach( function (t) {
  141. t.gotoNode(d)
  142. })
  143. if (push)
  144. pushHistory( { node: d })
  145. return false
  146. }
  147. function gotoLink(d, showMap, push) {
  148. showMap = trueDefault(showMap)
  149. push = trueDefault(push)
  150. targets.forEach( function (t) {
  151. t.gotoLink(d)
  152. })
  153. if (push)
  154. pushHistory( { link: d })
  155. return false
  156. }
  157. self.node = function (d, m, p) { return function () { return gotoNode(d, m, p) }}
  158. self.link = function (d, m, p) { return function () { return gotoLink(d, m, p) }}
  159. self.reset = resetView
  160. self.addMarkers = function (d) {
  161. markers = d
  162. }
  163. self.addTarget = function (d) { targets.push(d) }
  164. return self
  165. }
  166. })