From c9d825041bd87dfea8b9d1c241d7f6f97a0c0f0f Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Mon, 23 Mar 2015 15:46:19 +0100 Subject: [PATCH] move some functions to helper.js --- helper.js | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ history.html | 1 + history.js | 111 +-------------------------------------------------- 3 files changed, 113 insertions(+), 109 deletions(-) create mode 100644 helper.js diff --git a/helper.js b/helper.js new file mode 100644 index 0000000..3bfdeeb --- /dev/null +++ b/helper.js @@ -0,0 +1,110 @@ +function get(url) { + return new Promise(function(resolve, reject) { + var req = new XMLHttpRequest(); + req.open('GET', url); + + req.onload = function() { + if (req.status == 200) { + resolve(req.response); + } + else { + reject(Error(req.statusText)); + } + }; + + req.onerror = function() { + reject(Error("Network Error")); + }; + + req.send(); + }); +} + +function getJSON(url) { + return get(url).then(JSON.parse) +} + +function sortByKey(key, d) { + return d.slice().sort( function (a, b) { + return a[key] - b[key] + }).reverse() +} + +function limit(key, m, d) { + return d.filter( function (d) { + return d[key].isAfter(m) + }) +} + +function sum(a) { + return a.reduce( function (a, b) { + return a + b + }, 0) +} + +function one() { + return 1 +} + +function trueDefault(d) { + return d === undefined ? true : d +} + +function dictGet(dict, key) { + var k = key.shift() + + if (!(k in dict)) + return null + + if (key.length == 0) + return dict[k] + + return dictGet(dict[k], key) +} + +/* Helpers working with nodes */ + +function offline(d) { + return !d.flags.online +} + +function online(d) { + return d.flags.online +} + +function has_location(d) { + return "location" in d.nodeinfo +} + +function subtract(a, b) { + var ids = {} + + b.forEach( function (d) { + ids[d.nodeinfo.node_id] = true + }) + + return a.filter( function (d) { + return !(d.nodeinfo.node_id in ids) + }) +} + +/* Helpers working with links */ + +function showDistance(d) { + if (isNaN(d.distance)) + return + + return (new Intl.NumberFormat("de-DE", {maximumFractionDigits: 0}).format(d.distance)) + " m" +} + +function showTq(d) { + var opts = { maximumFractionDigits: 0 } + + return (new Intl.NumberFormat("de-DE", opts).format(100/d.tq)) + "%" +} + +function linkId(d) { + var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id] + + return ids.sort().join("-") +} diff --git a/history.html b/history.html index 1b316a6..ec74d4c 100644 --- a/history.html +++ b/history.html @@ -287,6 +287,7 @@ + diff --git a/history.js b/history.js index 6e56629..21e4860 100644 --- a/history.js +++ b/history.js @@ -1,31 +1,5 @@ document.addEventListener('DOMContentLoaded', main) -function get(url) { - return new Promise(function(resolve, reject) { - var req = new XMLHttpRequest(); - req.open('GET', url); - - req.onload = function() { - if (req.status == 200) { - resolve(req.response); - } - else { - reject(Error(req.statusText)); - } - }; - - req.onerror = function() { - reject(Error("Network Error")); - }; - - req.send(); - }); -} - -function getJSON(url) { - return get(url).then(JSON.parse) -} - function main() { getJSON("config.json").then( function (config) { moment.locale("de") @@ -52,42 +26,6 @@ function main() { }) } -function sort(key, d) { - return d.slice().sort( function (a, b) { - return a[key] - b[key] - }).reverse() -} - -function limit(key, m, d) { - return d.filter( function (d) { - return d[key].isAfter(m) - }) -} - -function offline(d) { - return !d.flags.online -} - -function online(d) { - return d.flags.online -} - -function has_location(d) { - return "location" in d.nodeinfo -} - -function subtract(a, b) { - var ids = {} - - b.forEach( function (d) { - ids[d.nodeinfo.node_id] = true - }) - - return a.filter( function (d) { - return !(d.nodeinfo.node_id in ids) - }) -} - function handle_data(config, sidebar, infobox, map, gotoAnything) { return function (data) { var nodedict = data[0] @@ -105,8 +43,8 @@ function handle_data(config, sidebar, infobox, map, gotoAnything) { var now = moment() var age = moment(now).subtract(14, 'days') - var newnodes = limit("firstseen", age, sort("firstseen", nodes).filter(online)) - var lostnodes = limit("lastseen", age, sort("lastseen", nodes).filter(offline)) + 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) @@ -206,25 +144,6 @@ function mkSidebar(el) { return container } -function showDistance(d) { - if (isNaN(d.distance)) - return - - return (new Intl.NumberFormat("de-DE", {maximumFractionDigits: 0}).format(d.distance)) + " m" -} - -function showTq(d) { - var opts = { maximumFractionDigits: 0 } - - return (new Intl.NumberFormat("de-DE", opts).format(100/d.tq)) + "%" -} - -function linkId(d) { - var ids = [d.source.node.nodeinfo.node_id, d.target.node.nodeinfo.node_id] - - return ids.sort().join("-") -} - function mkmap(map, sidebar, now, newnodes, lostnodes, onlinenodes, graph, gotoAnything) { function mkMarker(dict, iconFunc) { return function (d) { @@ -464,16 +383,6 @@ function mkNodesList(el, showContact, tf, gotoProxy, title, list) { el.appendChild(table) } -function sum(a) { - return a.reduce( function (a, b) { - return a + b - }, 0) -} - -function one() { - return 1 -} - function showMeshstats(el, nodes) { var h2 = document.createElement("h2") h2.textContent = "Übersicht" @@ -820,10 +729,6 @@ function gotoHistory(gotoAnything, dict, s) { } } -function trueDefault(d) { - return d === undefined ? true : d -} - function gotoBuilder(config, infobox, nodes, links) { var markers = {} var self = this @@ -870,15 +775,3 @@ function gotoBuilder(config, infobox, nodes, links) { return this } - -function dictGet(dict, key) { - var k = key.shift() - - if (!(k in dict)) - return null - - if (key.length == 0) - return dict[k] - - return dictGet(dict[k], key) -}