-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGroupRowSelectionHelper.tsx
134 lines (118 loc) · 5.48 KB
/
GroupRowSelectionHelper.tsx
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 dxDataGrid from 'devextreme/ui/data_grid';
import { DataGridTypes } from 'devextreme-react/data-grid';
import { LoadOptions } from 'devextreme/data';
import { isItemsArray } from 'devextreme/common/data/custom-store';
import { IGroupRowReadyParameter } from './GroupRowComponent';
export default class GroupSelectionHelper {
groupedColumns: DataGridTypes.Column[];
grid: dxDataGrid;
getSelectedKeysPromise: Promise<any[]> | null;
selectedKeys: any[] = [];
groupChildKeys: Record<string, any> = {};
constructor(grid: dxDataGrid) {
this.grid = grid;
this.groupedColumns = this.collectGroupedColumns(grid);
this.getSelectedKeysPromise = this.getSelectedKeys(grid);
this.getSelectedKeysPromise.then((keys: any[]) => {
this.selectedKeys = keys;
}).catch(() => {});
const defaultSelectionHandler: Function | undefined = grid.option('onSelectionChanged');
grid.option('onSelectionChanged', (e: DataGridTypes.SelectionChangedEvent) => {
this.selectionChanged(e);
if (defaultSelectionHandler) { defaultSelectionHandler(e); }
});
const defaultOptionChangedHandler: Function | undefined = grid.option('onOptionChanged');
grid.option('onOptionChanged', (e: DataGridTypes.OptionChangedEvent) => {
if (e.fullName.includes('groupIndex')) {
this.groupedColumns = this.collectGroupedColumns(grid);
}
if (defaultOptionChangedHandler) { defaultOptionChangedHandler(e); }
});
}
groupRowInit(arg: IGroupRowReadyParameter): Promise<any> {
const checkBoxId = this.calcCheckBoxId(this.grid, arg.key);
const promise = new Promise<any>((resolve, reject) => {
if (!this.groupChildKeys[checkBoxId]) {
const filter: any[] = [];
arg.key.forEach((key, i) => {
filter.push([this.groupedColumns[i].dataField, '=', key]);
});
const loadOptions: LoadOptions = {
filter,
};
const store = this.grid.getDataSource().store();
store.load(loadOptions).then((data) => {
if (isItemsArray(data)) {
this.groupChildKeys[checkBoxId] = data.map((d) => this.grid.keyOf(d));
this.getSelectedKeys(this.grid).then((selectedKeys) => {
const checkedState: boolean | undefined = this.areKeysSelected(this.groupChildKeys[checkBoxId], selectedKeys);
arg.setCheckedState(checkedState);
}).catch(() => {});
resolve(this.groupChildKeys[checkBoxId]);
}
}).catch(() => {});
} else {
this.getSelectedKeys(this.grid).then((selectedKeys) => {
const checkedState: boolean | undefined = this.areKeysSelected(this.groupChildKeys[checkBoxId], selectedKeys);
arg.setCheckedState(checkedState);
resolve(this.groupChildKeys[checkBoxId]);
}).catch(() => {});
}
});
return promise;
}
selectionChanged(e: DataGridTypes.SelectionChangedEvent): void {
const groupRows: DataGridTypes.Row[] = e.component.getVisibleRows().filter((r) => r.rowType === 'group');
this.getSelectedKeysPromise = null;
if (e.component.option('selection.deferred')) {
const selectionFilter = e.component.option('selectionFilter');
if (selectionFilter && selectionFilter.length >= 0) {
this.repaintGroupRowTree(e.component, groupRows);
} else {
e.component.repaintRows(groupRows.map((g) => g.rowIndex));
}
} else if (e.selectedRowKeys.length >= e.component.totalCount() || e.currentDeselectedRowKeys.length >= e.component.totalCount()) {
e.component.repaintRows(groupRows.map((g) => g.rowIndex));
} else {
this.repaintGroupRowTree(e.component, groupRows);
}
}
getSelectedKeys(grid: dxDataGrid): Promise<any[]> {
if (grid.option('selection.deferred')) {
if (!this.getSelectedKeysPromise) {
this.getSelectedKeysPromise = grid.getSelectedRowKeys();
}
return this.getSelectedKeysPromise;
}
return new Promise((resolve) => resolve(grid.getSelectedRowKeys()));
}
repaintGroupRowTree(grid: dxDataGrid, groupRows: DataGridTypes.Row[]): void {
const topGroupRow: DataGridTypes.Row | null = groupRows.filter((r) => r.isExpanded).reduce((acc: DataGridTypes.Row | null, curr) => (!acc || acc.key.length > curr.key.length ? curr : acc), null);
if (topGroupRow) {
const affectedGroupRows = groupRows.filter((g) => g.key[0] == topGroupRow.key[0]);
grid.repaintRows(affectedGroupRows.map((g) => g.rowIndex));
}
}
areKeysSelected(keysToCheck: any[], selectedKeys: any[]): boolean | undefined {
if (selectedKeys.length == 0) { return false; }
const intersectionCount = keysToCheck.filter((k) => selectedKeys.includes(k)).length;
if (intersectionCount === 0) { return false; }
if (intersectionCount === keysToCheck.length) { return true; }
return undefined;
}
getChildRowKeys(grid: dxDataGrid, groupRowKey: string[]): any[] {
return this.groupChildKeys[this.calcCheckBoxId(grid, groupRowKey)];
}
calcCheckBoxId(grid: dxDataGrid, groupRowKey: string[]): string {
const gridId: string = grid.element().id;
return `${gridId}groupCheckBox${groupRowKey.join('')}`;
}
collectGroupedColumns(grid: dxDataGrid): DataGridTypes.Column[] {
const allColumns: DataGridTypes.Column[] = grid.getVisibleColumns();
return allColumns.filter((c: DataGridTypes.Column) => c.groupIndex != undefined && c.groupIndex >= 0)
.sort((a, b) => {
if (!a.groupIndex || !b.groupIndex) return 0;
return a.groupIndex > b.groupIndex ? 1 : -1;
});
}
}