simplenodelist.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. define(["moment", "virtual-dom"], function (moment, V) {
  2. return function(config, nodes, field, router, title) {
  3. var self = this
  4. var el, tbody
  5. self.render = function (d) {
  6. el = document.createElement("div")
  7. d.appendChild(el)
  8. }
  9. self.setData = function (data) {
  10. var list = data.nodes[nodes]
  11. if (list.length === 0) {
  12. while (el.firstChild)
  13. el.removeChild(el.firstChild)
  14. tbody = null
  15. return
  16. }
  17. if (!tbody) {
  18. var h2 = document.createElement("h2")
  19. h2.textContent = title
  20. el.appendChild(h2)
  21. var table = document.createElement("table")
  22. el.appendChild(table)
  23. tbody = document.createElement("tbody")
  24. tbody.last = V.h("tbody")
  25. table.appendChild(tbody)
  26. }
  27. var items = list.map( function (d) {
  28. var time = moment(d[field]).from(data.now)
  29. var td1Content = []
  30. var aClass = ["hostname", d.flags.online ? "online" : "offline"]
  31. td1Content.push(V.h("a", { className: aClass.join(" "),
  32. onclick: router.node(d),
  33. href: "#"
  34. }, d.nodeinfo.hostname))
  35. if (has_location(d))
  36. td1Content.push(V.h("span", {className: "icon ion-location"}))
  37. if ("owner" in d.nodeinfo && config.showContact)
  38. td1Content.push(" - " + d.nodeinfo.owner.contact)
  39. var td1 = V.h("td", td1Content)
  40. var td2 = V.h("td", time)
  41. return V.h("tr", [td1, td2])
  42. })
  43. var tbodyNew = V.h("tbody", items)
  44. tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
  45. tbody.last = tbodyNew
  46. }
  47. return self
  48. }
  49. })