mxCellState.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxCellState
  7. *
  8. * Represents the current state of a cell in a given <mxGraphView>.
  9. *
  10. * For edges, the edge label position is stored in <absoluteOffset>.
  11. *
  12. * The size for oversize labels can be retrieved using the boundingBox property
  13. * of the <text> field as shown below.
  14. *
  15. * (code)
  16. * var bbox = (state.text != null) ? state.text.boundingBox : null;
  17. * (end)
  18. *
  19. * Constructor: mxCellState
  20. *
  21. * Constructs a new object that represents the current state of the given
  22. * cell in the specified view.
  23. *
  24. * Parameters:
  25. *
  26. * view - <mxGraphView> that contains the state.
  27. * cell - <mxCell> that this state represents.
  28. * style - Array of key, value pairs that constitute the style.
  29. */
  30. function mxCellState(view, cell, style)
  31. {
  32. this.view = view;
  33. this.cell = cell;
  34. this.style = (style != null) ? style : {};
  35. this.origin = new mxPoint();
  36. this.absoluteOffset = new mxPoint();
  37. };
  38. /**
  39. * Extends mxRectangle.
  40. */
  41. mxCellState.prototype = new mxRectangle();
  42. mxCellState.prototype.constructor = mxCellState;
  43. /**
  44. * Variable: view
  45. *
  46. * Reference to the enclosing <mxGraphView>.
  47. */
  48. mxCellState.prototype.view = null;
  49. /**
  50. * Variable: cell
  51. *
  52. * Reference to the <mxCell> that is represented by this state.
  53. */
  54. mxCellState.prototype.cell = null;
  55. /**
  56. * Variable: style
  57. *
  58. * Contains an array of key, value pairs that represent the style of the
  59. * cell.
  60. */
  61. mxCellState.prototype.style = null;
  62. /**
  63. * Variable: invalidStyle
  64. *
  65. * Specifies if the style is invalid. Default is false.
  66. */
  67. mxCellState.prototype.invalidStyle = false;
  68. /**
  69. * Variable: invalid
  70. *
  71. * Specifies if the state is invalid. Default is true.
  72. */
  73. mxCellState.prototype.invalid = true;
  74. /**
  75. * Variable: origin
  76. *
  77. * <mxPoint> that holds the origin for all child cells. Default is a new
  78. * empty <mxPoint>.
  79. */
  80. mxCellState.prototype.origin = null;
  81. /**
  82. * Variable: absolutePoints
  83. *
  84. * Holds an array of <mxPoints> that represent the absolute points of an
  85. * edge.
  86. */
  87. mxCellState.prototype.absolutePoints = null;
  88. /**
  89. * Variable: absoluteOffset
  90. *
  91. * <mxPoint> that holds the absolute offset. For edges, this is the
  92. * absolute coordinates of the label position. For vertices, this is the
  93. * offset of the label relative to the top, left corner of the vertex.
  94. */
  95. mxCellState.prototype.absoluteOffset = null;
  96. /**
  97. * Variable: visibleSourceState
  98. *
  99. * Caches the visible source terminal state.
  100. */
  101. mxCellState.prototype.visibleSourceState = null;
  102. /**
  103. * Variable: visibleTargetState
  104. *
  105. * Caches the visible target terminal state.
  106. */
  107. mxCellState.prototype.visibleTargetState = null;
  108. /**
  109. * Variable: terminalDistance
  110. *
  111. * Caches the distance between the end points for an edge.
  112. */
  113. mxCellState.prototype.terminalDistance = 0;
  114. /**
  115. * Variable: length
  116. *
  117. * Caches the length of an edge.
  118. */
  119. mxCellState.prototype.length = 0;
  120. /**
  121. * Variable: segments
  122. *
  123. * Array of numbers that represent the cached length of each segment of the
  124. * edge.
  125. */
  126. mxCellState.prototype.segments = null;
  127. /**
  128. * Variable: shape
  129. *
  130. * Holds the <mxShape> that represents the cell graphically.
  131. */
  132. mxCellState.prototype.shape = null;
  133. /**
  134. * Variable: text
  135. *
  136. * Holds the <mxText> that represents the label of the cell. Thi smay be
  137. * null if the cell has no label.
  138. */
  139. mxCellState.prototype.text = null;
  140. /**
  141. * Variable: unscaledWidth
  142. *
  143. * Holds the unscaled width of the state.
  144. */
  145. mxCellState.prototype.unscaledWidth = null;
  146. /**
  147. * Variable: unscaledHeight
  148. *
  149. * Holds the unscaled height of the state.
  150. */
  151. mxCellState.prototype.unscaledHeight = null;
  152. /**
  153. * Function: getPerimeterBounds
  154. *
  155. * Returns the <mxRectangle> that should be used as the perimeter of the
  156. * cell.
  157. *
  158. * Parameters:
  159. *
  160. * border - Optional border to be added around the perimeter bounds.
  161. * bounds - Optional <mxRectangle> to be used as the initial bounds.
  162. */
  163. mxCellState.prototype.getPerimeterBounds = function(border, bounds)
  164. {
  165. border = border || 0;
  166. bounds = (bounds != null) ? bounds : new mxRectangle(this.x, this.y, this.width, this.height);
  167. if (this.shape != null && this.shape.stencil != null && this.shape.stencil.aspect == 'fixed')
  168. {
  169. var aspect = this.shape.stencil.computeAspect(this.style, bounds.x, bounds.y, bounds.width, bounds.height);
  170. bounds.x = aspect.x;
  171. bounds.y = aspect.y;
  172. bounds.width = this.shape.stencil.w0 * aspect.width;
  173. bounds.height = this.shape.stencil.h0 * aspect.height;
  174. }
  175. if (border != 0)
  176. {
  177. bounds.grow(border);
  178. }
  179. return bounds;
  180. };
  181. /**
  182. * Function: setAbsoluteTerminalPoint
  183. *
  184. * Sets the first or last point in <absolutePoints> depending on isSource.
  185. *
  186. * Parameters:
  187. *
  188. * point - <mxPoint> that represents the terminal point.
  189. * isSource - Boolean that specifies if the first or last point should
  190. * be assigned.
  191. */
  192. mxCellState.prototype.setAbsoluteTerminalPoint = function(point, isSource)
  193. {
  194. if (isSource)
  195. {
  196. if (this.absolutePoints == null)
  197. {
  198. this.absolutePoints = [];
  199. }
  200. if (this.absolutePoints.length == 0)
  201. {
  202. this.absolutePoints.push(point);
  203. }
  204. else
  205. {
  206. this.absolutePoints[0] = point;
  207. }
  208. }
  209. else
  210. {
  211. if (this.absolutePoints == null)
  212. {
  213. this.absolutePoints = [];
  214. this.absolutePoints.push(null);
  215. this.absolutePoints.push(point);
  216. }
  217. else if (this.absolutePoints.length == 1)
  218. {
  219. this.absolutePoints.push(point);
  220. }
  221. else
  222. {
  223. this.absolutePoints[this.absolutePoints.length - 1] = point;
  224. }
  225. }
  226. };
  227. /**
  228. * Function: setCursor
  229. *
  230. * Sets the given cursor on the shape and text shape.
  231. */
  232. mxCellState.prototype.setCursor = function(cursor)
  233. {
  234. if (this.shape != null)
  235. {
  236. this.shape.setCursor(cursor);
  237. }
  238. if (this.text != null)
  239. {
  240. this.text.setCursor(cursor);
  241. }
  242. };
  243. /**
  244. * Function: getVisibleTerminal
  245. *
  246. * Returns the visible source or target terminal cell.
  247. *
  248. * Parameters:
  249. *
  250. * source - Boolean that specifies if the source or target cell should be
  251. * returned.
  252. */
  253. mxCellState.prototype.getVisibleTerminal = function(source)
  254. {
  255. var tmp = this.getVisibleTerminalState(source);
  256. return (tmp != null) ? tmp.cell : null;
  257. };
  258. /**
  259. * Function: getVisibleTerminalState
  260. *
  261. * Returns the visible source or target terminal state.
  262. *
  263. * Parameters:
  264. *
  265. * source - Boolean that specifies if the source or target state should be
  266. * returned.
  267. */
  268. mxCellState.prototype.getVisibleTerminalState = function(source)
  269. {
  270. return (source) ? this.visibleSourceState : this.visibleTargetState;
  271. };
  272. /**
  273. * Function: setVisibleTerminalState
  274. *
  275. * Sets the visible source or target terminal state.
  276. *
  277. * Parameters:
  278. *
  279. * terminalState - <mxCellState> that represents the terminal.
  280. * source - Boolean that specifies if the source or target state should be set.
  281. */
  282. mxCellState.prototype.setVisibleTerminalState = function(terminalState, source)
  283. {
  284. if (source)
  285. {
  286. this.visibleSourceState = terminalState;
  287. }
  288. else
  289. {
  290. this.visibleTargetState = terminalState;
  291. }
  292. };
  293. /**
  294. * Function: getCellBounds
  295. *
  296. * Returns the unscaled, untranslated bounds.
  297. */
  298. mxCellState.prototype.getCellBounds = function()
  299. {
  300. return this.cellBounds;
  301. };
  302. /**
  303. * Function: getPaintBounds
  304. *
  305. * Returns the unscaled, untranslated paint bounds. This is the same as
  306. * <getCellBounds> but with a 90 degree rotation if the shape's
  307. * isPaintBoundsInverted returns true.
  308. */
  309. mxCellState.prototype.getPaintBounds = function()
  310. {
  311. return this.paintBounds;
  312. };
  313. /**
  314. * Function: updateCachedBounds
  315. *
  316. * Updates the cellBounds and paintBounds.
  317. */
  318. mxCellState.prototype.updateCachedBounds = function()
  319. {
  320. var tr = this.view.translate;
  321. var s = this.view.scale;
  322. this.cellBounds = new mxRectangle(this.x / s - tr.x, this.y / s - tr.y, this.width / s, this.height / s);
  323. this.paintBounds = mxRectangle.fromRectangle(this.cellBounds);
  324. if (this.shape != null && this.shape.isPaintBoundsInverted())
  325. {
  326. this.paintBounds.rotate90();
  327. }
  328. };
  329. /**
  330. * Destructor: setState
  331. *
  332. * Copies all fields from the given state to this state.
  333. */
  334. mxCellState.prototype.setState = function(state)
  335. {
  336. this.view = state.view;
  337. this.cell = state.cell;
  338. this.style = state.style;
  339. this.absolutePoints = state.absolutePoints;
  340. this.origin = state.origin;
  341. this.absoluteOffset = state.absoluteOffset;
  342. this.boundingBox = state.boundingBox;
  343. this.terminalDistance = state.terminalDistance;
  344. this.segments = state.segments;
  345. this.length = state.length;
  346. this.x = state.x;
  347. this.y = state.y;
  348. this.width = state.width;
  349. this.height = state.height;
  350. this.unscaledWidth = state.unscaledWidth;
  351. this.unscaledHeight = state.unscaledHeight;
  352. };
  353. /**
  354. * Function: clone
  355. *
  356. * Returns a clone of this <mxPoint>.
  357. */
  358. mxCellState.prototype.clone = function()
  359. {
  360. var clone = new mxCellState(this.view, this.cell, this.style);
  361. // Clones the absolute points
  362. if (this.absolutePoints != null)
  363. {
  364. clone.absolutePoints = [];
  365. for (var i = 0; i < this.absolutePoints.length; i++)
  366. {
  367. clone.absolutePoints[i] = this.absolutePoints[i].clone();
  368. }
  369. }
  370. if (this.origin != null)
  371. {
  372. clone.origin = this.origin.clone();
  373. }
  374. if (this.absoluteOffset != null)
  375. {
  376. clone.absoluteOffset = this.absoluteOffset.clone();
  377. }
  378. if (this.boundingBox != null)
  379. {
  380. clone.boundingBox = this.boundingBox.clone();
  381. }
  382. clone.terminalDistance = this.terminalDistance;
  383. clone.segments = this.segments;
  384. clone.length = this.length;
  385. clone.x = this.x;
  386. clone.y = this.y;
  387. clone.width = this.width;
  388. clone.height = this.height;
  389. clone.unscaledWidth = this.unscaledWidth;
  390. clone.unscaledHeight = this.unscaledHeight;
  391. return clone;
  392. };
  393. /**
  394. * Destructor: destroy
  395. *
  396. * Destroys the state and all associated resources.
  397. */
  398. mxCellState.prototype.destroy = function()
  399. {
  400. this.view.graph.cellRenderer.destroy(this);
  401. };
  402. __mxOutput.mxCellState = typeof mxCellState !== 'undefined' ? mxCellState : undefined;