-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset-node.component.ts
More file actions
134 lines (118 loc) · 4.48 KB
/
dataset-node.component.ts
File metadata and controls
134 lines (118 loc) · 4.48 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
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
import { AfterContentChecked, ChangeDetectorRef, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Router } from '@angular/router';
import { Observable, Subject, Subscription, take } from 'rxjs';
import { DatasetNode } from './dataset-node';
import { Store } from '@ngrx/store';
import { selectExpandedDatasets, setExpandedDatasets } from './dataset-node.state';
import { selectDatasetId } from 'app/datasets/datasets.state';
import { ComponentValidator } from 'app/common/component-validator';
import { cloneDeep } from 'lodash';
@Component({
selector: 'gpf-dataset-node',
templateUrl: './dataset-node.component.html',
styleUrls: ['./dataset-node.component.css'],
standalone: false
})
export class DatasetNodeComponent extends ComponentValidator implements OnInit, AfterContentChecked {
@Input() public datasetNode: DatasetNode;
@Output() public setExpandabilityEvent = new EventEmitter<boolean>();
public selectedDatasetId: string;
public isExpanded = false;
public closeChildrenSubject: Subject<void> = new Subject<void>();
@Input() public closeObservable: Observable<void> = new Observable<void>();
public closeChildrenSubscription: Subscription;
public constructor(
private router: Router,
private changeDetector: ChangeDetectorRef,
protected store: Store
) {
super(store, 'datasetNode', selectExpandedDatasets);
}
public ngOnInit(): void {
this.store.select(selectDatasetId).pipe(take(1)).subscribe(datasetId => {
this.selectedDatasetId = datasetId;
if (this.datasetNode.dataset.id === this.selectedDatasetId) {
this.setExpandability();
}
});
this.store.select(selectExpandedDatasets).pipe(take(1)).subscribe((expandedDatasets: string[]) => {
if (expandedDatasets.includes(this.datasetNode.dataset.id)) {
this.isExpanded = true;
}
});
this.closeChildrenSubscription = this.closeObservable.subscribe(() => {
this.isExpanded = false;
this.removeFromState(this.datasetNode.dataset.id);
this.emitEventToChild();
});
}
public ngAfterContentChecked(): void {
this.changeDetector.detectChanges();
}
public setExpandability(): void {
this.setExpandabilityEvent.emit(true);
}
public emitEventToChild(): void {
this.closeChildrenSubject.next();
}
public select(openInNewTab = false): void {
if (!this.datasetNode?.dataset) {
return;
}
const url = `/datasets/${this.datasetNode.dataset.id}`;
if (openInNewTab) {
const newWindow = window.open('', '_blank');
if (newWindow) {
newWindow.location.assign(url);
}
} else {
if (this.datasetNode.dataset.id === this.selectedDatasetId) {
return;
}
this.router.navigate([url]);
}
}
private addToState(nodeId: string): void {
this.store.select(selectExpandedDatasets).pipe(take(1)).subscribe((expandedDatasetsState: string[]) => {
const expandedDatasets = cloneDeep(expandedDatasetsState);
if (!expandedDatasets.includes(nodeId)) {
expandedDatasets.push(nodeId);
this.store.dispatch(setExpandedDatasets({expandedDatasets: expandedDatasets}));
}
});
}
private removeFromState(nodeId: string): void {
this.store.select(selectExpandedDatasets).pipe(take(1)).subscribe((expandedDatasetsState: string[]) => {
const expandedDatasets = cloneDeep(expandedDatasetsState);
if (expandedDatasets.includes(nodeId)) {
const index = expandedDatasets.indexOf(nodeId, 0);
expandedDatasets.splice(index, 1);
this.store.dispatch(setExpandedDatasets({expandedDatasets: expandedDatasets}));
}
});
}
private updateState(nodeId: string): void {
this.store.select(selectExpandedDatasets).pipe(take(1)).subscribe((expandedDatasetsState: string[]) => {
const expandedDatasets = cloneDeep(expandedDatasetsState);
if (expandedDatasets.includes(nodeId)) {
const index = expandedDatasets.indexOf(nodeId, 0);
expandedDatasets.splice(index, 1);
} else {
expandedDatasets.push(nodeId);
}
this.store.dispatch(setExpandedDatasets({expandedDatasets: expandedDatasets}));
});
}
public setIsExpanded(): void {
this.isExpanded = true;
this.addToState(this.datasetNode.dataset.id);
this.setExpandability();
}
public toggleDatasetCollapse(selectedNodeId: string): void {
this.updateState(selectedNodeId);
this.isExpanded = !this.isExpanded;
if (!this.isExpanded) {
this.emitEventToChild();
}
}
}