mxGraphHierarchyNode.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxGraphHierarchyNode
  7. *
  8. * An abstraction of a hierarchical edge for the hierarchy layout
  9. *
  10. * Constructor: mxGraphHierarchyNode
  11. *
  12. * Constructs an internal node to represent the specified real graph cell
  13. *
  14. * Arguments:
  15. *
  16. * cell - the real graph cell this node represents
  17. */
  18. function mxGraphHierarchyNode(cell)
  19. {
  20. mxGraphAbstractHierarchyCell.apply(this, arguments);
  21. this.cell = cell;
  22. this.id = mxObjectIdentity.get(cell);
  23. this.connectsAsTarget = [];
  24. this.connectsAsSource = [];
  25. };
  26. /**
  27. * Extends mxGraphAbstractHierarchyCell.
  28. */
  29. mxGraphHierarchyNode.prototype = new mxGraphAbstractHierarchyCell();
  30. mxGraphHierarchyNode.prototype.constructor = mxGraphHierarchyNode;
  31. /**
  32. * Variable: cell
  33. *
  34. * The graph cell this object represents.
  35. */
  36. mxGraphHierarchyNode.prototype.cell = null;
  37. /**
  38. * Variable: id
  39. *
  40. * The object identity of the wrapped cell
  41. */
  42. mxGraphHierarchyNode.prototype.id = null;
  43. /**
  44. * Variable: connectsAsTarget
  45. *
  46. * Collection of hierarchy edges that have this node as a target
  47. */
  48. mxGraphHierarchyNode.prototype.connectsAsTarget = null;
  49. /**
  50. * Variable: connectsAsSource
  51. *
  52. * Collection of hierarchy edges that have this node as a source
  53. */
  54. mxGraphHierarchyNode.prototype.connectsAsSource = null;
  55. /**
  56. * Variable: hashCode
  57. *
  58. * Assigns a unique hashcode for each node. Used by the model dfs instead
  59. * of copying HashSets
  60. */
  61. mxGraphHierarchyNode.prototype.hashCode = false;
  62. /**
  63. * Function: getRankValue
  64. *
  65. * Returns the integer value of the layer that this node resides in
  66. */
  67. mxGraphHierarchyNode.prototype.getRankValue = function(layer)
  68. {
  69. return this.maxRank;
  70. };
  71. /**
  72. * Function: getNextLayerConnectedCells
  73. *
  74. * Returns the cells this cell connects to on the next layer up
  75. */
  76. mxGraphHierarchyNode.prototype.getNextLayerConnectedCells = function(layer)
  77. {
  78. if (this.nextLayerConnectedCells == null)
  79. {
  80. this.nextLayerConnectedCells = [];
  81. this.nextLayerConnectedCells[0] = [];
  82. for (var i = 0; i < this.connectsAsTarget.length; i++)
  83. {
  84. var edge = this.connectsAsTarget[i];
  85. if (edge.maxRank == -1 || edge.maxRank == layer + 1)
  86. {
  87. // Either edge is not in any rank or
  88. // no dummy nodes in edge, add node of other side of edge
  89. this.nextLayerConnectedCells[0].push(edge.source);
  90. }
  91. else
  92. {
  93. // Edge spans at least two layers, add edge
  94. this.nextLayerConnectedCells[0].push(edge);
  95. }
  96. }
  97. }
  98. return this.nextLayerConnectedCells[0];
  99. };
  100. /**
  101. * Function: getPreviousLayerConnectedCells
  102. *
  103. * Returns the cells this cell connects to on the next layer down
  104. */
  105. mxGraphHierarchyNode.prototype.getPreviousLayerConnectedCells = function(layer)
  106. {
  107. if (this.previousLayerConnectedCells == null)
  108. {
  109. this.previousLayerConnectedCells = [];
  110. this.previousLayerConnectedCells[0] = [];
  111. for (var i = 0; i < this.connectsAsSource.length; i++)
  112. {
  113. var edge = this.connectsAsSource[i];
  114. if (edge.minRank == -1 || edge.minRank == layer - 1)
  115. {
  116. // No dummy nodes in edge, add node of other side of edge
  117. this.previousLayerConnectedCells[0].push(edge.target);
  118. }
  119. else
  120. {
  121. // Edge spans at least two layers, add edge
  122. this.previousLayerConnectedCells[0].push(edge);
  123. }
  124. }
  125. }
  126. return this.previousLayerConnectedCells[0];
  127. };
  128. /**
  129. * Function: isVertex
  130. *
  131. * Returns true.
  132. */
  133. mxGraphHierarchyNode.prototype.isVertex = function()
  134. {
  135. return true;
  136. };
  137. /**
  138. * Function: getGeneralPurposeVariable
  139. *
  140. * Gets the value of temp for the specified layer
  141. */
  142. mxGraphHierarchyNode.prototype.getGeneralPurposeVariable = function(layer)
  143. {
  144. return this.temp[0];
  145. };
  146. /**
  147. * Function: setGeneralPurposeVariable
  148. *
  149. * Set the value of temp for the specified layer
  150. */
  151. mxGraphHierarchyNode.prototype.setGeneralPurposeVariable = function(layer, value)
  152. {
  153. this.temp[0] = value;
  154. };
  155. /**
  156. * Function: isAncestor
  157. */
  158. mxGraphHierarchyNode.prototype.isAncestor = function(otherNode)
  159. {
  160. // Firstly, the hash code of this node needs to be shorter than the
  161. // other node
  162. if (otherNode != null && this.hashCode != null && otherNode.hashCode != null
  163. && this.hashCode.length < otherNode.hashCode.length)
  164. {
  165. if (this.hashCode == otherNode.hashCode)
  166. {
  167. return true;
  168. }
  169. if (this.hashCode == null || this.hashCode == null)
  170. {
  171. return false;
  172. }
  173. // Secondly, this hash code must match the start of the other
  174. // node's hash code. Arrays.equals cannot be used here since
  175. // the arrays are different length, and we do not want to
  176. // perform another array copy.
  177. for (var i = 0; i < this.hashCode.length; i++)
  178. {
  179. if (this.hashCode[i] != otherNode.hashCode[i])
  180. {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. return false;
  187. };
  188. /**
  189. * Function: getCoreCell
  190. *
  191. * Gets the core vertex associated with this wrapper
  192. */
  193. mxGraphHierarchyNode.prototype.getCoreCell = function()
  194. {
  195. return this.cell;
  196. };