forked from CCDirectLink/crosscode-map-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualMapNode.model.ts
More file actions
87 lines (70 loc) · 1.97 KB
/
virtualMapNode.model.ts
File metadata and controls
87 lines (70 loc) · 1.97 KB
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
import { MapNode } from './mapNode.model';
/**
* A class that only returns the children that are included in the filter.
*/
export class VirtualMapNode {
private original: MapNode;
private knownChildren = new WeakMap<MapNode, VirtualMapNode>();
public constructor(original: MapNode) {
this.original = original;
if (original.children) {
for (const node of original.children) {
this.knownChildren.set(node, new VirtualMapNode(node));
}
}
}
public get names(): string[] {
if (!this.containsSingleDirectory) {
return [this.original.name];
}
return [this.original.name].concat(...this.realChildren![0].names);
}
public get path(): string | undefined {
return this.original.path;
}
public get children(): VirtualMapNode[] | undefined {
if (this.containsSingleDirectory) {
return this.realChildren![0].children;
}
return this.realChildren;
}
private resolve(node: MapNode): VirtualMapNode {
const known = this.knownChildren.get(node);
if (known) {
return known;
}
const result = new VirtualMapNode(node);
this.knownChildren.set(node, result);
return result;
}
private get isDirectory(): boolean {
return this.children !== undefined;
}
private get isRoot(): boolean {
return this.original.name === '';
}
private get containsSingleDirectory(): boolean {
const realChildren = this.realChildren;
return realChildren !== undefined
&& realChildren.length === 1
&& realChildren[0].isDirectory
&& !this.isRoot;
}
private get realChildren(): VirtualMapNode[] | undefined {
if (!this.original.children) {
return undefined;
}
return this.original.children
.filter(n => n.displayed)
.sort((a, b) => this.sort(a, b))
.map(n => this.resolve(n));
}
private sort(a: MapNode, b: MapNode): number {
const aIsDir = a.children !== undefined;
const bIsDir = b.children !== undefined;
if (aIsDir !== bIsDir) {
return aIsDir ? -1 : 1;
}
return a.name.localeCompare(b.name, undefined, {numeric: true});
}
}