Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/webroot/rsrc/js/application/diff/DiffTreeView.js
12242 views
1
/**
2
* @provides phabricator-diff-tree-view
3
* @requires javelin-dom
4
* @javelin
5
*/
6
7
JX.install('DiffTreeView', {
8
9
construct: function() {
10
this._keys = [];
11
this._tree = this._newTreeNode(null, [], 0);
12
this._nodes = {};
13
this._paths = [];
14
},
15
16
members: {
17
_node: null,
18
_keys: null,
19
_tree: null,
20
_nodes: null,
21
_dirty: false,
22
_paths: null,
23
_selectedPath: null,
24
_focusedPath: null,
25
26
getNode: function() {
27
if (!this._node) {
28
var attrs = {
29
className: 'diff-tree-view'
30
};
31
32
this._node = JX.$N('ul', attrs);
33
}
34
35
if (this._dirty) {
36
this.redraw();
37
}
38
39
return this._node;
40
},
41
42
addPath: function(path) {
43
this._paths.push(path);
44
45
var tree = this._getTree(this._tree, path.getPath(), 0);
46
tree.pathObject = path;
47
48
this._dirty = true;
49
50
return this;
51
},
52
53
getPaths: function() {
54
return this._paths;
55
},
56
57
setSelectedPath: function(path) {
58
if (this._selectedPath) {
59
this._selectedPath.setIsSelected(false);
60
this._selectedPath = null;
61
}
62
63
if (path) {
64
path.setIsSelected(true);
65
}
66
67
this._selectedPath = path;
68
69
return this;
70
},
71
72
setFocusedPath: function(path) {
73
if (this._focusedPath) {
74
this._focusedPath.setIsFocused(false);
75
this._focusedPath = null;
76
}
77
78
if (path) {
79
path.setIsFocused(true);
80
}
81
82
this._focusedPath = path;
83
84
return this;
85
},
86
87
redraw: function() {
88
if (!this._dirty) {
89
return;
90
}
91
this._dirty = false;
92
93
var ii;
94
95
// For nodes which don't have a path object yet, build one.
96
var tree;
97
var path;
98
var trees = [];
99
for (ii = 0; ii < this._keys.length; ii++) {
100
var key = this._keys[ii];
101
tree = this._nodes[key];
102
path = tree.pathObject;
103
104
if (!path) {
105
path = new JX.DiffPathView()
106
.setPath(tree.parts);
107
108
path.getIcon()
109
.setIcon('fa-folder-open-o')
110
.setColor('grey');
111
112
tree.pathObject = path;
113
}
114
115
trees.push(tree);
116
}
117
118
for (ii = 0; ii < trees.length; ii++) {
119
tree = trees[ii];
120
tree.displayRoot = null;
121
tree.displayPath = null;
122
tree.displayHide = false;
123
}
124
125
var child;
126
for (ii = 0; ii < trees.length; ii++) {
127
tree = trees[ii];
128
129
if (tree.childCount !== 1) {
130
continue;
131
}
132
133
for (var k in tree.children) {
134
if (tree.children.hasOwnProperty(k)) {
135
child = tree.children[k];
136
break;
137
}
138
}
139
140
if (child.pathObject.getChangeset()) {
141
continue;
142
}
143
144
child.displayRoot = tree.displayRoot || tree;
145
}
146
147
for (ii = 0; ii < trees.length; ii++) {
148
tree = trees[ii];
149
150
if (!tree.displayRoot) {
151
continue;
152
}
153
154
if (!tree.displayRoot.displayPath) {
155
tree.displayRoot.displayPath = [
156
tree.displayRoot.parts[tree.displayRoot.parts.length - 1]
157
];
158
}
159
160
tree.displayRoot.displayPath.push(tree.parts[tree.parts.length - 1]);
161
tree.displayHide = true;
162
}
163
164
for (ii = 0; ii < trees.length; ii++) {
165
tree = trees[ii];
166
path = tree.pathObject;
167
168
path.setHidden(!!tree.displayHide);
169
170
if (tree.displayPath) {
171
path.setDisplayPath(tree.displayPath.join('/'));
172
} else {
173
path.setDisplayPath(null);
174
}
175
}
176
177
for (ii = 0; ii < trees.length; ii++) {
178
tree = trees[ii];
179
180
if (!tree.parent) {
181
tree.depth = 0;
182
} else {
183
// If this node was collapsed into the parent node, don't increase
184
// the tree depth.
185
if (tree.displayHide) {
186
tree.depth = tree.parent.depth;
187
} else {
188
tree.depth = tree.parent.depth + 1;
189
}
190
}
191
192
path = tree.pathObject;
193
194
if (tree.childCount > 0) {
195
path.setIsDirectory(true);
196
}
197
198
path.setDepth((tree.depth - 1));
199
}
200
201
var nodes = [];
202
for (ii = 0; ii < trees.length; ii++) {
203
tree = trees[ii];
204
nodes.push(tree.pathObject.getNode());
205
}
206
207
JX.DOM.setContent(this.getNode(), nodes);
208
},
209
210
_getTree: function(root, path, ii) {
211
if (ii >= path.length) {
212
return root;
213
}
214
215
var part = path[ii];
216
217
if (!root.children.hasOwnProperty(part)) {
218
root.children[part] = this._newTreeNode(root, path, ii);
219
root.childCount++;
220
}
221
222
return this._getTree(root.children[part], path, ii + 1);
223
},
224
225
_newTreeNode: function(parent, path, ii) {
226
var key;
227
var parts;
228
if (path.length) {
229
parts = path.slice(0, ii + 1);
230
key = parts.join('/');
231
this._keys.push(key);
232
} else {
233
parts = [];
234
key = null;
235
}
236
237
var node = {
238
parent: parent,
239
nodeKey: key,
240
parts: parts,
241
children: {},
242
pathObject: null,
243
childCount: 0,
244
depth: 0
245
};
246
247
if (key !== null) {
248
this._nodes[key] = node;
249
}
250
251
return node;
252
}
253
254
}
255
256
});
257
258