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