gui.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. "use strict"
  2. define([ "lib/gui/nodeinfo"
  3. , "lib/gui/statistics"
  4. , "lib/gui/neighbours"
  5. , "lib/gui/menu"
  6. , "lib/streams"
  7. , "lib/neighbourstream"
  8. ], function ( NodeInfo
  9. , Statistics
  10. , Neighbours
  11. , Menu
  12. , Streams
  13. , NeighbourStream
  14. ) {
  15. function VerticalSplit(parent) {
  16. var el = document.createElement("div")
  17. el.className = "vertical-split"
  18. parent.appendChild(el)
  19. el.push = function (child) {
  20. var header = document.createElement("h2")
  21. header.appendChild(child.title)
  22. var div = document.createElement("div")
  23. div.className = "frame"
  24. div.node = child
  25. div.appendChild(header)
  26. el.appendChild(div)
  27. child.render(div)
  28. return function () {
  29. div.node.destroy()
  30. el.removeChild(div)
  31. }
  32. }
  33. el.clear = function () {
  34. while (el.firstChild) {
  35. el.firstChild.node.destroy()
  36. el.removeChild(el.firstChild)
  37. }
  38. }
  39. return el
  40. }
  41. var h1
  42. return function (mgmtBus, nodesBus) {
  43. function setTitle(node, state) {
  44. var title = node ? node.hostname : "(not connected)"
  45. document.title = title
  46. h1.textContent = title
  47. var icon = document.createElement("i")
  48. icon.className = "icon-down-dir"
  49. h1.appendChild(icon)
  50. switch (state) {
  51. case "connect":
  52. stateIcon.className = "icon-arrows-cw animate-spin"
  53. break
  54. case "fail":
  55. stateIcon.className = "icon-attention"
  56. break
  57. default:
  58. stateIcon.className = ""
  59. break
  60. }
  61. }
  62. var nodes = []
  63. function nodeMenu() {
  64. var myNodes = nodes.slice()
  65. myNodes.sort(function (a, b) {
  66. a = a.hostname
  67. b = b.hostname
  68. return (a < b) ? -1 : (a > b)
  69. })
  70. var menu = myNodes.map(function (d) {
  71. return [d.hostname, function () {
  72. mgmtBus.pushEvent("goto", d)
  73. }]
  74. })
  75. new Menu(menu).apply(this)
  76. }
  77. var header = document.createElement("header")
  78. h1 = document.createElement("h1")
  79. header.appendChild(h1)
  80. h1.onclick = nodeMenu
  81. var icons = document.createElement("p")
  82. icons.className = "icons"
  83. header.appendChild(icons)
  84. var stateIcon = document.createElement("i")
  85. icons.appendChild(stateIcon)
  86. document.body.appendChild(header)
  87. var container = document.createElement("div")
  88. container.className = "container"
  89. document.body.appendChild(container)
  90. setTitle()
  91. var content = new VerticalSplit(container)
  92. function nodeChanged(nodeInfo) {
  93. setTitle(nodeInfo, "connect")
  94. content.clear()
  95. content.push(new NodeInfo(nodeInfo))
  96. }
  97. function nodeNotArrived(nodeInfo) {
  98. setTitle(nodeInfo, "fail")
  99. }
  100. function nodeArrived(nodeInfo, ip) {
  101. setTitle(nodeInfo)
  102. var neighbourStream = new NeighbourStream(mgmtBus, nodesBus, ip)
  103. var statisticsStream = new Streams.Statistics(ip)
  104. content.push(new Statistics(statisticsStream))
  105. content.push(new Neighbours(nodeInfo, neighbourStream, mgmtBus))
  106. }
  107. function newNodes(d) {
  108. nodes = []
  109. for (var nodeId in d)
  110. nodes.push(d[nodeId])
  111. }
  112. mgmtBus.onEvent({ "goto": nodeChanged
  113. , "arrived": nodeArrived
  114. , "gotoFailed": nodeNotArrived
  115. })
  116. nodesBus.map(".nodes").onValue(newNodes)
  117. return this
  118. }
  119. })