make meshstats, simplenodelist setData idempotent

This commit is contained in:
Nils Schneider 2015-04-02 19:59:07 +02:00
parent 389291e585
commit 7b57a2245b
4 changed files with 53 additions and 45 deletions

1
app.js
View file

@ -10,6 +10,7 @@ require.config({
"d3": "../bower_components/d3/d3.min", "d3": "../bower_components/d3/d3.min",
"numeral": "../bower_components/numeraljs/min/numeral.min", "numeral": "../bower_components/numeraljs/min/numeral.min",
"numeral-intl": "../bower_components/numeraljs/min/languages.min", "numeral-intl": "../bower_components/numeraljs/min/languages.min",
"virtual-dom": "../bower_components/virtual-dom/dist/virtual-dom",
"helper": "../helper" "helper": "../helper"
}, },
shim: { shim: {

View file

@ -21,7 +21,8 @@
"r.js": "~2.1.16", "r.js": "~2.1.16",
"d3": "~3.5.5", "d3": "~3.5.5",
"numeraljs": "~1.5.3", "numeraljs": "~1.5.3",
"roboto-fontface": "~0.3.0" "roboto-fontface": "~0.3.0",
"virtual-dom": "~2.0.1"
}, },
"authors": [ "authors": [
"Nils Schneider <nils@nilsschneider.net>" "Nils Schneider <nils@nilsschneider.net>"

View file

@ -1,7 +1,7 @@
define(function () { define(function () {
return function () { return function () {
var self = this var self = this
var p var stats, timestamp
self.setData = function (d) { self.setData = function (d) {
var totalNodes = sum(d.nodes.all.filter(online).map(one)) var totalNodes = sum(d.nodes.all.filter(online).map(one))
@ -12,12 +12,11 @@ define(function () {
return d.flags.gateway return d.flags.gateway
}).map(one)) }).map(one))
p.textContent = totalNodes + " Knoten (online), " + stats.textContent = totalNodes + " Knoten (online), " +
totalClients + " Clients, " + totalClients + " Clients, " +
totalGateways + " Gateways" totalGateways + " Gateways"
p.appendChild(document.createElement("br")) timestamp.textContent = "Diese Daten sind von " + d.timestamp.format("LLLL") + "."
p.appendChild(document.createTextNode("Diese Daten sind von " + d.timestamp.format("LLLL") + "."))
} }
self.render = function (el) { self.render = function (el) {
@ -25,8 +24,13 @@ define(function () {
h2.textContent = "Übersicht" h2.textContent = "Übersicht"
el.appendChild(h2) el.appendChild(h2)
p = document.createElement("p") var p = document.createElement("p")
el.appendChild(p) el.appendChild(p)
stats = document.createTextNode("")
p.appendChild(stats)
p.appendChild(document.createElement("br"))
timestamp = document.createTextNode("")
p.appendChild(timestamp)
} }
return self return self

View file

@ -1,7 +1,7 @@
define(["moment"], function (moment) { define(["moment", "virtual-dom"], function (moment, V) {
return function(config, nodes, field, router, title) { return function(config, nodes, field, router, title) {
var self = this var self = this
var el var el, tbody
self.render = function (d) { self.render = function (d) {
el = document.createElement("div") el = document.createElement("div")
@ -11,52 +11,54 @@ define(["moment"], function (moment) {
self.setData = function (data) { self.setData = function (data) {
var list = data.nodes[nodes] var list = data.nodes[nodes]
if (list.length === 0) if (list.length === 0) {
while (el.firstChild)
el.removeChild(el.firstChild)
tbody = null
return return
}
var h2 = document.createElement("h2") if (!tbody) {
h2.textContent = title var h2 = document.createElement("h2")
el.appendChild(h2) h2.textContent = title
var table = document.createElement("table") el.appendChild(h2)
el.appendChild(table)
var tbody = document.createElement("tbody") var table = document.createElement("table")
el.appendChild(table)
list.forEach( function (d) { tbody = document.createElement("tbody")
var time = moment(d[field]).fromNow() tbody.last = V.h("tbody")
table.appendChild(tbody)
}
var row = document.createElement("tr") var items = list.map( function (d) {
var td1 = document.createElement("td") var time = moment(d[field]).from(data.now)
var a = document.createElement("a") var td1Content = []
a.classList.add("hostname")
a.classList.add(d.flags.online ? "online" : "offline")
a.textContent = d.nodeinfo.hostname
a.href = "#"
a.onclick = router.node(d)
td1.appendChild(a)
if (has_location(d)) { var aClass = ["hostname", d.flags.online ? "online" : "offline"]
var span = document.createElement("span")
span.classList.add("icon")
span.classList.add("ion-location")
td1.appendChild(span)
}
if ("owner" in d.nodeinfo && config.showContact) { td1Content.push(V.h("a", { className: aClass.join(" "),
var contact = d.nodeinfo.owner.contact onclick: router.node(d),
td1.appendChild(document.createTextNode(" " + contact + "")) href: "#"
} }, d.nodeinfo.hostname))
var td2 = document.createElement("td") if (has_location(d))
td2.textContent = time td1Content.push(V.h("span", {className: "icon ion-location"}))
row.appendChild(td1) if ("owner" in d.nodeinfo && config.showContact)
row.appendChild(td2) td1Content.push(" - " + d.nodeinfo.owner.contact)
tbody.appendChild(row)
var td1 = V.h("td", td1Content)
var td2 = V.h("td", time)
return V.h("tr", [td1, td2])
}) })
table.appendChild(tbody) var tbodyNew = V.h("tbody", items)
el.appendChild(table) tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
tbody.last = tbodyNew
} }
return self return self