refactor main.js

This commit is contained in:
Nils Schneider 2015-03-25 21:11:15 +01:00
parent c79dff9111
commit 5b352d7a32

View file

@ -1,48 +1,40 @@
require(["router", "map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"], require(["router", "map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"],
function (Router, Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) { function (Router, Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) {
main() getJSON("config.json").then(main)
function main() { function main(config) {
getJSON("config.json").then( function (config) { moment.locale("de")
moment.locale("de")
var linkScale = chroma.scale(chroma.interpolate.bezier(['green', 'yellow', 'red'])).domain([1, 5]) var linkScale = chroma.scale(chroma.interpolate.bezier(['green', 'yellow', 'red'])).domain([1, 5])
var sidebar = new Sidebar(document.body)
var router = new Router(config)
var sidebar = new Sidebar(document.body) var map = new Map(linkScale, sidebar, router)
document.body.insertBefore(map.div, document.body.firstChild)
var router = new Router(config) var infobox = new Infobox(config, sidebar, router)
var meshstats = new Meshstats()
var newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten")
var lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten")
var linklist = new Linklist(linkScale, router)
var infobox = new Infobox(config, sidebar, router) sidebar.add(meshstats)
router.addTarget(infobox) sidebar.add(newnodeslist)
sidebar.add(lostnodeslist)
sidebar.add(linklist)
var map = new Map(linkScale, sidebar, router) router.addTarget(infobox)
document.body.insertBefore(map.div, document.body.firstChild) router.addTarget(map)
router.addTarget(map)
var meshstats = new Meshstats() var urls = [ config.dataPath + 'nodes.json',
sidebar.add(meshstats) config.dataPath + 'graph.json'
]
var newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten") Promise.all(urls.map(getJSON))
sidebar.add(newnodeslist) .then(handle_data)
.then(function () { router.loadState(window.location.hash) })
var lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten") function handle_data(data) {
sidebar.add(lostnodeslist)
var linklist = new Linklist(linkScale, router)
sidebar.add(linklist)
var urls = [ config.dataPath + 'nodes.json',
config.dataPath + 'graph.json'
]
var p = Promise.all(urls.map(getJSON))
p.then(handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router))
.then(function () { router.loadState(window.location.hash) })
})
}
function handle_data(sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, router) {
return function (data) {
var nodedict = data[0] var nodedict = data[0]
var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] }) var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })