make nodelist.setData idempotent

This commit is contained in:
Nils Schneider 2015-04-02 21:28:21 +02:00
parent 7ddfbd964c
commit d3bbf6060e

View file

@ -1,6 +1,7 @@
define(["tablesort", "tablesort.numeric"], function (Tablesort) { define(["tablesort", "virtual-dom", "tablesort.numeric"],
function (Tablesort, V) {
return function(router) { return function(router) {
function showUptime(el, now, d) { function showUptime(now, d) {
var uptime var uptime
if (d.flags.online && "uptime" in d.statistics) if (d.flags.online && "uptime" in d.statistics)
uptime = Math.round(d.statistics.uptime / 3600) uptime = Math.round(d.statistics.uptime / 3600)
@ -15,12 +16,11 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
else else
s = uptime + "h" s = uptime + "h"
el.textContent = s return {v: s, sort: uptime !== undefined ? -uptime : 0}
el.setAttribute("data-sort", uptime !== undefined ? -uptime : 0)
} }
var self = this var self = this
var el var el, tbody, sort
self.render = function (d) { self.render = function (d) {
el = document.createElement("div") el = document.createElement("div")
@ -31,69 +31,66 @@ define(["tablesort", "tablesort.numeric"], function (Tablesort) {
if (data.nodes.all.length === 0) if (data.nodes.all.length === 0)
return return
var h2 = document.createElement("h2") if (!tbody) {
h2.textContent = "Alle Knoten" var h2 = document.createElement("h2")
el.appendChild(h2) h2.textContent = "Alle Knoten"
el.appendChild(h2)
var table = document.createElement("table") var table = document.createElement("table")
var thead = document.createElement("thead") el.appendChild(table)
var tr = document.createElement("tr") var thead = document.createElement("thead")
var th1 = document.createElement("th")
th1.textContent = "Knoten"
th1.classList.add("sort-default")
tr.appendChild(th1)
var th2 = document.createElement("th") var tr = document.createElement("tr")
th2.textContent = "Uptime" var th1 = document.createElement("th")
tr.appendChild(th2) th1.textContent = "Knoten"
th1.classList.add("sort-default")
tr.appendChild(th1)
var th3 = document.createElement("th") var th2 = document.createElement("th")
th3.textContent = "Clients" th2.textContent = "Uptime"
tr.appendChild(th3) tr.appendChild(th2)
thead.appendChild(tr) var th3 = document.createElement("th")
th3.textContent = "Clients"
tr.appendChild(th3)
table.appendChild(thead) thead.appendChild(tr)
table.appendChild(thead)
var tbody = document.createElement("tbody") tbody = document.createElement("tbody")
tbody.last = V.h("tbody")
table.appendChild(tbody)
data.nodes.all.forEach( function (d) { sort = new Tablesort(table)
var row = document.createElement("tr") }
var td1 = document.createElement("td")
var a = document.createElement("a")
a.textContent = d.nodeinfo.hostname
a.href = "#"
a.onclick = router.node(d)
a.classList.add("hostname")
a.classList.add(d.flags.online ? "online" : "offline")
td1.appendChild(a)
row.appendChild(td1)
if (has_location(d)) { var items = data.nodes.all.map( function (d) {
var span = document.createElement("span") var td1Content = []
span.classList.add("icon") var aClass = ["hostname", d.flags.online ? "online" : "offline"]
span.classList.add("ion-location")
td1.appendChild(span)
}
var td2 = document.createElement("td") td1Content.push(V.h("a", { className: aClass.join(" "),
showUptime(td2, data.now, d) onclick: router.node(d),
row.appendChild(td2) href: "#"
}, d.nodeinfo.hostname))
var td3 = document.createElement("td") if (has_location(d))
td3.textContent = "clients" in d.statistics ? d.statistics.clients : "" td1Content.push(V.h("span", {className: "icon ion-location"}))
row.appendChild(td3)
tbody.appendChild(row) var uptime = showUptime(data.now, d)
var td1 = V.h("td", td1Content)
var td2 = V.h("td", {attributes: { "data-sort": uptime.sort }}, uptime.v)
var td3 = V.h("td", "clients" in d.statistics ? d.statistics.clients : "")
return V.h("tr", [td1, td2, td3])
}) })
table.appendChild(tbody) var tbodyNew = V.h("tbody", items)
tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
new Tablesort(table) tbody.last = tbodyNew
sort.refresh()
el.appendChild(table)
} }
} }
}) })