mxSwimlaneManager.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxSwimlaneManager
  7. *
  8. * Manager for swimlanes and nested swimlanes that sets the size of newly added
  9. * swimlanes to that of their siblings, and propagates changes to the size of a
  10. * swimlane to its siblings, if <siblings> is true, and its ancestors, if
  11. * <bubbling> is true.
  12. *
  13. * Constructor: mxSwimlaneManager
  14. *
  15. * Constructs a new swimlane manager for the given graph.
  16. *
  17. * Arguments:
  18. *
  19. * graph - Reference to the enclosing graph.
  20. */
  21. function mxSwimlaneManager(graph, horizontal, addEnabled, resizeEnabled)
  22. {
  23. this.horizontal = (horizontal != null) ? horizontal : true;
  24. this.addEnabled = (addEnabled != null) ? addEnabled : true;
  25. this.resizeEnabled = (resizeEnabled != null) ? resizeEnabled : true;
  26. this.addHandler = mxUtils.bind(this, function(sender, evt)
  27. {
  28. if (this.isEnabled() && this.isAddEnabled())
  29. {
  30. this.cellsAdded(evt.getProperty('cells'));
  31. }
  32. });
  33. this.resizeHandler = mxUtils.bind(this, function(sender, evt)
  34. {
  35. if (this.isEnabled() && this.isResizeEnabled())
  36. {
  37. this.cellsResized(evt.getProperty('cells'));
  38. }
  39. });
  40. this.setGraph(graph);
  41. };
  42. /**
  43. * Extends mxEventSource.
  44. */
  45. mxSwimlaneManager.prototype = new mxEventSource();
  46. mxSwimlaneManager.prototype.constructor = mxSwimlaneManager;
  47. /**
  48. * Variable: graph
  49. *
  50. * Reference to the enclosing <mxGraph>.
  51. */
  52. mxSwimlaneManager.prototype.graph = null;
  53. /**
  54. * Variable: enabled
  55. *
  56. * Specifies if event handling is enabled. Default is true.
  57. */
  58. mxSwimlaneManager.prototype.enabled = true;
  59. /**
  60. * Variable: horizontal
  61. *
  62. * Specifies the orientation of the swimlanes. Default is true.
  63. */
  64. mxSwimlaneManager.prototype.horizontal = true;
  65. /**
  66. * Variable: addEnabled
  67. *
  68. * Specifies if newly added cells should be resized to match the size of their
  69. * existing siblings. Default is true.
  70. */
  71. mxSwimlaneManager.prototype.addEnabled = true;
  72. /**
  73. * Variable: resizeEnabled
  74. *
  75. * Specifies if resizing of swimlanes should be handled. Default is true.
  76. */
  77. mxSwimlaneManager.prototype.resizeEnabled = true;
  78. /**
  79. * Variable: moveHandler
  80. *
  81. * Holds the function that handles the move event.
  82. */
  83. mxSwimlaneManager.prototype.addHandler = null;
  84. /**
  85. * Variable: moveHandler
  86. *
  87. * Holds the function that handles the move event.
  88. */
  89. mxSwimlaneManager.prototype.resizeHandler = null;
  90. /**
  91. * Function: isEnabled
  92. *
  93. * Returns true if events are handled. This implementation
  94. * returns <enabled>.
  95. */
  96. mxSwimlaneManager.prototype.isEnabled = function()
  97. {
  98. return this.enabled;
  99. };
  100. /**
  101. * Function: setEnabled
  102. *
  103. * Enables or disables event handling. This implementation
  104. * updates <enabled>.
  105. *
  106. * Parameters:
  107. *
  108. * enabled - Boolean that specifies the new enabled state.
  109. */
  110. mxSwimlaneManager.prototype.setEnabled = function(value)
  111. {
  112. this.enabled = value;
  113. };
  114. /**
  115. * Function: isHorizontal
  116. *
  117. * Returns <horizontal>.
  118. */
  119. mxSwimlaneManager.prototype.isHorizontal = function()
  120. {
  121. return this.horizontal;
  122. };
  123. /**
  124. * Function: setHorizontal
  125. *
  126. * Sets <horizontal>.
  127. */
  128. mxSwimlaneManager.prototype.setHorizontal = function(value)
  129. {
  130. this.horizontal = value;
  131. };
  132. /**
  133. * Function: isAddEnabled
  134. *
  135. * Returns <addEnabled>.
  136. */
  137. mxSwimlaneManager.prototype.isAddEnabled = function()
  138. {
  139. return this.addEnabled;
  140. };
  141. /**
  142. * Function: setAddEnabled
  143. *
  144. * Sets <addEnabled>.
  145. */
  146. mxSwimlaneManager.prototype.setAddEnabled = function(value)
  147. {
  148. this.addEnabled = value;
  149. };
  150. /**
  151. * Function: isResizeEnabled
  152. *
  153. * Returns <resizeEnabled>.
  154. */
  155. mxSwimlaneManager.prototype.isResizeEnabled = function()
  156. {
  157. return this.resizeEnabled;
  158. };
  159. /**
  160. * Function: setResizeEnabled
  161. *
  162. * Sets <resizeEnabled>.
  163. */
  164. mxSwimlaneManager.prototype.setResizeEnabled = function(value)
  165. {
  166. this.resizeEnabled = value;
  167. };
  168. /**
  169. * Function: getGraph
  170. *
  171. * Returns the graph that this manager operates on.
  172. */
  173. mxSwimlaneManager.prototype.getGraph = function()
  174. {
  175. return this.graph;
  176. };
  177. /**
  178. * Function: setGraph
  179. *
  180. * Sets the graph that the manager operates on.
  181. */
  182. mxSwimlaneManager.prototype.setGraph = function(graph)
  183. {
  184. if (this.graph != null)
  185. {
  186. this.graph.removeListener(this.addHandler);
  187. this.graph.removeListener(this.resizeHandler);
  188. }
  189. this.graph = graph;
  190. if (this.graph != null)
  191. {
  192. this.graph.addListener(mxEvent.ADD_CELLS, this.addHandler);
  193. this.graph.addListener(mxEvent.CELLS_RESIZED, this.resizeHandler);
  194. }
  195. };
  196. /**
  197. * Function: isSwimlaneIgnored
  198. *
  199. * Returns true if the given swimlane should be ignored.
  200. */
  201. mxSwimlaneManager.prototype.isSwimlaneIgnored = function(swimlane)
  202. {
  203. return !this.getGraph().isSwimlane(swimlane);
  204. };
  205. /**
  206. * Function: isCellHorizontal
  207. *
  208. * Returns true if the given cell is horizontal. If the given cell is not a
  209. * swimlane, then the global orientation is returned.
  210. */
  211. mxSwimlaneManager.prototype.isCellHorizontal = function(cell)
  212. {
  213. if (this.graph.isSwimlane(cell))
  214. {
  215. var style = this.graph.getCellStyle(cell);
  216. return mxUtils.getValue(style, mxConstants.STYLE_HORIZONTAL, 1) == 1;
  217. }
  218. return !this.isHorizontal();
  219. };
  220. /**
  221. * Function: cellsAdded
  222. *
  223. * Called if any cells have been added.
  224. *
  225. * Parameters:
  226. *
  227. * cell - Array of <mxCells> that have been added.
  228. */
  229. mxSwimlaneManager.prototype.cellsAdded = function(cells)
  230. {
  231. if (cells != null)
  232. {
  233. var model = this.getGraph().getModel();
  234. model.beginUpdate();
  235. try
  236. {
  237. for (var i = 0; i < cells.length; i++)
  238. {
  239. if (!this.isSwimlaneIgnored(cells[i]))
  240. {
  241. this.swimlaneAdded(cells[i]);
  242. }
  243. }
  244. }
  245. finally
  246. {
  247. model.endUpdate();
  248. }
  249. }
  250. };
  251. /**
  252. * Function: swimlaneAdded
  253. *
  254. * Updates the size of the given swimlane to match that of any existing
  255. * siblings swimlanes.
  256. *
  257. * Parameters:
  258. *
  259. * swimlane - <mxCell> that represents the new swimlane.
  260. */
  261. mxSwimlaneManager.prototype.swimlaneAdded = function(swimlane)
  262. {
  263. var model = this.getGraph().getModel();
  264. var parent = model.getParent(swimlane);
  265. var childCount = model.getChildCount(parent);
  266. var geo = null;
  267. // Finds the first valid sibling swimlane as reference
  268. for (var i = 0; i < childCount; i++)
  269. {
  270. var child = model.getChildAt(parent, i);
  271. if (child != swimlane && !this.isSwimlaneIgnored(child))
  272. {
  273. geo = model.getGeometry(child);
  274. if (geo != null)
  275. {
  276. break;
  277. }
  278. }
  279. }
  280. // Applies the size of the refernece to the newly added swimlane
  281. if (geo != null)
  282. {
  283. var parentHorizontal = (parent != null) ? this.isCellHorizontal(parent) : this.horizontal;
  284. this.resizeSwimlane(swimlane, geo.width, geo.height, parentHorizontal);
  285. }
  286. };
  287. /**
  288. * Function: cellsResized
  289. *
  290. * Called if any cells have been resizes. Calls <swimlaneResized> for all
  291. * swimlanes where <isSwimlaneIgnored> returns false.
  292. *
  293. * Parameters:
  294. *
  295. * cells - Array of <mxCells> whose size was changed.
  296. */
  297. mxSwimlaneManager.prototype.cellsResized = function(cells)
  298. {
  299. if (cells != null)
  300. {
  301. var model = this.getGraph().getModel();
  302. model.beginUpdate();
  303. try
  304. {
  305. // Finds the top-level swimlanes and adds offsets
  306. for (var i = 0; i < cells.length; i++)
  307. {
  308. if (!this.isSwimlaneIgnored(cells[i]))
  309. {
  310. var geo = model.getGeometry(cells[i]);
  311. if (geo != null)
  312. {
  313. var size = new mxRectangle(0, 0, geo.width, geo.height);
  314. var top = cells[i];
  315. var current = top;
  316. while (current != null)
  317. {
  318. top = current;
  319. current = model.getParent(current);
  320. var tmp = (this.graph.isSwimlane(current)) ?
  321. this.graph.getStartSize(current) :
  322. new mxRectangle();
  323. size.width += tmp.width;
  324. size.height += tmp.height;
  325. }
  326. var parentHorizontal = (current != null) ? this.isCellHorizontal(current) : this.horizontal;
  327. this.resizeSwimlane(top, size.width, size.height, parentHorizontal);
  328. }
  329. }
  330. }
  331. }
  332. finally
  333. {
  334. model.endUpdate();
  335. }
  336. }
  337. };
  338. /**
  339. * Function: resizeSwimlane
  340. *
  341. * Called from <cellsResized> for all swimlanes that are not ignored to update
  342. * the size of the siblings and the size of the parent swimlanes, recursively,
  343. * if <bubbling> is true.
  344. *
  345. * Parameters:
  346. *
  347. * swimlane - <mxCell> whose size has changed.
  348. */
  349. mxSwimlaneManager.prototype.resizeSwimlane = function(swimlane, w, h, parentHorizontal)
  350. {
  351. var model = this.getGraph().getModel();
  352. model.beginUpdate();
  353. try
  354. {
  355. var horizontal = this.isCellHorizontal(swimlane);
  356. if (!this.isSwimlaneIgnored(swimlane))
  357. {
  358. var geo = model.getGeometry(swimlane);
  359. if (geo != null)
  360. {
  361. if ((parentHorizontal && geo.height != h) || (!parentHorizontal && geo.width != w))
  362. {
  363. geo = geo.clone();
  364. if (parentHorizontal)
  365. {
  366. geo.height = h;
  367. }
  368. else
  369. {
  370. geo.width = w;
  371. }
  372. model.setGeometry(swimlane, geo);
  373. }
  374. }
  375. }
  376. var tmp = (this.graph.isSwimlane(swimlane)) ?
  377. this.graph.getStartSize(swimlane) :
  378. new mxRectangle();
  379. w -= tmp.width;
  380. h -= tmp.height;
  381. var childCount = model.getChildCount(swimlane);
  382. for (var i = 0; i < childCount; i++)
  383. {
  384. var child = model.getChildAt(swimlane, i);
  385. this.resizeSwimlane(child, w, h, horizontal);
  386. }
  387. }
  388. finally
  389. {
  390. model.endUpdate();
  391. }
  392. };
  393. /**
  394. * Function: destroy
  395. *
  396. * Removes all handlers from the <graph> and deletes the reference to it.
  397. */
  398. mxSwimlaneManager.prototype.destroy = function()
  399. {
  400. this.setGraph(null);
  401. };
  402. __mxOutput.mxSwimlaneManager = typeof mxSwimlaneManager !== 'undefined' ? mxSwimlaneManager : undefined;