forcegraph.js 8.9 KB

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