nodelist.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. define(["tablesort", "virtual-dom", "tablesort.numeric"],
  2. function (Tablesort, V) {
  3. return function(router) {
  4. function showUptime(now, d) {
  5. var uptime
  6. if (d.flags.online && "uptime" in d.statistics)
  7. uptime = Math.round(d.statistics.uptime / 3600)
  8. else if (!d.flags.online && "lastseen" in d)
  9. uptime = Math.round(-(now - d.lastseen) / 3600000)
  10. var s = ""
  11. if (uptime !== undefined)
  12. if (Math.abs(uptime) >= 24)
  13. s = Math.round(uptime / 24) + "d"
  14. else
  15. s = uptime + "h"
  16. return {v: s, sort: uptime !== undefined ? -uptime : 0}
  17. }
  18. var self = this
  19. var el, tbody, sort
  20. self.render = function (d) {
  21. el = document.createElement("div")
  22. d.appendChild(el)
  23. }
  24. self.setData = function (data) {
  25. if (data.nodes.all.length === 0)
  26. return
  27. if (!tbody) {
  28. var h2 = document.createElement("h2")
  29. h2.textContent = "Alle Knoten"
  30. el.appendChild(h2)
  31. var table = document.createElement("table")
  32. el.appendChild(table)
  33. var thead = document.createElement("thead")
  34. var tr = document.createElement("tr")
  35. var th1 = document.createElement("th")
  36. th1.textContent = "Knoten"
  37. th1.classList.add("sort-default")
  38. tr.appendChild(th1)
  39. var th2 = document.createElement("th")
  40. th2.textContent = "Uptime"
  41. tr.appendChild(th2)
  42. var th3 = document.createElement("th")
  43. th3.textContent = "Clients"
  44. tr.appendChild(th3)
  45. thead.appendChild(tr)
  46. table.appendChild(thead)
  47. tbody = document.createElement("tbody")
  48. tbody.last = V.h("tbody")
  49. table.appendChild(tbody)
  50. sort = new Tablesort(table)
  51. }
  52. var items = data.nodes.all.map( function (d) {
  53. var td1Content = []
  54. var aClass = ["hostname", d.flags.online ? "online" : "offline"]
  55. td1Content.push(V.h("a", { className: aClass.join(" "),
  56. onclick: router.node(d),
  57. href: "#"
  58. }, d.nodeinfo.hostname))
  59. if (has_location(d))
  60. td1Content.push(V.h("span", {className: "icon ion-location"}))
  61. var uptime = showUptime(data.now, d)
  62. var td1 = V.h("td", td1Content)
  63. var td2 = V.h("td", {attributes: { "data-sort": uptime.sort }}, uptime.v)
  64. var td3 = V.h("td", "clients" in d.statistics ? d.statistics.clients : "")
  65. return V.h("tr", [td1, td2, td3])
  66. })
  67. var tbodyNew = V.h("tbody", items)
  68. tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
  69. tbody.last = tbodyNew
  70. sort.refresh()
  71. }
  72. }
  73. })