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) {
return while (el.firstChild)
el.removeChild(el.firstChild)
tbody = null
return
}
if (!tbody) {
var h2 = document.createElement("h2") var h2 = document.createElement("h2")
h2.textContent = title h2.textContent = title
el.appendChild(h2) el.appendChild(h2)
var table = document.createElement("table") var table = document.createElement("table")
el.appendChild(table) el.appendChild(table)
var tbody = document.createElement("tbody") tbody = document.createElement("tbody")
tbody.last = V.h("tbody")
list.forEach( function (d) { table.appendChild(tbody)
var time = moment(d[field]).fromNow()
var row = document.createElement("tr")
var td1 = document.createElement("td")
var a = document.createElement("a")
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 span = document.createElement("span")
span.classList.add("icon")
span.classList.add("ion-location")
td1.appendChild(span)
} }
if ("owner" in d.nodeinfo && config.showContact) { var items = list.map( function (d) {
var contact = d.nodeinfo.owner.contact var time = moment(d[field]).from(data.now)
td1.appendChild(document.createTextNode(" " + contact + "")) var td1Content = []
}
var td2 = document.createElement("td") var aClass = ["hostname", d.flags.online ? "online" : "offline"]
td2.textContent = time
row.appendChild(td1) td1Content.push(V.h("a", { className: aClass.join(" "),
row.appendChild(td2) onclick: router.node(d),
tbody.appendChild(row) href: "#"
}, d.nodeinfo.hostname))
if (has_location(d))
td1Content.push(V.h("span", {className: "icon ion-location"}))
if ("owner" in d.nodeinfo && config.showContact)
td1Content.push(" - " + d.nodeinfo.owner.contact)
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