hopglass/lib/nodelist.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-04-03 02:32:32 +02:00
define(["virtual-dom"],
function (V) {
2015-03-26 01:31:46 +01:00
return function(router) {
2015-04-02 21:28:21 +02:00
function showUptime(now, d) {
2015-03-29 16:14:10 +02:00
var uptime
if (d.flags.online && "uptime" in d.statistics)
uptime = Math.round(d.statistics.uptime / 3600)
else if (!d.flags.online && "lastseen" in d)
uptime = Math.round(-(now - d.lastseen) / 3600000)
var s = ""
if (uptime !== undefined)
if (Math.abs(uptime) >= 24)
s = Math.round(uptime / 24) + "d"
else
s = uptime + "h"
2015-04-02 21:28:21 +02:00
return {v: s, sort: uptime !== undefined ? -uptime : 0}
2015-03-29 16:14:10 +02:00
}
2015-03-26 01:31:46 +01:00
var self = this
2015-04-03 02:32:32 +02:00
var el, tbody
2015-03-26 01:31:46 +01:00
2015-03-29 16:14:10 +02:00
self.render = function (d) {
2015-03-26 01:31:46 +01:00
el = document.createElement("div")
d.appendChild(el)
}
2015-03-29 17:48:25 +02:00
self.setData = function (data) {
if (data.nodes.all.length === 0)
2015-03-26 01:31:46 +01:00
return
2015-04-02 21:28:21 +02:00
if (!tbody) {
var h2 = document.createElement("h2")
h2.textContent = "Alle Knoten"
el.appendChild(h2)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var table = document.createElement("table")
el.appendChild(table)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var thead = document.createElement("thead")
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var tr = document.createElement("tr")
var th1 = document.createElement("th")
th1.textContent = "Knoten"
tr.appendChild(th1)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var th2 = document.createElement("th")
th2.textContent = "Uptime"
tr.appendChild(th2)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var th3 = document.createElement("th")
th3.textContent = "Clients"
tr.appendChild(th3)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
thead.appendChild(tr)
table.appendChild(thead)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
tbody = document.createElement("tbody")
tbody.last = V.h("tbody")
table.appendChild(tbody)
}
2015-03-26 01:31:46 +01:00
2015-04-03 02:32:32 +02:00
var nodes = data.nodes.all.slice(0).sort( function (a, b) {
return a.nodeinfo.hostname.localeCompare(b.nodeinfo.hostname)
})
2015-03-26 01:31:46 +01:00
2015-04-03 02:32:32 +02:00
var items = nodes.map( function (d) {
2015-04-02 21:28:21 +02:00
var td1Content = []
var aClass = ["hostname", d.flags.online ? "online" : "offline"]
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
td1Content.push(V.h("a", { className: aClass.join(" "),
onclick: router.node(d),
href: "#"
}, d.nodeinfo.hostname))
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
if (has_location(d))
td1Content.push(V.h("span", {className: "icon ion-location"}))
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var uptime = showUptime(data.now, d)
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var td1 = V.h("td", td1Content)
2015-04-03 02:32:32 +02:00
var td2 = V.h("td", uptime.v)
2015-04-02 21:28:21 +02:00
var td3 = V.h("td", "clients" in d.statistics ? d.statistics.clients : "")
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
return V.h("tr", [td1, td2, td3])
})
2015-03-26 01:31:46 +01:00
2015-04-02 21:28:21 +02:00
var tbodyNew = V.h("tbody", items)
tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
tbody.last = tbodyNew
2015-03-29 16:14:10 +02:00
}
2015-03-26 01:31:46 +01:00
}
})