linklist.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. define(["virtual-dom"],
  2. function (V) {
  3. return function(linkScale, router) {
  4. var self = this
  5. var el, tbody
  6. self.render = function (d) {
  7. el = document.createElement("div")
  8. d.appendChild(el)
  9. }
  10. self.setData = function (data) {
  11. if (data.graph.links.length === 0)
  12. return
  13. if (!tbody) {
  14. var h2 = document.createElement("h2")
  15. h2.textContent = "Verbindungen"
  16. el.appendChild(h2)
  17. var table = document.createElement("table")
  18. el.appendChild(table)
  19. var thead = document.createElement("thead")
  20. var tr = document.createElement("tr")
  21. var th1 = document.createElement("th")
  22. th1.textContent = "Knoten"
  23. tr.appendChild(th1)
  24. var th2 = document.createElement("th")
  25. th2.textContent = "TQ"
  26. tr.appendChild(th2)
  27. var th3 = document.createElement("th")
  28. th3.textContent = "Entfernung"
  29. tr.appendChild(th3)
  30. thead.appendChild(tr)
  31. table.appendChild(thead)
  32. tbody = document.createElement("tbody")
  33. tbody.last = V.h("tbody")
  34. table.appendChild(tbody)
  35. }
  36. var links = data.graph.links.slice(0).sort( function (a, b) {
  37. a = a.distance === undefined ? -1 : a.distance
  38. b = b.distance === undefined ? -1 : b.distance
  39. return b - a
  40. })
  41. var items = links.map( function (d) {
  42. var name = d.source.node.nodeinfo.hostname + " – " + d.target.node.nodeinfo.hostname
  43. var td1Content = [V.h("a", {href: "#", onclick: router.link(d)}, name)]
  44. if (d.vpn)
  45. td1Content.push(" (VPN)")
  46. var td1 = V.h("td", td1Content)
  47. var td2 = V.h("td", {style: {color: linkScale(d.tq).hex()}}, showTq(d))
  48. var td3 = V.h("td", showDistance(d))
  49. return V.h("tr", [td1, td2, td3])
  50. })
  51. var tbodyNew = V.h("tbody", items)
  52. tbody = V.patch(tbody, V.diff(tbody.last, tbodyNew))
  53. tbody.last = tbodyNew
  54. }
  55. }
  56. })