mxWebColaLayout.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /**
  2. * Copyright (c) 2006-2018, JGraph Ltd
  3. * Copyright (c) 2006-2018, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxWebColaLayout
  7. *
  8. * Extends <mxGraphLayout> to implement a WebCola-based layout.
  9. *
  10. * Example:
  11. *
  12. * (code)
  13. * var layout = new mxWebColaLayout(graph);
  14. * layout.execute(graph.getDefaultParent());
  15. * (end)
  16. *
  17. * Constructor: mxWebColaLayout
  18. *
  19. * Constructs a new WebCola-based layout for the graph.
  20. *
  21. * Arguments:
  22. *
  23. * graph - <mxGraph> that contains the cells.
  24. *
  25. **/
  26. function mxWebColaLayout(graph, layoutType)
  27. /**
  28. * Constructs a WebCola-based layout
  29. * @param graph <mxGraph> that contains the cells.
  30. * @param layoutType Type of WebCola layout
  31. */
  32. {
  33. mxGraphLayout.call(this, graph);
  34. this.layoutType = layoutType;
  35. this.originalGeometries = new mxDictionary();
  36. };
  37. mxWebColaLayout.prototype = new mxGraphLayout();
  38. mxWebColaLayout.prototype.constructor = mxWebColaLayout;
  39. mxWebColaLayout.prototype.layoutType = null;
  40. mxWebColaLayout.prototype.execute = function(parent)
  41. /**
  42. * Runs re-layouting of the portion of a graph from a given starting cell
  43. * @param parent starting cell
  44. */
  45. {
  46. var movableVertices = this.getReachableVertices(parent);
  47. var ps = this.graph.getPageSize();
  48. this.layout = new mxWebColaAdaptor(this.graph, (this.graph.pageVisible) ?
  49. [ps.width, ps.height] : [800, 800], movableVertices);
  50. var initial = true;
  51. var update = function (isUndoable) {
  52. // console.log("mxColaLayout: update");
  53. this.updateGraph(isUndoable, initial);
  54. initial = false;
  55. }.bind(this);
  56. this.layout.updatePositions = update;
  57. //this.resetGraph(this.graph);
  58. var finalLayout = this.computePositions(this.layout);
  59. };
  60. mxWebColaLayout.prototype.getReachableVertices = function(parent)
  61. /***
  62. * Finds all vertices reachable from a given parent
  63. * @param parent starting cell of a search
  64. * @returns dictionary of vertice IDs that are reachable in the form of {id: True}
  65. * if undefined is returned, it means parent was not provided and it should be interpreted as all vertices
  66. * are reachable
  67. */
  68. {
  69. if (parent == undefined)
  70. return undefined;
  71. // first, get all incidental edges in a sub-tree (or a connected component if with loops)
  72. var edges = this.graph.getEdges(parent, null, true, true, true, true);
  73. // now all vertices that are reachable are subject to change; unreachable should be fixed
  74. var reachableVertices = void(0);
  75. if (edges.length != 0)
  76. {
  77. var reachableVertices = {};
  78. for (var i = 0; i < edges.length; i++)
  79. {
  80. var edge = edges[i];
  81. reachableVertices[edge.source.id] = true;
  82. reachableVertices[edge.target.id] = true;
  83. }
  84. }
  85. // vertices connected by returned edges must be allowed to move, rest must be fixed in place
  86. return reachableVertices;
  87. }
  88. mxWebColaLayout.prototype.computePositions = function()
  89. /**
  90. * Executes layout to compute positions
  91. */
  92. {
  93. return this.layout.run();
  94. }
  95. mxWebColaLayout.prototype.resetGraph = function(graph)
  96. /**
  97. * Resets initial vertex positions
  98. */
  99. {
  100. var model = graph.getModel();
  101. var cells = model.cells;
  102. var view = graph.getView();
  103. model.beginUpdate();
  104. try
  105. {
  106. for (var id in cells)
  107. {
  108. var cell = cells[id];
  109. var state = view.getState(cell);
  110. var bounds = view.getBoundingBox(state, true);
  111. var isFirst = true;
  112. if (cell.isVertex()) {
  113. var geometry = model.getGeometry(cell);
  114. if (geometry != null && typeof geometry != "undefined")
  115. {
  116. geometry = geometry.clone();
  117. geometry.offset = null;
  118. model.setGeometry(cell, geometry);
  119. }
  120. }
  121. }
  122. }
  123. finally
  124. {
  125. model.endUpdate();
  126. }
  127. }
  128. mxWebColaLayout.prototype.getGroupBounds = function(model, groupCell)
  129. /**
  130. * Computes bounds of a group as boundary encompassing all children of a group
  131. * @param model graph model
  132. * @param groupCell starting group
  133. * @returns boundaries of all children inside a group
  134. */
  135. {
  136. var minX = 1000000;
  137. var minY = 1000000;
  138. var maxX = -1000000;
  139. var maxY = -1000000;
  140. if (groupCell.children == null || groupCell.children.length == 0)
  141. return null;
  142. var cellsToVisit = [];
  143. cellsToVisit = cellsToVisit.concat(groupCell.children);
  144. while (cellsToVisit.length > 0)
  145. {
  146. var child = cellsToVisit.shift();
  147. if (child.isVertex())
  148. {
  149. if (this.layout.isLeafOrCollapsed(child))
  150. {
  151. var geometry = model.getGeometry(child);
  152. if (geometry != null && typeof geometry != "undefined")
  153. {
  154. if (geometry.x < minX)
  155. {
  156. minX = geometry.x;
  157. }
  158. if (geometry.y < minY)
  159. {
  160. minY = geometry.y;
  161. }
  162. if (geometry.x + geometry.width > maxX)
  163. {
  164. maxX = geometry.x + geometry.width;
  165. }
  166. if (geometry.y + geometry.height > maxY)
  167. {
  168. maxY = geometry.y + geometry.height;
  169. }
  170. }
  171. }
  172. else
  173. {
  174. cellsToVisit = cellsToVisit.concat(child.children);
  175. }
  176. }
  177. }
  178. var bounds = model.getGeometry(groupCell).clone();
  179. bounds.x = minX;
  180. bounds.y = minY;
  181. bounds.width = maxX - minX;
  182. bounds.height = maxY - minY;
  183. return bounds;
  184. }
  185. mxWebColaLayout.prototype.adjustChildOffsets = function(model, groupCell)
  186. /**
  187. * Adjusts offset of child vertices to be relative to parent groups
  188. * @param model graph model
  189. * @param groupCell starting group cell
  190. */
  191. {
  192. if (groupCell.children == null || groupCell.children.length == 0)
  193. return;
  194. var groupBounds = model.getGeometry(groupCell);
  195. var offsetX = groupBounds.x;
  196. var offsetY = groupBounds.y;
  197. var cellsToVisit = [];
  198. cellsToVisit = cellsToVisit.concat(groupCell.children);
  199. while (cellsToVisit.length > 0)
  200. {
  201. var child = cellsToVisit.shift();
  202. if (child.isVertex())
  203. {
  204. if (this.layout.isLeafOrCollapsed(child))
  205. {
  206. var geometry = model.getGeometry(child);
  207. if (geometry != null && typeof geometry != "undefined")
  208. {
  209. geometry = geometry.clone();
  210. geometry.x = geometry.x - offsetX;
  211. geometry.y = geometry.y - offsetY;
  212. model.setGeometry(child, geometry);
  213. }
  214. }
  215. else
  216. {
  217. cellsToVisit = cellsToVisit.concat(child.children);
  218. }
  219. }
  220. }
  221. }
  222. mxWebColaLayout.prototype.updateGraph = function(isUndoable = false, initial = false)
  223. /**
  224. * Updates graph based on layout's vertex/group positions
  225. */
  226. {
  227. // find X, Y ranges first
  228. var minX = 1000000;
  229. var maxX = -1000000;
  230. var minY = 1000000;
  231. var maxY = -1000000;
  232. for (var i = 0; i < this.layout.adaptor._nodes.length; i++)
  233. {
  234. var node = this.layout.adaptor._nodes[i];
  235. var x = node.x;
  236. var y = node.y;
  237. minX = Math.min(minX, x);
  238. minY = Math.min(minY, y);
  239. maxX = Math.max(maxX, x);
  240. maxY = Math.max(maxY, y);
  241. }
  242. var spanX = maxX - minX;
  243. var spanY = maxY - minY;
  244. var model = this.graph.getModel();
  245. if (isUndoable)
  246. {
  247. model.beginUpdate();
  248. }
  249. try
  250. {
  251. var cells = model.cells;
  252. var view = this.graph.getView();
  253. // scan leaves and edges
  254. for (var id in cells)
  255. {
  256. var cell = cells[id];
  257. var state = view.getState(cell);
  258. var bounds = view.getBoundingBox(state, true);
  259. if (cell.isVertex() && this.layout.isLeafOrCollapsed(cell))
  260. {
  261. var nodeId = this.layout.cellToNode[id];
  262. if (typeof nodeId == "undefined")
  263. continue;
  264. var node = this.layout.adaptor._nodes[nodeId];
  265. var geometry = model.getGeometry(cell);
  266. if (geometry != null)
  267. {
  268. // First run creates a temporary geometry that can
  269. // be changed in-place to update the view and keeps
  270. // a copy of the original geometry to use in the
  271. // final undoable edit to force a change event
  272. if (initial)
  273. {
  274. this.originalGeometries.put(cell, geometry);
  275. geometry = geometry.clone();
  276. if (model.isVertex(cell))
  277. {
  278. geometry.offset = null;
  279. }
  280. }
  281. geometry.x = node.x - minX;
  282. geometry.y = node.y - minY;
  283. if (isUndoable)
  284. {
  285. // Restores original geometry for the change to be detected
  286. cell.geometry = this.originalGeometries.get(cell);
  287. model.setGeometry(cell, geometry);
  288. }
  289. else if (initial)
  290. {
  291. cell.geometry = geometry;
  292. }
  293. else
  294. {
  295. this.graph.view.invalidate(cell, true, true);
  296. }
  297. }
  298. else
  299. {
  300. console.log("ERROR: vertex cell id:" + id + " has no geometry!");
  301. }
  302. }
  303. else if (cell.isEdge())
  304. {
  305. this.graph.resetEdge(cell);
  306. }
  307. }
  308. // scan groups
  309. for (var id in cells)
  310. {
  311. var cell = cells[id];
  312. var state = view.getState(cell);
  313. var bounds = view.getBoundingBox(state, true);
  314. if (cell.isVertex() && !this.layout.isLeafOrCollapsed(cell))
  315. {
  316. var bounds = this.getGroupBounds(model, cell);
  317. if (bounds != null && typeof bounds != "undefined")
  318. {
  319. model.setGeometry(cell, bounds);
  320. this.adjustChildOffsets(model, cell);
  321. }
  322. }
  323. }
  324. }
  325. finally
  326. {
  327. if (isUndoable)
  328. {
  329. model.endUpdate();
  330. }
  331. else
  332. {
  333. this.graph.view.validate();
  334. }
  335. // console.log("Updated graph, undoable=" + isUndoable + " undo level=" + this.graph.model.updateLevel);
  336. }
  337. }