-
Notifications
You must be signed in to change notification settings - Fork 1
/
area-model-tree.ts
224 lines (193 loc) · 6.43 KB
/
area-model-tree.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
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
import { TreeRow } from "../../common/tree-row";
import { TreeRowService } from "../../../service/tree-row.service";
import { AbstractAreaModel } from "./abstract-area-model";
import { AreaIdent } from "../area-ident.type";
import { ColumnDefIf } from "../column/column-def.if";
import { FilterFunction } from "../../common/filter-function";
import { SorterService } from "../../../service/sorter.service";
import { SortItem } from "../../common/sort-item";
import {TreeRowIf} from "../../common/tree-row-if";
/**
* Represents a tree-based table model.
* Extends the AbstractAreaModel class.
*
* @template S - The type of the row data.
*/
export class AreaModelTree<S> extends AbstractAreaModel<TreeRow<S>> {
public type = 'AreaModelTree';
protected readonly properties: string[];
protected sorterService: SorterService = new SorterService();
private readonly service = new TreeRowService();
private filteredFlattenRows: TreeRowIf<S>[];
private flattenRows: TreeRowIf<S>[];
private lastPredictFn?: FilterFunction<any>;
constructor(
public override areaIdent: AreaIdent,
public readonly rows: TreeRowIf<S>[],
public override defaultRowHeight: number,
protected override columnDefs: ColumnDefIf[] = []
) {
super(areaIdent, columnDefs, defaultRowHeight);
this.properties = columnDefs.map(def => def.property);
this.flattenRows = this.service.flattenTree(rows);
this.filteredFlattenRows = [...this.flattenRows];
}
override changeColumnOrder(sourceColumnIndex: number, targetColumnIndex: number): void {
// not this! this.arrayMove(this.columnDefs...)
this.arrayMove(this.properties, sourceColumnIndex, targetColumnIndex);
super.changeColumnOrder(sourceColumnIndex, targetColumnIndex);
}
recalcVisibleTreeRows() {
this.filteredFlattenRows = this.service.flattenTree(this.rows);
}
getFilteredFlattenRows(): TreeRowIf<S>[] {
return this.filteredFlattenRows;
}
getRowCount(): number {
let n = 0;
for (const r of this.filteredFlattenRows) {
if (this.service.isVisible(r)) {
n++;
}
}
return n;
}
getValueAt(rowIndex: number, columnIndex: number): any {
const row = this.getRowByIndex(rowIndex);
if (row) {
const prop = this.properties[columnIndex];
if (prop) {
if (prop.includes(".")) {
return this.getPropertyValue(row.data, prop.split("."));
}
// @ts-ignore
return row.data[prop];
}
}
return "";
}
getRowHeight(_rowIndex: number): number {
return this.defaultRowHeight;
}
override getCustomClassesAt(rowIndex: number, _columnIndex: number): string[] {
const row = this.filteredFlattenRows[rowIndex];
return ["ge-table-tree-cell", "ge-table-tree-deep-" + row.deep];
}
override getRowByIndex(idx: number): TreeRowIf<S> | undefined {
return this.filteredFlattenRows[idx];
}
getValueByT(t: S, property: string) {
if (property.includes(".")) {
return this.getPropertyValue(t, property.split("."));
}
// @ts-ignore
return t[property];
}
externalFilterChanged<S>(predictFn: FilterFunction<S>): void {
this.lastPredictFn = predictFn;
this.doFiltering();
}
override doSort(sortItems: SortItem[]): boolean {
const { columnIndex, sortState } = sortItems[sortItems.length - 1];
const f = sortState === "asc" ? 1 :
sortState === "desc" ? -1 : 0;
const property = this.properties[columnIndex];
this.treeSort(this.rows, property, f);
this.flattenRows = this.service.flattenTree(this.rows);
this.filteredFlattenRows = [...this.flattenRows];
return true;
}
toggleExpandCollapseAll(expanded: boolean) {
this.expandAllRecursive(this.rows, expanded);
this.flattenRows = this.service.flattenTree(this.rows);
this.doFiltering();
}
setAllParentsOk(item: TreeRowIf<any>) {
if (item.parent) {
item.parent.keep = true;
this.setAllParentsOk(item.parent);
}
return false;
}
override setValue(rowIndex: number, columnIndex: number, value: any): boolean {
const editInputPipe = this.columnDefs[columnIndex]?.editInputPipe;
if (editInputPipe) {
value = editInputPipe(value, rowIndex, columnIndex);
}
const treeRow: TreeRowIf<S> | undefined = this.getRowByIndex(rowIndex);
if (treeRow) {
const row: S = treeRow.data;
const property = this.columnDefs[columnIndex].property;
if (property.includes(".")) {
return this.setPropertyValue(row, property.split("."), value);
}
// @ts-ignore
row[property] = value;
return true;
}
return false;
}
protected genericTreeTableSortComparator(property: string, f: number) {
return (a: TreeRowIf<S>, b: TreeRowIf<S>) => {
const va = this.getValueByT(a.data, property);
const vb = this.getValueByT(b.data, property);
return this.sorterService.genericSortComparator(va, vb, f);
};
}
private expandAllRecursive(arr: TreeRowIf<S>[], expanded: boolean) {
for (const row of arr) {
row.expanded = expanded;
if (row.children) {
this.expandAllRecursive(row.children, expanded);
}
}
}
private doFiltering() {
if (!this.lastPredictFn) {
this.filteredFlattenRows = [...(this.flattenRows ? this.flattenRows : [])];
} else {
if (!this.flattenRows) {
this.flattenRows = [];
}
let maxDeep = 0;
this.flattenRows.forEach(v => {
maxDeep = Math.max(maxDeep, v.deep);
});
const okRows = this.flattenRows.filter(
(value, index, array) => {
// @ts-ignore
return this.lastPredictFn(value, index, array);
}
);
this.flattenRows.forEach(v => v.keep = false);
this.flattenRows.forEach(v => {
if (okRows.includes(v)) {
v.keep = true;
this.setAllParentsOk(v);
}
});
this.filteredFlattenRows = this.flattenRows.filter(
(value, _index, _array) => {
return value.keep;
}
);
}
}
private getPropertyValue(o: any, props: string[]): any {
const prop = props.shift();
// @ts-ignore
let o2 = o[prop];
if (o2 && props.length) {
return this.getPropertyValue(o2, props);
}
return o2;
}
private treeSort(rows: TreeRowIf<S>[], property: string, f: number) {
rows.sort(this.genericTreeTableSortComparator(property, f));
for (const row of rows) {
if (row.children) {
this.treeSort(row.children, property, f);
}
}
}
}