From 5b352d7a325b4ce43d47998abe395c638a6a3d01 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Wed, 25 Mar 2015 21:11:15 +0100 Subject: [PATCH] refactor main.js --- lib/main.js | 60 +++++++++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/lib/main.js b/lib/main.js index 818def8..cbd9878 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,48 +1,40 @@ require(["router", "map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"], function (Router, Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) { - main() + getJSON("config.json").then(main) - function main() { - getJSON("config.json").then( function (config) { - moment.locale("de") + function main(config) { + 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) - router.addTarget(infobox) + sidebar.add(meshstats) + sidebar.add(newnodeslist) + sidebar.add(lostnodeslist) + sidebar.add(linklist) - var map = new Map(linkScale, sidebar, router) - document.body.insertBefore(map.div, document.body.firstChild) - router.addTarget(map) + router.addTarget(infobox) + router.addTarget(map) - var meshstats = new Meshstats() - sidebar.add(meshstats) + var urls = [ config.dataPath + 'nodes.json', + config.dataPath + 'graph.json' + ] - var newnodeslist = new SimpleNodelist(config, "firstseen", router, "Neue Knoten") - sidebar.add(newnodeslist) + Promise.all(urls.map(getJSON)) + .then(handle_data) + .then(function () { router.loadState(window.location.hash) }) - var lostnodeslist = new SimpleNodelist(config, "lastseen", router, "Verschwundene Knoten") - 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) { + function handle_data(data) { var nodedict = data[0] var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })