forcegraph.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. define(["d3"], function (d3) {
  2. return function (config, linkScale, sidebar, router) {
  3. var self = this
  4. var svg, vis, link, node
  5. var nodesDict, linksDict
  6. var zoomBehavior
  7. var force
  8. var el
  9. var doAnimation = false
  10. var intNodes = []
  11. var LINK_DISTANCE = 70
  12. function graphDiameter(nodes) {
  13. return Math.sqrt(nodes.length / Math.PI) * LINK_DISTANCE
  14. }
  15. function savePositions() {
  16. if (!localStorageTest())
  17. return
  18. var save = intNodes.map( function (d) {
  19. return { id: d.o.id, x: d.x, y: d.y }
  20. })
  21. localStorage.setItem("graph/nodeposition", JSON.stringify(save))
  22. }
  23. function nodeName(d) {
  24. if (d.o.node && d.o.node.nodeinfo)
  25. return d.o.node.nodeinfo.hostname
  26. else
  27. return d.o.id
  28. }
  29. function dragstart(d) {
  30. d3.event.sourceEvent.stopPropagation()
  31. d.fixed |= 2
  32. }
  33. function dragmove(d) {
  34. d.px = d3.event.x
  35. d.py = d3.event.y
  36. force.resume()
  37. }
  38. function dragend(d) {
  39. d3.event.sourceEvent.stopPropagation()
  40. d.fixed &= 1
  41. }
  42. function animatePanzoom(translate, scale) {
  43. zoomBehavior.scale(scale)
  44. zoomBehavior.translate(translate)
  45. var el = vis
  46. if (doAnimation)
  47. el = el.transition().duration(500)
  48. el.attr("transform", "translate(" + translate + ") " +
  49. "scale(" + scale + ")")
  50. }
  51. function panzoom() {
  52. var translate = zoomBehavior.translate()
  53. var scale = zoomBehavior.scale()
  54. vis.attr("transform", "translate(" + translate + ") " +
  55. "scale(" + scale + ")")
  56. }
  57. function getSize() {
  58. var sidebarWidth = sidebar.getWidth()
  59. var width = el.offsetWidth - sidebarWidth
  60. var height = el.offsetHeight
  61. return [width, height]
  62. }
  63. function panzoomTo(a, b) {
  64. var sidebarWidth = sidebar.getWidth()
  65. var size = getSize()
  66. var targetWidth = Math.max(1, b[0] - a[0])
  67. var targetHeight = Math.max(1, b[1] - a[1])
  68. var scaleX = size[0] / targetWidth
  69. var scaleY = size[1] / targetHeight
  70. var scaleMax = zoomBehavior.scaleExtent()[1]
  71. var scale = 0.5 * Math.min(scaleMax, Math.min(scaleX, scaleY))
  72. var centroid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2]
  73. var x = -centroid[0] * scale + size[0] / 2
  74. var y = -centroid[1] * scale + size[1] / 2
  75. var translate = [x + sidebarWidth, y]
  76. animatePanzoom(translate, scale)
  77. }
  78. function tickEvent() {
  79. link.selectAll("line")
  80. .attr("x1", function(d) { return d.source.x })
  81. .attr("y1", function(d) { return d.source.y })
  82. .attr("x2", function(d) { return d.target.x })
  83. .attr("y2", function(d) { return d.target.y })
  84. node
  85. .attr("cx", function(d) { return d.x })
  86. .attr("cy", function(d) { return d.y })
  87. }
  88. el = document.createElement("div")
  89. el.classList.add("graph")
  90. self.div = el
  91. zoomBehavior = d3.behavior.zoom()
  92. .scaleExtent([1 / 3, 3])
  93. .on("zoom", panzoom)
  94. .translate([sidebar.getWidth(), 0])
  95. svg = d3.select(el).append("svg")
  96. .attr("pointer-events", "all")
  97. .call(zoomBehavior)
  98. vis = svg.append("g")
  99. vis.append("g").attr("class", "links")
  100. vis.append("g").attr("class", "nodes")
  101. force = d3.layout.force()
  102. .charge(-70)
  103. .gravity(0.05)
  104. .linkDistance(LINK_DISTANCE)
  105. .linkStrength(function (d) {
  106. return 1 / d.o.tq
  107. })
  108. .on("tick", tickEvent)
  109. .on("end", savePositions)
  110. panzoom()
  111. var draggableNode = d3.behavior.drag()
  112. .on("dragstart", dragstart)
  113. .on("drag", dragmove)
  114. .on("dragend", dragend)
  115. self.setData = function (data) {
  116. var oldNodes = {}
  117. intNodes.forEach( function (d) {
  118. oldNodes[d.o.id] = d
  119. })
  120. intNodes = data.graph.nodes.map( function (d) {
  121. var e
  122. if (d.id in oldNodes)
  123. e = oldNodes[d.id]
  124. else
  125. e = {}
  126. e.o = d
  127. return e
  128. })
  129. var newNodesDict = {}
  130. intNodes.forEach( function (d) {
  131. newNodesDict[d.o.id] = d
  132. })
  133. var intLinks = data.graph.links.filter( function (d) {
  134. return !d.vpn
  135. }).map( function (d) {
  136. var source = newNodesDict[d.source.id]
  137. var target = newNodesDict[d.target.id]
  138. return {o: d, source: source, target: target}
  139. })
  140. link = vis.select("g.links")
  141. .selectAll("g.link")
  142. .data(links, function (d) { return d.id })
  143. link.exit().remove()
  144. var linkEnter = link.enter().append("g")
  145. .attr("class", "link")
  146. .on("click", function (d) {
  147. if (!d3.event.defaultPrevented)
  148. router.link(d)()
  149. })
  150. linkEnter.append("line")
  151. .append("title")
  152. link.selectAll("line")
  153. .style("stroke", function (d) { return linkScale(d.o.tq).hex() })
  154. link.selectAll("title").text(function (d) { return showTq(d.o) })
  155. linksDict = {}
  156. link.each( function (d) {
  157. if (d.o.source.node && d.o.target.node)
  158. linksDict[d.o.id] = d
  159. })
  160. node = vis.select("g.nodes")
  161. .selectAll(".node")
  162. .data(intNodes, function(d) { return d.o.id })
  163. node.exit().remove()
  164. var nodeEnter = node.enter().append("circle")
  165. .attr("r", 8)
  166. .on("click", function (d) {
  167. if (!d3.event.defaultPrevented)
  168. router.node(d.o.node)()
  169. })
  170. .call(draggableNode)
  171. node.attr("class", function (d) {
  172. var s = ["node"]
  173. if (!d.o.node)
  174. s.push("unknown")
  175. return s.join(" ")
  176. })
  177. nodesDict = {}
  178. node.each( function (d) {
  179. if (d.o.node)
  180. nodesDict[d.o.node.nodeinfo.node_id] = d
  181. })
  182. nodeEnter.append("title")
  183. if (localStorageTest()) {
  184. var save = JSON.parse(localStorage.getItem("graph/nodeposition"))
  185. if (save) {
  186. var nodePositions = {}
  187. save.forEach( function (d) {
  188. nodePositions[d.id] = d
  189. })
  190. nodeEnter.each( function (d) {
  191. if (nodePositions[d.o.id]) {
  192. d.x = nodePositions[d.o.id].x
  193. d.y = nodePositions[d.o.id].y
  194. }
  195. })
  196. }
  197. }
  198. node.selectAll("title").text(nodeName)
  199. var diameter = graphDiameter(intNodes)
  200. force.nodes(intNodes)
  201. .links(intLinks)
  202. .size([diameter, diameter])
  203. if (node.enter().size() + link.enter().size() > 0)
  204. force.start()
  205. }
  206. self.resetView = function () {
  207. node.classed("highlight", false)
  208. link.classed("highlight", false)
  209. panzoomTo([0, 0], force.size())
  210. doAnimation = true
  211. }
  212. self.gotoNode = function (d) {
  213. link.classed("highlight", false)
  214. node.classed("highlight", function (e) {
  215. return e.o.node === d && d !== undefined
  216. })
  217. var n = nodesDict[d.nodeinfo.node_id]
  218. if (n)
  219. panzoomTo([n.x, n.y], [n.x, n.y])
  220. doAnimation = true
  221. }
  222. self.gotoLink = function (d) {
  223. node.classed("highlight", false)
  224. link.classed("highlight", function (e) {
  225. return e.o === d && d !== undefined
  226. })
  227. var l = linksDict[d.id]
  228. if (l) {
  229. var x = d3.extent([l.source, l.target], function (d) { return d.x })
  230. var y = d3.extent([l.source, l.target], function (d) { return d.y })
  231. panzoomTo([x[0], y[0]], [x[1], y[1]])
  232. }
  233. doAnimation = true
  234. }
  235. self.destroy = function () {
  236. force.stop()
  237. node.remove()
  238. link.remove()
  239. svg.remove()
  240. force = null
  241. svg = null
  242. vis = null
  243. link = null
  244. node = null
  245. }
  246. return self
  247. }
  248. })