This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 293
/
AjxpNode.es6
389 lines (377 loc) · 10.9 KB
/
AjxpNode.es6
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
class AjxpNode extends Observable{
/**
*
* @param path String
* @param isLeaf Boolean
* @param label String
* @param icon String
* @param iNodeProvider IAjxpNodeProvider
*/
constructor(path, isLeaf=false, label='', icon='', iNodeProvider=null){
super();
this._path = path;
if(this._path && this._path.length && this._path.length > 1){
if(this._path[this._path.length-1] == "/"){
this._path = this._path.substring(0, this._path.length-1);
}
}
this._isLeaf = isLeaf;
this._label = label;
this._icon = icon;
this._isRoot = false;
this._metadata = new Map();
this._children = new Map();
this._isLoaded = false;
this.fake = false;
this._iNodeProvider = iNodeProvider;
}
/**
* The node is loaded or not
* @returns Boolean
*/
isLoaded(){
return this._isLoaded;
}
/**
* Changes loaded status
* @param bool Boolean
*/
setLoaded(bool){
this._isLoaded = bool;
}
/**
* Loads the node using its own provider or the one passed
* @param iAjxpNodeProvider IAjxpNodeProvider Optionnal
* @param additionalParameters Object of optional parameters
*/
load(iAjxpNodeProvider, additionalParameters=null){
if(this._isLoading) return;
if(!iAjxpNodeProvider){
if(this._iNodeProvider){
iAjxpNodeProvider = this._iNodeProvider;
}else{
iAjxpNodeProvider = new RemoteNodeProvider();
}
}
this._isLoading = true;
this.notify("loading");
if(this._isLoaded){
this._isLoading = false;
this.notify("loaded");
return;
}
iAjxpNodeProvider.loadNode(this, function(node){
this._isLoaded = true;
this._isLoading = false;
this.notify("loaded");
this.notify("first_load");
}.bind(this), null, false, -1, additionalParameters);
}
/**
* Remove children and reload node
* @param iAjxpNodeProvider IAjxpNodeProvider Optionnal
*/
reload(iAjxpNodeProvider){
this._children.forEach(function(child,key){
child.notify("node_removed");
child._parentNode = null;
this._children.delete(key);
this.notify("child_removed", child);
}, this);
this._isLoaded = false;
this.load(iAjxpNodeProvider);
}
/**
* Unload child and notify "force_clear"
*/
clear(){
this._children.forEach(function(child,key){
child.notify("node_removed");
child._parentNode = null;
this._children.delete(key);
this.notify("child_removed", child);
}, this);
this._isLoaded = false;
this.notify("force_clear");
}
/**
* Sets this AjxpNode as being the root parent
*/
setRoot(){
this._isRoot = true;
}
/**
* Set the node children as a bunch
* @param ajxpNodes AjxpNodes[]
*/
setChildren(ajxpNodes){
this._children = new Map();
ajxpNodes.forEach(function(value){
this._children.set(value.getPath(), value);
value.setParent(this);
}.bind(this));
}
/**
* Get all children as a bunch
* @returns AjxpNode[]
*/
getChildren(){
return this._children;
}
getFirstChildIfExists(){
if(this._children.size){
return this._children.values().next().value;
}
return null;
}
/**
* Adds a child to children
* @param ajxpNode AjxpNode The child
*/
addChild(ajxpNode){
ajxpNode.setParent(this);
if(this._iNodeProvider) ajxpNode._iNodeProvider = this._iNodeProvider;
var existingNode = this.findChildByPath(ajxpNode.getPath());
if(existingNode && !(existingNode instanceof String)){
existingNode.replaceBy(ajxpNode, "override");
}else{
this._children.set(ajxpNode.getPath(), ajxpNode);
this.notify("child_added", ajxpNode.getPath());
}
}
/**
* Removes the child from the children
* @param ajxpNode AjxpNode
*/
removeChild(ajxpNode){
var removePath = ajxpNode.getPath();
ajxpNode.notify("node_removed");
ajxpNode._parentNode = null;
this._children.delete(ajxpNode.getPath());
this.notify("child_removed", removePath);
}
/**
* Replaces the current node by a new one. Copy all properties deeply
* @param ajxpNode AjxpNode
* @param metaMerge
*/
replaceBy(ajxpNode, metaMerge){
this._isLeaf = ajxpNode._isLeaf;
if(ajxpNode.getPath() && this._path != ajxpNode.getPath()){
var originalPath = this._path;
if(this.getParent()){
var parentChildrenIndex = this.getParent()._children;
parentChildrenIndex.set(ajxpNode.getPath(), this);
parentChildrenIndex.delete(originalPath);
}
this._path = ajxpNode.getPath();
var pathChanged = true;
}
if(ajxpNode._label){
this._label = ajxpNode._label;
}
if(ajxpNode._icon){
this._icon = ajxpNode._icon;
}
if(ajxpNode._iNodeProvider){
this._iNodeProvider = ajxpNode._iNodeProvider;
}
//this._isRoot = ajxpNode._isRoot;
this._isLoaded = ajxpNode._isLoaded;
this.fake = ajxpNode.fake;
var meta = ajxpNode.getMetadata();
if(metaMerge == "override") this._metadata = new Map();
meta.forEach(function(value, key){
if(metaMerge == "override"){
this._metadata.set(key, value);
}else{
if(this._metadata.has(key) && value === ""){
return;
}
this._metadata.set(key, value);
}
}.bind(this) );
if(pathChanged && !this._isLeaf && this.getChildren().size){
window.setTimeout(function(){
this.reload(this._iNodeProvider);
}.bind(this), 100);
return;
}
ajxpNode.getChildren().forEach(function(child){
this.addChild(child);
}.bind(this) );
this.notify("node_replaced", this);
}
/**
* Finds a child node by its path
* @param path String
* @returns AjxpNode
*/
findChildByPath(path){
return this._children.get(path);
}
/**
* Sets the metadata as a bunch
* @param data Map A Map
*/
setMetadata(data){
this._metadata = data;
}
/**
* Gets the metadat
* @returns Map
*/
getMetadata(){
return this._metadata;
}
/**
* Is this node a leaf
* @returns Boolean
*/
isLeaf(){
return this._isLeaf;
}
/**
* @returns String
*/
getPath(){
return this._path;
}
/**
* @returns String
*/
getLabel(){
return this._label;
}
/**
* @returns String
*/
getIcon(){
return this._icon;
}
/**
* @returns Boolean
*/
isRecycle(){
return (this.getAjxpMime() == 'ajxp_recycle');
}
/**
* Search the mime type in the parent branch
* @param ajxpMime String
* @returns Boolean
*/
hasAjxpMimeInBranch(ajxpMime){
if(this.getAjxpMime() == ajxpMime.toLowerCase()) return true;
var parent, crt = this;
while(parent =crt._parentNode){
if(parent.getAjxpMime() == ajxpMime.toLowerCase()){return true;}
crt = parent;
}
return false;
}
/**
* Search the mime type in the parent branch
* @returns Boolean
* @param metadataKey
* @param metadataValue
*/
hasMetadataInBranch(metadataKey, metadataValue){
if(this.getMetadata().has(metadataKey)) {
if(metadataValue) {
return this.getMetadata().get(metadataKey) == metadataValue;
}else {
return true;
}
}
var parent, crt = this;
while(parent =crt._parentNode){
if(parent.getMetadata().has(metadataKey)){
if(metadataValue){
return (parent.getMetadata().get(metadataKey) == metadataValue);
}else{
return true;
}
}
crt = parent;
}
return false;
}
/**
* Sets a reference to the parent node
* @param parentNode AjxpNode
*/
setParent(parentNode){
this._parentNode = parentNode;
}
/**
* Gets the parent Node
* @returns AjxpNode
*/
getParent(){
return this._parentNode;
}
/**
* Finds this node by path if it already exists in arborescence
* @param rootNode AjxpNode
* @param fakeNodes AjxpNode[]
* @returns AjxpNode|undefined
*/
findInArbo(rootNode, fakeNodes){
if(!this.getPath()) return;
var pathParts = this.getPath().split("/");
var crtPath = "";
var crtNode, crtParentNode = rootNode;
for(var i=0;i<pathParts.length;i++){
if(pathParts[i] == "") continue;
crtPath = crtPath + "/" + pathParts[i];
var node = crtParentNode.findChildByPath(crtPath);
if(node && !(node instanceof String)){
crtNode = node;
}else{
if(fakeNodes === undefined) return undefined;
crtNode = new AjxpNode(crtPath, false, PathUtils.getBasename(crtPath));
crtNode.fake = true;
crtNode.getMetadata().set("text", PathUtils.getBasename(crtPath));
fakeNodes.push(crtNode);
crtParentNode.addChild(crtNode);
}
crtParentNode = crtNode;
}
return crtNode;
}
/**
* @returns Boolean
*/
isRoot(){
return this._isRoot;
}
/**
* Check if it's the parent of the given node
* @param node AjxpNode
* @returns Boolean
*/
isParentOf(node){
var childPath = node.getPath();
var parentPath = this.getPath();
return (childPath.substring(0,parentPath.length) == parentPath);
}
/**
* Check if it's a child of the given node
* @param node AjxpNode
* @returns Boolean
*/
isChildOf(node){
var childPath = this.getPath();
var parentPath = node.getPath();
return (childPath.substring(0,parentPath.length) == parentPath);
}
/**
* Gets the current's node mime type, either by ajxp_mime or by extension.
* @returns String
*/
getAjxpMime(){
if(this._metadata && this._metadata.has("ajxp_mime")) return this._metadata.get("ajxp_mime").toLowerCase();
if(this._metadata && this.isLeaf()) return PathUtils.getAjxpMimeType(this._metadata).toLowerCase();
return "";
}
}