2017-01-29 23:51:08 +00:00
|
|
|
'use strict';
|
2016-05-27 21:59:01 +00:00
|
|
|
|
2016-05-26 16:37:24 +00:00
|
|
|
define({
|
2017-01-29 23:51:08 +00:00
|
|
|
get: function get(url) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var req = new XMLHttpRequest();
|
2017-01-29 23:51:08 +00:00
|
|
|
req.open('GET', url);
|
2016-05-26 16:37:24 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
req.onload = function onload() {
|
|
|
|
if (req.status === 200) {
|
2016-05-26 16:37:24 +00:00
|
|
|
resolve(req.response);
|
2017-01-29 23:51:08 +00:00
|
|
|
} else {
|
2016-05-26 16:37:24 +00:00
|
|
|
reject(Error(req.statusText));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
req.onerror = function onerror() {
|
|
|
|
reject(Error('Network Error'));
|
2016-05-26 16:37:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
req.send();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
getJSON: function getJSON(url) {
|
|
|
|
return require('helper').get(url).then(JSON.parse);
|
2016-05-26 16:37:24 +00:00
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
sortByKey: function sortByKey(key, d) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return d.slice().sort(function (a, b) {
|
|
|
|
return a[key] - b[key];
|
|
|
|
}).reverse();
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
limit: function limit(key, m, d) {
|
|
|
|
return d.filter(function (n) {
|
|
|
|
return n[key].isAfter(m);
|
2016-05-26 16:37:24 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
sum: function sum(a) {
|
|
|
|
return a.reduce(function (b, c) {
|
|
|
|
return b + c;
|
2016-05-26 16:37:24 +00:00
|
|
|
}, 0);
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
one: function one() {
|
2016-05-26 16:37:24 +00:00
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
trueDefault: function trueDefault(d) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return d === undefined ? true : d;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
dictGet: function dictGet(dict, key) {
|
2016-05-26 16:37:24 +00:00
|
|
|
var k = key.shift();
|
|
|
|
|
|
|
|
if (!(k in dict)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
if (key.length === 0) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return dict[k];
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.dictGet(dict[k], key);
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
localStorageTest: function localStorageTest() {
|
|
|
|
var test = 'test';
|
2016-05-26 16:37:24 +00:00
|
|
|
try {
|
|
|
|
localStorage.setItem(test, test);
|
|
|
|
localStorage.removeItem(test);
|
|
|
|
return true;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
listReplace: function listReplace(s, subst) {
|
2016-05-27 21:59:01 +00:00
|
|
|
for (var key in subst) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var re = new RegExp(key, 'g');
|
2016-05-26 16:37:24 +00:00
|
|
|
s = s.replace(re, subst[key]);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Helpers working with nodes */
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
offline: function offline(d) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return !d.flags.online;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
online: function online(d) {
|
2016-05-26 16:37:24 +00:00
|
|
|
return d.flags.online;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
hasLocation: function hasLocation(d) {
|
|
|
|
return 'location' in d.nodeinfo &&
|
2016-05-26 16:37:24 +00:00
|
|
|
Math.abs(d.nodeinfo.location.latitude) < 90 &&
|
|
|
|
Math.abs(d.nodeinfo.location.longitude) < 180;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
subtract: function subtract(a, b) {
|
2016-05-26 16:37:24 +00:00
|
|
|
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 */
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
showDistance: function showDistance(d) {
|
2016-05-26 16:37:24 +00:00
|
|
|
if (isNaN(d.distance)) {
|
2017-01-29 23:51:08 +00:00
|
|
|
return '';
|
2016-05-26 16:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
return d.distance.toFixed(0) + ' m';
|
2016-05-26 16:37:24 +00:00
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
showTq: function showTq(d) {
|
|
|
|
return (1 / d.tq * 100).toFixed(0) + '%';
|
2016-05-26 16:37:24 +00:00
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
attributeEntry: function attributeEntry(el, label, value) {
|
|
|
|
if (value === null || value === undefined) {
|
|
|
|
return '';
|
2016-05-26 16:37:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var tr = document.createElement('tr');
|
|
|
|
var th = document.createElement('th');
|
2017-01-28 14:33:13 +00:00
|
|
|
th.textContent = _.t(label);
|
2016-05-26 16:37:24 +00:00
|
|
|
tr.appendChild(th);
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var td = document.createElement('td');
|
2016-05-26 16:37:24 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
if (typeof value === 'function') {
|
2016-05-26 16:37:24 +00:00
|
|
|
value(td);
|
|
|
|
} else {
|
|
|
|
td.appendChild(document.createTextNode(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
tr.appendChild(td);
|
|
|
|
|
|
|
|
el.appendChild(tr);
|
|
|
|
|
|
|
|
return td;
|
|
|
|
},
|
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
showStat: function showStat(o, subst) {
|
|
|
|
var content;
|
|
|
|
subst = typeof subst !== 'undefined' ? subst : {};
|
2016-05-26 16:37:24 +00:00
|
|
|
|
2017-02-09 23:19:41 +00:00
|
|
|
content = document.createElement('img');
|
|
|
|
content.src = require('helper').listReplace(o.image, subst);
|
2016-05-26 16:37:24 +00:00
|
|
|
|
2017-01-29 23:51:08 +00:00
|
|
|
var p = document.createElement('p');
|
2016-05-26 16:37:24 +00:00
|
|
|
|
|
|
|
if (o.href) {
|
2017-01-29 23:51:08 +00:00
|
|
|
var link = document.createElement('a');
|
|
|
|
link.target = '_blank';
|
|
|
|
link.href = require('helper').listReplace(o.href, subst);
|
2016-05-26 16:37:24 +00:00
|
|
|
link.appendChild(content);
|
|
|
|
|
2017-02-09 23:19:41 +00:00
|
|
|
if (o.title) {
|
|
|
|
link.title = require('helper').listReplace(o.title, subst);
|
2016-05-26 16:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.appendChild(link);
|
|
|
|
} else {
|
|
|
|
p.appendChild(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
2017-02-05 01:34:09 +00:00
|
|
|
},
|
2017-02-05 21:42:59 +00:00
|
|
|
|
2017-02-05 01:34:09 +00:00
|
|
|
getTileBBox: function getTileBBox(s, map, tileSize, margin) {
|
|
|
|
var tl = map.unproject([s.x - margin, s.y - margin]);
|
|
|
|
var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize]);
|
|
|
|
|
2017-02-06 00:50:08 +00:00
|
|
|
return { minX: br.lat, minY: tl.lng, maxX: tl.lat, maxY: br.lng };
|
2017-02-09 08:29:01 +00:00
|
|
|
},
|
|
|
|
positionClients: function positionClients(ctx, p, startAngle, clients, startDistance) {
|
|
|
|
if (clients === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var radius = 3;
|
|
|
|
var a = 1.2;
|
|
|
|
|
|
|
|
for (var orbit = 0, i = 0; i < clients; orbit++) {
|
|
|
|
var distance = startDistance + orbit * 2 * radius * a;
|
|
|
|
var n = Math.floor((Math.PI * distance) / (a * radius));
|
|
|
|
var delta = clients - i;
|
|
|
|
|
|
|
|
for (var j = 0; j < Math.min(delta, n); i++, j++) {
|
|
|
|
var angle = 2 * Math.PI / n * j;
|
|
|
|
var x = p.x + distance * Math.cos(angle + startAngle);
|
|
|
|
var y = p.y + distance * Math.sin(angle + startAngle);
|
|
|
|
|
|
|
|
ctx.moveTo(x, y);
|
|
|
|
ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 16:37:24 +00:00
|
|
|
}
|
|
|
|
});
|