2015-03-23 15:46:19 +01:00
|
|
|
function get(url) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2016-06-08 23:23:56 +02:00
|
|
|
var req = new XMLHttpRequest()
|
|
|
|
req.open('GET', url)
|
2015-03-23 15:46:19 +01:00
|
|
|
|
|
|
|
req.onload = function() {
|
|
|
|
if (req.status == 200) {
|
2016-06-08 23:23:56 +02:00
|
|
|
resolve(req.response)
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
else {
|
2016-06-08 23:23:56 +02:00
|
|
|
reject(Error(req.statusText))
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
2016-06-08 23:23:56 +02:00
|
|
|
}
|
2015-03-23 15:46:19 +01:00
|
|
|
|
|
|
|
req.onerror = function() {
|
2016-06-08 23:23:56 +02:00
|
|
|
reject(Error("Network Error"))
|
|
|
|
}
|
2015-03-23 15:46:19 +01:00
|
|
|
|
2016-06-08 23:23:56 +02:00
|
|
|
req.send()
|
|
|
|
})
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-04-01 15:59:05 +02:00
|
|
|
function localStorageTest() {
|
|
|
|
var test = 'test'
|
|
|
|
try {
|
|
|
|
localStorage.setItem(test, test)
|
|
|
|
localStorage.removeItem(test)
|
|
|
|
return true
|
|
|
|
} catch(e) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-16 20:40:08 +01:00
|
|
|
function listReplace(s, subst) {
|
|
|
|
for (key in subst) {
|
2016-06-08 23:23:56 +02:00
|
|
|
var re = new RegExp(key, 'g')
|
2016-04-20 14:04:46 +02:00
|
|
|
s = s.replace(re, subst[key])
|
2016-03-16 20:40:08 +01:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2015-03-23 15:46:19 +01:00
|
|
|
/* Helpers working with nodes */
|
|
|
|
|
|
|
|
function offline(d) {
|
|
|
|
return !d.flags.online
|
|
|
|
}
|
|
|
|
|
|
|
|
function online(d) {
|
|
|
|
return d.flags.online
|
|
|
|
}
|
|
|
|
|
|
|
|
function has_location(d) {
|
2015-04-10 14:51:07 +02:00
|
|
|
return "location" in d.nodeinfo &&
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
|
2015-04-02 02:33:17 +02:00
|
|
|
return numeral(d.distance).format("0,0") + " m"
|
2015-03-23 15:46:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function showTq(d) {
|
2015-04-02 02:33:17 +02: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) {
|
|
|
|
if (value === null || value == undefined)
|
|
|
|
return
|
|
|
|
|
|
|
|
var tr = document.createElement("tr")
|
|
|
|
var th = document.createElement("th")
|
2016-07-04 10:57:04 +02:00
|
|
|
if (typeof label === "string")
|
|
|
|
th.textContent = label
|
|
|
|
|
|
|
|
else
|
|
|
|
th.appendChild(label)
|
|
|
|
|
2015-03-25 11:21:09 +01:00
|
|
|
tr.appendChild(th)
|
|
|
|
|
|
|
|
var td = document.createElement("td")
|
|
|
|
|
|
|
|
if (typeof value == "function")
|
|
|
|
value(td)
|
|
|
|
else
|
|
|
|
td.appendChild(document.createTextNode(value))
|
|
|
|
|
|
|
|
tr.appendChild(td)
|
|
|
|
|
|
|
|
el.appendChild(tr)
|
|
|
|
|
|
|
|
return td
|
|
|
|
}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
function createIframe(opt, width, height) {
|
|
|
|
el = document.createElement("iframe")
|
2016-06-08 23:23:56 +02:00
|
|
|
width = typeof width !== 'undefined' ? width : '525px'
|
|
|
|
height = typeof height !== 'undefined' ? height : '350px'
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
if (opt.src)
|
|
|
|
el.src = opt.src
|
|
|
|
else
|
|
|
|
el.src = opt
|
|
|
|
|
|
|
|
if (opt.frameBorder)
|
|
|
|
el.frameBorder = opt.frameBorder
|
|
|
|
else
|
|
|
|
el.frameBorder = 1
|
|
|
|
|
|
|
|
if (opt.width)
|
|
|
|
el.width = opt.width
|
|
|
|
else
|
|
|
|
el.width = width
|
|
|
|
|
|
|
|
if (opt.height)
|
|
|
|
el.height = opt.height
|
|
|
|
else
|
|
|
|
el.height = height
|
|
|
|
|
|
|
|
el.scrolling = "no"
|
|
|
|
el.seamless = "seamless"
|
|
|
|
|
|
|
|
return el
|
|
|
|
}
|
|
|
|
|
|
|
|
function showStat(o, subst) {
|
|
|
|
var content, caption
|
2016-06-08 23:23:56 +02:00
|
|
|
subst = typeof subst !== 'undefined' ? subst : {}
|
2016-03-16 20:40:08 +01:00
|
|
|
|
|
|
|
if (o.thumbnail) {
|
|
|
|
content = document.createElement("img")
|
|
|
|
content.src = listReplace(o.thumbnail, subst)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o.caption) {
|
|
|
|
caption = listReplace(o.caption, subst)
|
|
|
|
|
|
|
|
if (!content)
|
|
|
|
content = document.createTextNode(caption)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o.iframe) {
|
2016-04-24 19:47:48 +02:00
|
|
|
content = createIframe(o.iframe, o.width, o.height)
|
2016-03-16 20:40:08 +01:00
|
|
|
if (o.iframe.src)
|
|
|
|
content.src = listReplace(o.iframe.src, subst)
|
|
|
|
else
|
|
|
|
content.src = listReplace(o.iframe, subst)
|
|
|
|
}
|
|
|
|
|
|
|
|
var p = document.createElement("p")
|
|
|
|
|
|
|
|
if (o.href) {
|
|
|
|
var link = document.createElement("a")
|
|
|
|
link.target = "_blank"
|
|
|
|
link.href = listReplace(o.href, subst)
|
|
|
|
link.appendChild(content)
|
|
|
|
|
|
|
|
if (caption && o.thumbnail)
|
|
|
|
link.title = caption
|
|
|
|
|
|
|
|
p.appendChild(link)
|
|
|
|
} else
|
|
|
|
p.appendChild(content)
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|