2015-03-23 15:46:19 +01:00
|
|
|
function get(url) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open('GET', url);
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
req.onload = function () {
|
2015-03-23 15:46:19 +01:00
|
|
|
if (req.status == 200) {
|
2017-03-17 03:14:57 +01:00
|
|
|
resolve(req.response);
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-03-17 03:14:57 +01:00
|
|
|
reject(Error(req.statusText));
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
2017-03-17 03:14:57 +01:00
|
|
|
};
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
req.onerror = function () {
|
|
|
|
reject(Error("Network Error"));
|
|
|
|
};
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
req.send();
|
|
|
|
});
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function getJSON(url) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return get(url).then(JSON.parse);
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function sortByKey(key, d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return d.slice().sort(function (a, b) {
|
|
|
|
return a[key] - b[key];
|
|
|
|
}).reverse();
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function limit(key, m, d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return d.filter(function (d) {
|
|
|
|
return d[key].isAfter(m);
|
|
|
|
});
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function sum(a) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return a.reduce(function (a, b) {
|
|
|
|
return a + b;
|
|
|
|
}, 0);
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function one() {
|
2017-03-17 03:14:57 +01:00
|
|
|
return 1;
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function trueDefault(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return d === undefined ? true : d;
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function dictGet(dict, key) {
|
2017-03-17 03:14:57 +01:00
|
|
|
var k = key.shift();
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (!(k in dict)) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (key.length == 0) {
|
|
|
|
return dict[k];
|
|
|
|
}
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return dictGet(dict[k], key);
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 15:59:05 +02:00
|
|
|
function localStorageTest() {
|
2017-03-17 03:14:57 +01:00
|
|
|
var test = 'test';
|
2015-04-01 15:59:05 +02:00
|
|
|
try {
|
2017-03-17 03:14:57 +01:00
|
|
|
localStorage.setItem(test, test);
|
|
|
|
localStorage.removeItem(test);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
2015-04-01 15:59:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:40:08 +01:00
|
|
|
function listReplace(s, subst) {
|
|
|
|
for (key in subst) {
|
2017-03-17 03:14:57 +01:00
|
|
|
var re = new RegExp(key, 'g');
|
|
|
|
s = s.replace(re, subst[key]);
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
2017-03-17 03:14:57 +01:00
|
|
|
return s;
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|
2015-03-23 15:46:19 +01:00
|
|
|
/* Helpers working with nodes */
|
|
|
|
|
|
|
|
function offline(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return !d.flags.online;
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function online(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return d.flags.online;
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function has_location(d) {
|
2015-04-10 14:51:07 +02:00
|
|
|
return "location" in d.nodeinfo &&
|
2017-03-17 03:14:57 +01:00
|
|
|
Math.abs(d.nodeinfo.location.latitude) < 90 &&
|
|
|
|
Math.abs(d.nodeinfo.location.longitude) < 180;
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function subtract(a, b) {
|
2017-03-17 03:14:57 +01:00
|
|
|
var ids = {};
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
b.forEach(function (d) {
|
|
|
|
ids[d.nodeinfo.node_id] = true;
|
|
|
|
});
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return a.filter(function (d) {
|
|
|
|
return !(d.nodeinfo.node_id in ids);
|
|
|
|
});
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helpers working with links */
|
|
|
|
|
|
|
|
function showDistance(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
if (isNaN(d.distance)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return numeral(d.distance).format("0,0") + " m";
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function showTq(d) {
|
2017-03-17 03:14:57 +01:00
|
|
|
return numeral(1 / d.tq).format("0%");
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 11:21:09 +01:00
|
|
|
/* Infobox stuff (XXX: move to module) */
|
|
|
|
|
|
|
|
function attributeEntry(el, label, value) {
|
2017-03-17 03:14:57 +01:00
|
|
|
if (value === null || value == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
var tr = document.createElement("tr");
|
|
|
|
var th = document.createElement("th");
|
|
|
|
if (typeof label === "string") {
|
|
|
|
th.textContent = label;
|
|
|
|
} else {
|
|
|
|
th.appendChild(label);
|
|
|
|
tr.className = "routerpic";
|
2016-11-06 00:11:49 +01:00
|
|
|
}
|
2016-07-04 10:57:04 +02:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
tr.appendChild(th);
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
var td = document.createElement("td");
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (typeof value == "function") {
|
|
|
|
value(td);
|
|
|
|
} else {
|
|
|
|
td.appendChild(document.createTextNode(value));
|
|
|
|
}
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
tr.appendChild(td);
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
el.appendChild(tr);
|
2015-03-25 11:21:09 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return td;
|
2015-03-25 11:21:09 +01:00
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
function createIframe(opt, width, height) {
|
2017-03-17 03:14:57 +01:00
|
|
|
el = document.createElement("iframe");
|
|
|
|
width = typeof width !== 'undefined' ? width : '100%';
|
|
|
|
height = typeof height !== 'undefined' ? height : '350px';
|
|
|
|
|
|
|
|
if (opt.src) {
|
|
|
|
el.src = opt.src;
|
|
|
|
} else {
|
|
|
|
el.src = opt;
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (opt.frameBorder) {
|
|
|
|
el.frameBorder = opt.frameBorder;
|
|
|
|
} else {
|
|
|
|
el.frameBorder = 1;
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (opt.width) {
|
|
|
|
el.width = opt.width;
|
|
|
|
} else {
|
|
|
|
el.width = width;
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (opt.height) {
|
|
|
|
el.height = opt.height;
|
|
|
|
} else {
|
|
|
|
el.height = height;
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
el.scrolling = "no";
|
|
|
|
el.seamless = "seamless";
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return el;
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function showStat(o, subst) {
|
2017-03-17 03:14:57 +01:00
|
|
|
var content, caption;
|
|
|
|
subst = typeof subst !== 'undefined' ? subst : {};
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
if (o.thumbnail) {
|
2017-03-17 03:14:57 +01:00
|
|
|
content = document.createElement("img");
|
|
|
|
content.src = listReplace(o.thumbnail, subst);
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (o.caption) {
|
2017-03-17 03:14:57 +01:00
|
|
|
caption = listReplace(o.caption, subst);
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (!content) {
|
|
|
|
content = document.createTextNode(caption);
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (o.iframe) {
|
2017-03-17 03:14:57 +01:00
|
|
|
content = createIframe(o.iframe, o.width, o.height);
|
|
|
|
if (o.iframe.src) {
|
|
|
|
content.src = listReplace(o.iframe.src, subst);
|
|
|
|
} else {
|
|
|
|
content.src = listReplace(o.iframe, subst);
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
var p = document.createElement("p");
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
if (o.href) {
|
2017-03-17 03:14:57 +01:00
|
|
|
var link = document.createElement("a");
|
|
|
|
link.target = "_blank";
|
|
|
|
link.href = listReplace(o.href, subst);
|
|
|
|
link.appendChild(content);
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
if (caption && o.thumbnail) {
|
|
|
|
link.title = caption;
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
p.appendChild(link);
|
|
|
|
} else {
|
|
|
|
p.appendChild(content);
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
2017-03-17 03:14:57 +01:00
|
|
|
return p;
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
|