meshviewer/lib/main.js

228 lines
5.9 KiB
JavaScript
Raw Normal View History

require(["map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"],
function (Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) {
2015-03-24 23:54:00 +00:00
main()
function main() {
getJSON("config.json").then( function (config) {
moment.locale("de")
var linkScale = chroma.scale(chroma.interpolate.bezier(['green', 'yellow', 'red'])).domain([1, 5])
2015-03-25 14:33:36 +00:00
var sidebar = new Sidebar(document.body)
2015-03-25 00:16:15 +00:00
2015-03-25 18:45:21 +00:00
var router = new Router(config)
2015-03-25 10:21:09 +00:00
2015-03-25 18:45:21 +00:00
var infobox = new Infobox(config, sidebar, router)
router.addTarget(infobox)
2015-03-25 10:21:09 +00:00
2015-03-25 18:45:21 +00:00
var map = new Map(linkScale, sidebar, router)
2015-03-24 23:54:00 +00:00
document.body.insertBefore(map.div, document.body.firstChild)
2015-03-25 18:45:21 +00:00
router.addTarget(map)
2015-03-24 23:54:00 +00:00
2015-03-25 14:33:36 +00:00
var meshstats = new Meshstats()
sidebar.add(meshstats)
2015-03-24 23:54:00 +00:00
2015-03-25 18:45:21 +00:00
var newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten")
sidebar.add(newnodeslist)
2015-03-25 18:45:21 +00:00
var lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten")
sidebar.add(lostnodeslist)
2015-03-25 18:45:21 +00:00
var linklist = new Linklist(linkScale, router)
2015-03-25 15:04:23 +00:00
sidebar.add(linklist)
2015-03-24 23:54:00 +00:00
var urls = [ config.dataPath + 'nodes.json',
config.dataPath + 'graph.json'
]
var p = Promise.all(urls.map(getJSON))
2015-03-25 18:45:21 +00:00
p.then(handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router))
2015-03-24 23:54:00 +00:00
})
}
2015-03-25 18:45:21 +00:00
function handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router) {
2015-03-24 23:54:00 +00:00
return function (data) {
var nodedict = data[0]
var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
nodes = nodes.filter( function (d) {
return "firstseen" in d && "lastseen" in d
})
nodes.forEach( function(node) {
node.firstseen = moment.utc(node.firstseen)
node.lastseen = moment.utc(node.lastseen)
})
var now = moment()
var age = moment(now).subtract(14, 'days')
var newnodes = limit("firstseen", age, sortByKey("firstseen", nodes).filter(online))
var lostnodes = limit("lastseen", age, sortByKey("lastseen", nodes).filter(offline))
var onlinenodes = nodes.filter(online)
var graph = data[1].batadv
var graphnodes = data[0].nodes
graph.nodes.forEach( function (d) {
if (d.node_id in graphnodes)
d.node = graphnodes[d.node_id]
})
graph.links.forEach( function (d) {
if (graph.nodes[d.source].node)
d.source = graph.nodes[d.source]
else
d.source = undefined
if (graph.nodes[d.target].node)
d.target = graph.nodes[d.target]
else
d.target = undefined
})
var links = graph.links.filter( function (d) {
return d.source !== undefined && d.target !== undefined
})
links.forEach( function (d) {
if (!("location" in d.source.node.nodeinfo && "location" in d.target.node.nodeinfo))
return
d.latlngs = []
d.latlngs.push(L.latLng(d.source.node.nodeinfo.location.latitude, d.source.node.nodeinfo.location.longitude))
d.latlngs.push(L.latLng(d.target.node.nodeinfo.location.latitude, d.target.node.nodeinfo.location.longitude))
d.distance = d.latlngs[0].distanceTo(d.latlngs[1])
})
nodes.forEach( function (d) {
d.neighbours = []
})
links.forEach( function (d) {
d.source.node.neighbours.push({ node: d.target.node, link: d })
d.target.node.neighbours.push({ node: d.source.node, link: d })
})
2015-03-25 15:36:24 +00:00
map.setData(now, newnodes, lostnodes, onlinenodes, links)
2015-03-25 14:33:36 +00:00
meshstats.setData(nodes)
2015-03-25 15:04:23 +00:00
linklist.setData(links)
newnodeslist.setData(newnodes)
lostnodeslist.setData(lostnodes)
2015-03-24 23:54:00 +00:00
var historyDict = { nodes: {}, links: {} }
nodes.forEach( function (d) {
historyDict.nodes[d.nodeinfo.node_id] = d
})
links.forEach( function (d) {
historyDict.links[linkId(d)] = d
})
2015-03-25 18:45:21 +00:00
router.reset(false)
2015-03-25 01:50:28 +00:00
2015-03-25 18:45:21 +00:00
gotoHistory(router, historyDict, window.location.hash)
2015-03-24 23:54:00 +00:00
window.onpopstate = function (d) {
2015-03-25 18:45:21 +00:00
gotoHistory(router, historyDict, d.state)
2015-03-24 23:54:00 +00:00
}
}
}
function pushHistory(d) {
var s = "#!"
if (d) {
if ("node" in d)
s += "n:" + d.node.nodeinfo.node_id
if ("link" in d)
s += "l:" + linkId(d.link)
}
window.history.pushState(s, undefined, s)
}
2015-03-25 18:45:21 +00:00
function gotoHistory(router, dict, s) {
2015-03-24 23:54:00 +00:00
if (!s.startsWith("#!"))
return
s = s.slice(2)
var args = s.split(":")
if (args[0] === "n") {
var id = args[1]
if (id in dict.nodes)
2015-03-25 18:45:21 +00:00
router.node(dict.nodes[id], true, false)()
2015-03-24 23:54:00 +00:00
}
if (args[0] === "l") {
var id = args[1]
if (id in dict.links)
2015-03-25 18:45:21 +00:00
router.link(dict.links[id], true, false)()
2015-03-24 23:54:00 +00:00
}
}
2015-03-25 14:33:36 +00:00
function Router(config, nodes) {
2015-03-25 00:16:15 +00:00
var targets = []
2015-03-24 23:54:00 +00:00
var self = this
2015-03-25 10:21:09 +00:00
var infobox
2015-03-25 01:50:28 +00:00
2015-03-25 14:07:43 +00:00
function resetView(push) {
push = trueDefault(push)
2015-03-25 01:50:28 +00:00
targets.forEach( function (t) {
t.resetView()
})
2015-03-25 10:21:09 +00:00
2015-03-25 14:07:43 +00:00
if (push)
pushHistory()
2015-03-25 01:50:28 +00:00
}
2015-03-24 23:54:00 +00:00
function gotoNode(d, showMap, push) {
showMap = trueDefault(showMap)
push = trueDefault(push)
2015-03-25 00:16:15 +00:00
targets.forEach( function (t) {
t.gotoNode(d)
})
2015-03-24 23:54:00 +00:00
if (push)
pushHistory( { node: d })
return false
}
function gotoLink(d, showMap, push) {
showMap = trueDefault(showMap)
push = trueDefault(push)
2015-03-25 00:16:15 +00:00
targets.forEach( function (t) {
t.gotoLink(d)
})
2015-03-24 23:54:00 +00:00
if (push)
pushHistory( { link: d })
return false
}
2015-03-25 10:21:09 +00:00
self.node = function (d, m, p) { return function () { return gotoNode(d, m, p) }}
self.link = function (d, m, p) { return function () { return gotoLink(d, m, p) }}
self.reset = resetView
self.addMarkers = function (d) {
2015-03-24 23:54:00 +00:00
markers = d
}
2015-03-25 10:21:09 +00:00
self.addTarget = function (d) { targets.push(d) }
2015-03-24 23:54:00 +00:00
2015-03-25 10:21:09 +00:00
return self
2015-03-24 23:54:00 +00:00
}
})