4
0

node.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. define(["moment", "numeral", "tablesort", "tablesort.numeric"],
  2. function (moment, numeral, Tablesort) {
  3. function showGeoURI(d) {
  4. function showLatitude(d) {
  5. var suffix = Math.sign(d) > -1 ? "' N" : "' S"
  6. d = Math.abs(d)
  7. var a = Math.floor(d)
  8. var min = (d * 60) % 60
  9. a = (a < 10 ? "0" : "") + a
  10. return a + "° " + numeral(min).format("0.000") + suffix
  11. }
  12. function showLongitude(d) {
  13. var suffix = Math.sign(d) > -1 ? "' E" : "' W"
  14. d = Math.abs(d)
  15. var a = Math.floor(d)
  16. var min = (d * 60) % 60
  17. a = (a < 100 ? "0" + (a < 10 ? "0" : "") : "") + a
  18. return a + "° " + numeral(min).format("0.000") + suffix
  19. }
  20. if (!has_location(d))
  21. return undefined
  22. return function (el) {
  23. var latitude = d.nodeinfo.location.latitude
  24. var longitude = d.nodeinfo.location.longitude
  25. var a = document.createElement("a")
  26. a.textContent = showLatitude(latitude) + " " +
  27. showLongitude(longitude)
  28. a.href = "geo:" + latitude + "," + longitude
  29. el.appendChild(a)
  30. }
  31. }
  32. function showFirmware(d) {
  33. var release = dictGet(d.nodeinfo, ["software", "firmware", "release"])
  34. var base = dictGet(d.nodeinfo, ["software", "firmware", "base"])
  35. if (release === null || base === null)
  36. return undefined
  37. return release + " / " + base
  38. }
  39. function showUptime(d) {
  40. if (!("uptime" in d.statistics))
  41. return undefined
  42. return moment.duration(d.statistics.uptime, "seconds").humanize()
  43. }
  44. function showFirstseen(d) {
  45. if (!("firstseen" in d))
  46. return undefined
  47. return d.firstseen.fromNow(true)
  48. }
  49. function showClients(d) {
  50. if (!d.flags.online)
  51. return undefined
  52. return function (el) {
  53. el.appendChild(document.createTextNode(d.statistics.clients > 0 ? d.statistics.clients : "keine"))
  54. el.appendChild(document.createElement("br"))
  55. var span = document.createElement("span")
  56. span.classList.add("clients")
  57. span.textContent = " ".repeat(d.statistics.clients)
  58. el.appendChild(span)
  59. }
  60. }
  61. function showIPs(d) {
  62. var ips = dictGet(d.nodeinfo, ["network", "addresses"])
  63. if (ips === null)
  64. return undefined
  65. ips.sort()
  66. return function (el) {
  67. ips.forEach( function (ip, i) {
  68. var link = !ip.startsWith("fe80:")
  69. if (i > 0)
  70. el.appendChild(document.createElement("br"))
  71. if (link) {
  72. var a = document.createElement("a")
  73. a.href = "http://[" + ip + "]/"
  74. a.textContent = ip
  75. el.appendChild(a)
  76. } else
  77. el.appendChild(document.createTextNode(ip))
  78. })
  79. }
  80. }
  81. function showBar(className, v) {
  82. var span = document.createElement("span")
  83. span.classList.add("bar")
  84. span.classList.add(className)
  85. var bar = document.createElement("span")
  86. bar.style.width = (v * 100) + "%"
  87. span.appendChild(bar)
  88. var label = document.createElement("label")
  89. label.textContent = (Math.round(v * 100)) + " %"
  90. span.appendChild(label)
  91. return span
  92. }
  93. function showRAM(d) {
  94. if (!("memory_usage" in d.statistics))
  95. return undefined
  96. return function (el) {
  97. el.appendChild(showBar("memory-usage", d.statistics.memory_usage))
  98. }
  99. }
  100. function showAutoupdate(d) {
  101. var au = dictGet(d.nodeinfo, ["software", "autoupdater"])
  102. if (!au)
  103. return undefined
  104. return au.enabled ? "aktiviert (" + au.branch + ")" : "deaktiviert"
  105. }
  106. function showStatImg(o, nodeId) {
  107. var content, caption
  108. if (o.thumbnail) {
  109. content = document.createElement("img")
  110. content.src = o.thumbnail.replace("{NODE_ID}", nodeId)
  111. }
  112. if (o.caption) {
  113. caption = o.caption.replace("{NODE_ID}", nodeId)
  114. if (!content)
  115. content = document.createTextNode(caption)
  116. }
  117. var p = document.createElement("p")
  118. if (o.href) {
  119. var link = document.createElement("a")
  120. link.target = "_blank"
  121. link.href = o.href.replace("{NODE_ID}", nodeId)
  122. link.appendChild(content)
  123. if (caption && o.thumbnail)
  124. link.title = caption
  125. p.appendChild(link)
  126. } else
  127. p.appendChild(content)
  128. return p
  129. }
  130. return function(config, el, router, d) {
  131. var h2 = document.createElement("h2")
  132. h2.textContent = d.nodeinfo.hostname
  133. var span = document.createElement("span")
  134. span.classList.add(d.flags.online ? "online" : "offline")
  135. span.textContent = " (" + (d.flags.online ? "online" : "offline, " + d.lastseen.fromNow(true)) + ")"
  136. h2.appendChild(span)
  137. el.appendChild(h2)
  138. var attributes = document.createElement("table")
  139. attributes.classList.add("attributes")
  140. attributeEntry(attributes, "Gateway", d.flags.gateway ? "ja" : null)
  141. attributeEntry(attributes, "Koordinaten", showGeoURI(d))
  142. if (config.showContact)
  143. attributeEntry(attributes, "Kontakt", dictGet(d.nodeinfo, ["owner", "contact"]))
  144. attributeEntry(attributes, "Hardware", dictGet(d.nodeinfo, ["hardware", "model"]))
  145. attributeEntry(attributes, "Primäre MAC", dictGet(d.nodeinfo, ["network", "mac"]))
  146. attributeEntry(attributes, "Node ID", dictGet(d.nodeinfo, ["node_id"]))
  147. attributeEntry(attributes, "Firmware", showFirmware(d))
  148. attributeEntry(attributes, "Uptime", showUptime(d))
  149. attributeEntry(attributes, "Teil des Netzes", showFirstseen(d))
  150. attributeEntry(attributes, "Arbeitsspeicher", showRAM(d))
  151. attributeEntry(attributes, "IP Adressen", showIPs(d))
  152. attributeEntry(attributes, "Autom. Updates", showAutoupdate(d))
  153. attributeEntry(attributes, "Clients", showClients(d))
  154. el.appendChild(attributes)
  155. if (config.nodeInfos)
  156. config.nodeInfos.forEach( function (nodeInfo) {
  157. var h4 = document.createElement("h4")
  158. h4.textContent = nodeInfo.name
  159. el.appendChild(h4)
  160. el.appendChild(showStatImg(nodeInfo, d.nodeinfo.node_id))
  161. })
  162. if (d.neighbours.length > 0) {
  163. var h3 = document.createElement("h3")
  164. h3.textContent = "Nachbarknoten (" + d.neighbours.length + ")"
  165. el.appendChild(h3)
  166. var table = document.createElement("table")
  167. var thead = document.createElement("thead")
  168. var tr = document.createElement("tr")
  169. var th1 = document.createElement("th")
  170. th1.textContent = "Knoten"
  171. th1.classList.add("sort-default")
  172. tr.appendChild(th1)
  173. var th2 = document.createElement("th")
  174. th2.textContent = "TQ"
  175. tr.appendChild(th2)
  176. var th3 = document.createElement("th")
  177. th3.textContent = "Entfernung"
  178. tr.appendChild(th3)
  179. thead.appendChild(tr)
  180. table.appendChild(thead)
  181. var tbody = document.createElement("tbody")
  182. d.neighbours.forEach( function (d) {
  183. var tr = document.createElement("tr")
  184. var td1 = document.createElement("td")
  185. var a1 = document.createElement("a")
  186. a1.classList.add("hostname")
  187. a1.textContent = d.node.nodeinfo.hostname
  188. a1.href = "#"
  189. a1.onclick = router.node(d.node)
  190. td1.appendChild(a1)
  191. if (d.link.vpn)
  192. td1.appendChild(document.createTextNode(" (VPN)"))
  193. if (has_location(d.node)) {
  194. var span = document.createElement("span")
  195. span.classList.add("icon")
  196. span.classList.add("ion-location")
  197. td1.appendChild(span)
  198. }
  199. tr.appendChild(td1)
  200. var td2 = document.createElement("td")
  201. var a2 = document.createElement("a")
  202. a2.href = "#"
  203. a2.textContent = showTq(d.link)
  204. a2.onclick = router.link(d.link)
  205. td2.appendChild(a2)
  206. tr.appendChild(td2)
  207. var td3 = document.createElement("td")
  208. var a3 = document.createElement("a")
  209. a3.href = "#"
  210. a3.textContent = showDistance(d.link)
  211. a3.onclick = router.link(d.link)
  212. td3.appendChild(a3)
  213. td3.setAttribute("data-sort", d.link.distance !== undefined ? -d.link.distance : 1)
  214. tr.appendChild(td3)
  215. tbody.appendChild(tr)
  216. })
  217. table.appendChild(tbody)
  218. new Tablesort(table)
  219. el.appendChild(table)
  220. }
  221. }
  222. })