meshviewer/lib/nodelist.js

103 lines
2.5 KiB
JavaScript
Raw Normal View History

define(['sorttable', 'snabbdom', 'helper'], function (SortTable, V, helper) {
'use strict';
V = V.default;
2016-05-27 21:59:01 +00:00
2015-04-07 15:41:17 +00:00
function showUptime(uptime) {
2017-10-26 13:58:28 +00:00
// 1000ms are 1 second and 60 second are 1min: 60 * 1000 = 60000
var s = uptime / 60000;
if (Math.abs(s) < 60) {
return Math.round(s) + ' m';
}
2017-10-26 13:58:28 +00:00
s /= 60;
if (Math.abs(s) < 24) {
return Math.round(s) + ' h';
}
s /= 24;
return Math.round(s) + ' d';
2015-04-07 15:41:17 +00:00
}
2015-03-26 00:31:46 +00:00
var headings = [{
name: ''
}, {
2017-01-28 14:33:13 +00:00
name: 'node.nodes',
sort: function (a, b) {
return a.hostname.localeCompare(b.hostname);
},
reverse: false
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.uptime',
class: 'ion-time',
sort: function (a, b) {
return a.uptime - b.uptime;
},
reverse: true
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.links',
class: 'ion-share-alt',
sort: function (a, b) {
2017-05-09 13:22:32 +00:00
return a.neighbours.length - b.neighbours.length;
},
reverse: true
2017-02-04 15:14:24 +00:00
}, {
2017-01-28 14:33:13 +00:00
name: 'node.clients',
class: 'ion-people',
sort: function (a, b) {
2017-10-29 19:52:17 +00:00
return a.clients - b.clients;
},
reverse: true
}];
2015-03-26 00:31:46 +00:00
return function (router) {
2015-04-07 15:41:17 +00:00
function renderRow(d) {
2017-10-29 19:52:17 +00:00
var td0Content = '';
if (helper.hasLocation(d)) {
td0Content = V.h('span', { props: { className: 'icon ion-location' } });
}
2015-03-26 00:31:46 +00:00
2017-10-29 19:52:17 +00:00
var td1Content = V.h('a', {
props: {
2017-10-29 19:52:17 +00:00
className: ['hostname', d.is_online ? 'online' : 'offline'].join(' '),
href: router.generateLink({ node: d.node_id })
}, on: {
click: function (e) {
router.fullUrl({ node: d.node_id }, e);
}
2017-03-05 11:29:21 +00:00
}
2017-10-29 19:52:17 +00:00
}, d.hostname);
2015-03-26 00:31:46 +00:00
2017-10-29 19:52:17 +00:00
return V.h('tr', [
V.h('td', td0Content),
V.h('td', td1Content),
V.h('td', showUptime(d.uptime)),
V.h('td', d.neighbours.length),
V.h('td', d.clients)
]);
2015-04-07 15:41:17 +00:00
}
2015-03-26 00:31:46 +00:00
var table = new SortTable(headings, 1, renderRow);
2015-03-26 00:31:46 +00:00
this.render = function render(d) {
var h2 = document.createElement('h2');
2017-01-28 14:33:13 +00:00
h2.textContent = _.t('node.all');
d.appendChild(h2);
table.el.elm.classList.add('node-list');
d.appendChild(table.el.elm);
};
2015-03-26 00:31:46 +00:00
this.setData = function setData(d) {
2015-04-07 15:41:17 +00:00
var data = d.nodes.all.map(function (e) {
var n = Object.create(e);
2017-10-26 13:58:28 +00:00
if (e.is_online) {
n.uptime = d.now - new Date(e.uptime).getTime();
} else {
n.uptime = e.lastseen - d.now;
}
2017-05-09 13:22:32 +00:00
n.neighbours = e.neighbours;
return n;
});
2015-03-26 00:31:46 +00:00
table.setData(data);
};
};
});