-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtree-view.component.ts
79 lines (79 loc) · 2.18 KB
/
tree-view.component.ts
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
import {
Component,
Input,
OnChanges,
OnInit,
SimpleChange,
} from "@angular/core";
import { FlatTreeControl } from "@angular/cdk/tree";
import {
MatTreeFlatDataSource,
MatTreeFlattener,
} from "@angular/material/tree";
import {
FlatNode,
TreeBaseComponent,
TreeNode,
} from "shared/modules/scientific-metadata-tree/base-classes/tree-base";
import { DatePipe } from "@angular/common";
@Component({
selector: "tree-view",
templateUrl: "./tree-view.component.html",
styleUrls: ["./tree-view.component.scss"],
})
export class TreeViewComponent
extends TreeBaseComponent
implements OnInit, OnChanges
{
@Input() metadata: any;
constructor(datePipe: DatePipe) {
super();
this.datePipe = datePipe;
this.treeFlattener = new MatTreeFlattener(
this.transformer,
this.getLevel,
this.isExpandable,
this.getChildren,
);
this.treeControl = new FlatTreeControl<FlatNode>(
this.getLevel,
this.isExpandable,
);
this.dataSource = new MatTreeFlatDataSource(
this.treeControl,
this.treeFlattener,
);
this.nestNodeMap = new Map<TreeNode, FlatNode>();
this.flatNodeMap = new Map<FlatNode, TreeNode>();
}
ngOnInit() {
this.dataTree = this.buildDataTree(this.metadata, 0);
this.dataSource.data = this.dataTree;
}
ngOnChanges(changes: { [propKey: string]: SimpleChange }) {
for (const propName in changes) {
if (propName === "metadata") {
this.metadata = changes[propName].currentValue;
this.dataTree = this.buildDataTree(this.metadata, 0);
this.dataSource.data = this.dataTree;
this.filterText = this._filterText;
}
}
}
transformer = (node: TreeNode, level: number) => {
const existingNode = this.nestNodeMap.get(node);
const flatNode =
existingNode && existingNode.key === node.key
? existingNode
: new FlatNode();
flatNode.key = node.key;
flatNode.level = level;
flatNode.value = node.value;
flatNode.unit = node.unit;
flatNode.expandable = node.children?.length > 0;
flatNode.visible = true;
this.flatNodeMap.set(flatNode, node);
this.nestNodeMap.set(node, flatNode);
return flatNode;
};
}