meshviewer/lib/utils/helper.js

184 lines
4.2 KiB
JavaScript
Raw Normal View History

'use strict';
2016-05-27 21:59:01 +00:00
2016-05-26 16:37:24 +00:00
define({
get: function get(url) {
2016-05-26 16:37:24 +00:00
return new Promise(function (resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
2016-05-26 16:37:24 +00:00
req.onload = function onload() {
if (req.status === 200) {
2016-05-26 16:37:24 +00:00
resolve(req.response);
} else {
2016-05-26 16:37:24 +00:00
reject(Error(req.statusText));
}
};
req.onerror = function onerror() {
reject(Error('Network Error'));
2016-05-26 16:37:24 +00:00
};
req.send();
});
},
getJSON: function getJSON(url) {
return require('helper').get(url).then(JSON.parse);
2016-05-26 16:37:24 +00:00
},
sortByKey: function sortByKey(key, d) {
2017-10-31 12:32:39 +00:00
return d.sort(function (a, b) {
return b[key] - a[key];
});
2016-05-26 16:37:24 +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
});
},
sum: function sum(a) {
return a.reduce(function (b, c) {
return b + c;
2016-05-26 16:37:24 +00:00
}, 0);
},
one: function one() {
2016-05-26 16:37:24 +00:00
return 1;
},
dictGet: function dictGet(dict, key) {
2016-05-26 16:37:24 +00:00
var k = key.shift();
if (!(k in dict)) {
return null;
}
if (key.length === 0) {
2016-05-26 16:37:24 +00:00
return dict[k];
}
return this.dictGet(dict[k], key);
},
listReplace: function listReplace(s, subst) {
2016-05-27 21:59:01 +00:00
for (var key in subst) {
if (subst.hasOwnProperty(key)) {
var re = new RegExp(key, 'g');
s = s.replace(re, subst[key]);
}
2016-05-26 16:37:24 +00:00
}
return s;
},
hasLocation: function hasLocation(d) {
return 'location' in d &&
Math.abs(d.location.latitude) < 90 &&
Math.abs(d.location.longitude) < 180;
2016-05-26 16:37:24 +00:00
},
subtract: function subtract(a, b) {
2016-05-26 16:37:24 +00:00
var ids = {};
b.forEach(function (d) {
ids[d.node_id] = true;
2016-05-26 16:37:24 +00:00
});
return a.filter(function (d) {
2017-10-31 12:32:39 +00:00
return !ids[d.node_id];
2016-05-26 16:37:24 +00:00
});
},
/* Helpers working with links */
showDistance: function showDistance(d) {
2016-05-26 16:37:24 +00:00
if (isNaN(d.distance)) {
return '';
2016-05-26 16:37:24 +00:00
}
return d.distance.toFixed(0) + ' m';
2016-05-26 16:37:24 +00:00
},
showTq: function showTq(d) {
return (d * 100).toFixed(0) + '%';
2016-05-26 16:37:24 +00:00
},
2017-11-04 15:23:17 +00:00
attributeEntry: function attributeEntry(V, children, label, value) {
if (value !== undefined) {
if (typeof value !== 'object') {
value = V.h('td', value);
}
2016-05-26 16:37:24 +00:00
2017-11-04 15:23:17 +00:00
children.push(V.h('tr', [
V.h('th', _.t(label)),
value
]));
2016-05-26 16:37:24 +00:00
}
},
showStat: function showStat(V, o, subst) {
var content;
subst = typeof subst !== 'undefined' ? subst : {};
2016-05-26 16:37:24 +00:00
content = V.h('img', { attrs: { src: require('helper').listReplace(o.image, subst) } });
2016-05-26 16:37:24 +00:00
if (o.href) {
return V.h('p', V.h('a', {
attrs:
{
href: require('helper').listReplace(o.href, subst),
target: '_blank',
title: require('helper').listReplace(o.title, subst)
}
}, content));
2016-05-26 16:37:24 +00:00
}
return V.h('p', content);
},
2017-02-05 21:42:59 +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 };
},
positionClients: function positionClients(ctx, p, startAngle, node, startDistance) {
if (node.clients === 0) {
return;
}
var radius = 3;
var a = 1.2;
var mode = 0;
2017-10-29 10:36:29 +00:00
ctx.beginPath();
2017-11-05 17:23:40 +00:00
ctx.fillStyle = config.client.wifi24;
2017-10-29 10:36:29 +00:00
for (var orbit = 0, i = 0; i < node.clients; orbit++) {
var distance = startDistance + orbit * 2 * radius * a;
var n = Math.floor((Math.PI * distance) / (a * radius));
var delta = node.clients - i;
for (var j = 0; j < Math.min(delta, n); i++, j++) {
if (mode !== 1 && i >= (node.clients_wifi24 + node.clients_wifi5)) {
mode = 1;
ctx.fill();
ctx.beginPath();
2017-11-05 17:23:40 +00:00
ctx.fillStyle = config.client.wifi5;
} else if (mode === 0 && i >= node.clients_wifi24) {
mode = 2;
ctx.fill();
ctx.beginPath();
2017-11-05 17:23:40 +00:00
ctx.fillStyle = config.client.other;
}
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);
}
}
2017-10-29 10:36:29 +00:00
ctx.fill();
2016-05-26 16:37:24 +00:00
}
});