refactor nodeslists into simplenodelist

This commit is contained in:
Nils Schneider 2015-03-25 16:28:36 +01:00
parent 33bcb23cc1
commit a935c765c4
2 changed files with 74 additions and 56 deletions

View File

@ -1,5 +1,5 @@
require(["map", "sidebar", "meshstats", "linklist", "infobox/main"],
function (Map, Sidebar, Meshstats, Linklist, Infobox) {
require(["map", "sidebar", "meshstats", "linklist", "simplenodelist", "infobox/main"],
function (Map, Sidebar, Meshstats, Linklist, SimpleNodelist, Infobox) {
main()
function main() {
@ -22,6 +22,12 @@ function (Map, Sidebar, Meshstats, Linklist, Infobox) {
var meshstats = new Meshstats()
sidebar.add(meshstats)
var newnodeslist = new SimpleNodelist(config, "firstseen", gotoAnything, "Neue Knoten")
sidebar.add(newnodeslist)
var lostnodeslist = new SimpleNodelist(config, "lastseen", gotoAnything, "Verschwundene Knoten")
sidebar.add(lostnodeslist)
var linklist = new Linklist(linkScale, gotoAnything)
sidebar.add(linklist)
@ -30,11 +36,11 @@ function (Map, Sidebar, Meshstats, Linklist, Infobox) {
]
var p = Promise.all(urls.map(getJSON))
p.then(handle_data(config, linkScale, sidebar, meshstats, linklist, infobox, map, gotoAnything))
p.then(handle_data(config, linkScale, sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, gotoAnything))
})
}
function handle_data(config, linkScale, sidebar, meshstats, linklist, infobox, map, gotoAnything) {
function handle_data(config, linkScale, sidebar, meshstats, linklist, newnodeslist, lostnodeslist, infobox, map, gotoAnything) {
return function (data) {
var nodedict = data[0]
var nodes = Object.keys(nodedict.nodes).map(function (key) { return nodedict.nodes[key] })
@ -103,9 +109,8 @@ function (Map, Sidebar, Meshstats, Linklist, Infobox) {
map.setData(linkScale, now, newnodes, lostnodes, onlinenodes, links)
meshstats.setData(nodes)
linklist.setData(links)
mkNodesList(sidebar.container, config.showContact, "firstseen", gotoAnything.node, "Neue Knoten", newnodes)
mkNodesList(sidebar.container, config.showContact, "lastseen", gotoAnything.node, "Verschwundene Knoten", lostnodes)
newnodeslist.setData(newnodes)
lostnodeslist.setData(lostnodes)
var historyDict = { nodes: {}, links: {} }
@ -127,55 +132,6 @@ function (Map, Sidebar, Meshstats, Linklist, Infobox) {
}
}
function mkNodesList(el, showContact, tf, gotoProxy, title, list) {
if (list.length == 0)
return
var h2 = document.createElement("h2")
h2.textContent = title
el.appendChild(h2)
var table = document.createElement("table")
el.appendChild(table)
var tbody = document.createElement("tbody")
list.forEach( function (d) {
var time = moment(d[tf]).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 = gotoProxy(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 && showContact) {
var contact = d.nodeinfo.owner.contact
td1.appendChild(document.createTextNode(" " + contact + ""))
}
var td2 = document.createElement("td")
td2.textContent = time
row.appendChild(td1)
row.appendChild(td2)
tbody.appendChild(row)
})
table.appendChild(tbody)
el.appendChild(table)
}
function pushHistory(d) {
var s = "#!"

62
lib/simplenodelist.js Normal file
View File

@ -0,0 +1,62 @@
define(function () {
return function(config, field, gotoAnything, title) {
var self = this
var el
self.render = function (d) {
el = document.createElement("div")
d.appendChild(el)
}
self.setData = function (list) {
if (list.length == 0)
return
var h2 = document.createElement("h2")
h2.textContent = title
el.appendChild(h2)
var table = document.createElement("table")
el.appendChild(table)
var tbody = document.createElement("tbody")
list.forEach( function (d) {
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 = gotoAnything.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 contact = d.nodeinfo.owner.contact
td1.appendChild(document.createTextNode(" " + contact + ""))
}
var td2 = document.createElement("td")
td2.textContent = time
row.appendChild(td1)
row.appendChild(td2)
tbody.appendChild(row)
})
table.appendChild(tbody)
el.appendChild(table)
}
return self
}
})