move some functions to helper.js

This commit is contained in:
Nils Schneider 2015-03-23 15:46:19 +01:00
parent c9351d1258
commit c9d825041b
3 changed files with 113 additions and 109 deletions

110
helper.js Normal file
View File

@ -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("-")
}

View File

@ -287,6 +287,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tablesort/3.0.2/tablesort.min.js"></script>
<script src="vendor/tablesort.numeric.js"></script>
<script src="helper.js"></script>
<script src="history.js"></script>
</head>
<body>

View File

@ -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)
}