meshviewer/lib/main.js

125 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-03-05 11:29:21 +00:00
define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'],
function (moment, Router, L, GUI, helper, Language) {
'use strict';
2016-05-27 21:59:01 +00:00
return function () {
function handleData(data) {
var timestamp;
var nodes = [];
var links = [];
var nodeDict = {};
for (var i = 0; i < data.length; ++i) {
nodes = nodes.concat(data[i].nodes);
timestamp = data[i].timestamp;
2017-10-27 19:17:43 +00:00
links = links.concat(data[i].links);
}
2015-04-07 19:10:37 +00:00
nodes.forEach(function (node) {
node.firstseen = moment.utc(node.firstseen).local();
node.lastseen = moment.utc(node.lastseen).local();
});
2015-03-24 23:54:00 +00:00
var now = moment();
var age = moment(now).subtract(config.maxAge, 'days');
2015-03-24 23:54:00 +00:00
2017-10-31 12:32:39 +00:00
var online = nodes.filter(function (d) {
return d.is_online;
});
var offline = nodes.filter(function (d) {
return !d.is_online;
});
var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', online));
var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', offline));
2015-03-24 23:54:00 +00:00
2017-02-12 23:31:31 +00:00
nodes.forEach(function (d) {
d.neighbours = [];
nodeDict[d.node_id] = d;
2017-02-12 23:31:31 +00:00
});
links.forEach(function (d) {
d.source = nodeDict[d.source];
d.target = nodeDict[d.target];
2017-10-29 11:36:57 +00:00
d.id = [d.source.node_id, d.target.node_id].join('-');
d.source.neighbours.push({ node: d.target, link: d });
d.target.neighbours.push({ node: d.source, link: d });
2017-02-12 23:31:31 +00:00
try {
d.latlngs = [];
d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude));
d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude));
2017-02-12 23:31:31 +00:00
d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
} catch (e) {
// ignore exception
}
});
return {
now: now,
timestamp: moment.utc(timestamp).local(),
nodes: {
all: nodes,
2017-10-31 12:32:39 +00:00
online: online,
offline: offline,
new: newnodes,
lost: lostnodes
},
links: links,
nodeDict: nodeDict
};
}
2016-05-26 16:37:24 +00:00
var language = new Language();
window.router = new Router(language);
2017-10-31 12:32:39 +00:00
config.dataPath.forEach(function (d, i) {
config.dataPath[i] += 'meshviewer.json';
});
2017-10-31 07:34:18 +00:00
language.init(router);
function update() {
2017-10-31 12:32:39 +00:00
return Promise.all(config.dataPath.map(helper.getJSON))
.then(handleData);
}
update()
2017-11-05 15:58:41 +00:00
.then(function (d) {
return new Promise(function (resolve, reject) {
var count = 0;
(function waitForLanguage() {
if (Object.keys(_.phrases).length > 0) {
resolve(d);
} else if (count > 500) {
reject(new Error('translation not loaded after 10 seconds'));
} else {
setTimeout(waitForLanguage.bind(this), 20);
}
count++;
})();
});
})
.then(function (d) {
var gui = new GUI(language);
gui.setData(d);
router.setData(d);
2017-03-05 11:29:21 +00:00
router.resolve();
window.setInterval(function () {
update().then(function (n) {
gui.setData(n);
router.setData(n);
});
}, 60000);
})
.catch(function (e) {
2017-03-18 17:53:59 +00:00
document.querySelector('.loader').innerHTML += e.message
2017-10-21 00:06:42 +00:00
+ '<br /><br /><button onclick="location.reload(true)" class="btn text" aria-label="Try to reload">Try to reload</button><br /> or report to your community';
2016-05-26 16:37:24 +00:00
console.warn(e);
});
};
});