mxCellPath.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. var mxCellPath =
  6. {
  7. /**
  8. * Class: mxCellPath
  9. *
  10. * Implements a mechanism for temporary cell Ids.
  11. *
  12. * Variable: PATH_SEPARATOR
  13. *
  14. * Defines the separator between the path components. Default is ".".
  15. */
  16. PATH_SEPARATOR: '.',
  17. /**
  18. * Function: create
  19. *
  20. * Creates the cell path for the given cell. The cell path is a
  21. * concatenation of the indices of all ancestors on the (finite) path to
  22. * the root, eg. "0.0.0.1".
  23. *
  24. * Parameters:
  25. *
  26. * cell - Cell whose path should be returned.
  27. */
  28. create: function(cell)
  29. {
  30. var result = '';
  31. if (cell != null)
  32. {
  33. var parent = cell.getParent();
  34. while (parent != null)
  35. {
  36. var index = parent.getIndex(cell);
  37. result = index + mxCellPath.PATH_SEPARATOR + result;
  38. cell = parent;
  39. parent = cell.getParent();
  40. }
  41. }
  42. // Removes trailing separator
  43. var n = result.length;
  44. if (n > 1)
  45. {
  46. result = result.substring(0, n - 1);
  47. }
  48. return result;
  49. },
  50. /**
  51. * Function: getParentPath
  52. *
  53. * Returns the path for the parent of the cell represented by the given
  54. * path. Returns null if the given path has no parent.
  55. *
  56. * Parameters:
  57. *
  58. * path - Path whose parent path should be returned.
  59. */
  60. getParentPath: function(path)
  61. {
  62. if (path != null)
  63. {
  64. var index = path.lastIndexOf(mxCellPath.PATH_SEPARATOR);
  65. if (index >= 0)
  66. {
  67. return path.substring(0, index);
  68. }
  69. else if (path.length > 0)
  70. {
  71. return '';
  72. }
  73. }
  74. return null;
  75. },
  76. /**
  77. * Function: resolve
  78. *
  79. * Returns the cell for the specified cell path using the given root as the
  80. * root of the path.
  81. *
  82. * Parameters:
  83. *
  84. * root - Root cell of the path to be resolved.
  85. * path - String that defines the path.
  86. */
  87. resolve: function(root, path)
  88. {
  89. var parent = root;
  90. if (path != null)
  91. {
  92. var tokens = path.split(mxCellPath.PATH_SEPARATOR);
  93. for (var i=0; i<tokens.length; i++)
  94. {
  95. parent = parent.getChildAt(parseInt(tokens[i]));
  96. }
  97. }
  98. return parent;
  99. },
  100. /**
  101. * Function: compare
  102. *
  103. * Compares the given cell paths and returns -1 if p1 is smaller, 0 if
  104. * p1 is equal and 1 if p1 is greater than p2.
  105. */
  106. compare: function(p1, p2)
  107. {
  108. var min = Math.min(p1.length, p2.length);
  109. var comp = 0;
  110. for (var i = 0; i < min; i++)
  111. {
  112. if (p1[i] != p2[i])
  113. {
  114. if (p1[i].length == 0 ||
  115. p2[i].length == 0)
  116. {
  117. comp = (p1[i] == p2[i]) ? 0 : ((p1[i] > p2[i]) ? 1 : -1);
  118. }
  119. else
  120. {
  121. var t1 = parseInt(p1[i]);
  122. var t2 = parseInt(p2[i]);
  123. comp = (t1 == t2) ? 0 : ((t1 > t2) ? 1 : -1);
  124. }
  125. break;
  126. }
  127. }
  128. // Compares path length if both paths are equal to this point
  129. if (comp == 0)
  130. {
  131. var t1 = p1.length;
  132. var t2 = p2.length;
  133. if (t1 != t2)
  134. {
  135. comp = (t1 > t2) ? 1 : -1;
  136. }
  137. }
  138. return comp;
  139. }
  140. };
  141. __mxOutput.mxCellPath = typeof mxCellPath !== 'undefined' ? mxCellPath : undefined;