-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathslickRowSelectionModel.ts
271 lines (229 loc) · 8.99 KB
/
slickRowSelectionModel.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { type SelectionModel } from '../enums/index.js';
import type { GridOption, OnActiveCellChangedEventArgs, RowSelectionModelOption } from '../interfaces/index.js';
import { SlickCellRangeSelector } from '../extensions/slickCellRangeSelector.js';
import { SlickEvent, SlickEventData, SlickEventHandler, type SlickGrid, SlickRange } from '../core/index.js';
export class SlickRowSelectionModel implements SelectionModel {
pluginName: 'RowSelectionModel' = 'RowSelectionModel' as const;
/** triggered when selected ranges changes */
onSelectedRangesChanged: SlickEvent<SlickRange[]>;
protected _options: RowSelectionModelOption;
protected _eventHandler: SlickEventHandler;
protected _inHandler = false;
protected _grid!: SlickGrid;
protected _ranges: SlickRange[] = [];
protected _selector?: SlickCellRangeSelector;
protected _defaults = {
autoScrollWhenDrag: true,
cellRangeSelector: undefined,
dragToSelect: false,
selectActiveRow: true,
} as RowSelectionModelOption;
constructor(options?: RowSelectionModelOption) {
this.onSelectedRangesChanged = new SlickEvent<SlickRange[]>('onSelectedRangesChanged');
this._eventHandler = new SlickEventHandler();
this._options = { ...this._defaults, ...options };
}
get addonOptions(): RowSelectionModelOption {
return this._options;
}
get eventHandler(): SlickEventHandler {
return this._eventHandler;
}
get gridOptions(): GridOption {
return this._grid?.getOptions();
}
init(grid: SlickGrid): void {
this._grid = grid;
this._options = { ...this._defaults, ...this._options };
this._selector = this.addonOptions.cellRangeSelector;
// add PubSub instance to all SlickEvent
const pubSub = grid.getPubSubService();
if (pubSub) {
this.onSelectedRangesChanged.setPubSubService(pubSub);
}
if (!this._selector && this._options.dragToSelect) {
this._selector = new SlickCellRangeSelector({
selectionCss: { border: 'none' } as CSSStyleDeclaration,
autoScroll: this._options.autoScrollWhenDrag,
});
this.addonOptions.cellRangeSelector = this._selector;
}
this._eventHandler
.subscribe(this._grid.onActiveCellChanged, this.handleActiveCellChange.bind(this))
.subscribe(this._grid.onClick, this.handleClick.bind(this))
.subscribe(this._grid.onKeyDown, this.handleKeyDown.bind(this));
if (this._selector) {
this._grid.registerPlugin(this._selector);
this._selector.onCellRangeSelecting.subscribe(this.handleCellRangeSelected.bind(this));
this._selector.onCellRangeSelected.subscribe(this.handleCellRangeSelected.bind(this));
this._selector.onBeforeCellRangeSelected.subscribe(this.handleBeforeCellRangeSelected.bind(this));
}
}
destroy(): void {
this.dispose();
}
dispose(): void {
this._eventHandler.unsubscribeAll();
this.disposeSelector();
}
disposeSelector(): void {
if (this._selector) {
this._selector.onCellRangeSelecting.unsubscribe(this.handleCellRangeSelected.bind(this));
this._selector.onCellRangeSelected.unsubscribe(this.handleCellRangeSelected.bind(this));
this._selector.onBeforeCellRangeSelected.unsubscribe(this.handleBeforeCellRangeSelected.bind(this));
this._grid.unregisterPlugin(this._selector);
this._selector?.destroy();
this._selector?.dispose();
}
}
getCellRangeSelector(): SlickCellRangeSelector | undefined {
return this._selector;
}
getSelectedRanges(): SlickRange[] {
return this._ranges;
}
getSelectedRows(): number[] {
return this.rangesToRows(this._ranges);
}
refreshSelections(): void {
this.setSelectedRows(this.getSelectedRows());
}
setSelectedRows(rows: number[]): void {
this.setSelectedRanges(this.rowsToRanges(rows), 'SlickRowSelectionModel.setSelectedRows');
}
setSelectedRanges(ranges: SlickRange[], caller = 'SlickRowSelectionModel.setSelectedRanges'): void {
// simple check for: empty selection didn't change, prevent firing onSelectedRangesChanged
if ((!this._ranges || this._ranges.length === 0) && (!ranges || ranges.length === 0)) {
return;
}
this._ranges = ranges;
// provide extra "caller" argument through SlickEventData event to avoid breaking the previous pubsub event structure
// that only accepts an array of selected range `SlickRange[]`, the SlickEventData args will be merged and used later by `onSelectedRowsChanged`
const eventData = new SlickEventData(new CustomEvent('click', { detail: { caller } }), this._ranges);
this.onSelectedRangesChanged.notify(this._ranges, eventData);
}
//
// protected functions
// ---------------------
protected getRowsRange(from: number, to: number): number[] {
let i;
const rows = [];
for (i = from; i <= to; i++) {
rows.push(i);
}
for (i = to; i < from; i++) {
rows.push(i);
}
return rows;
}
protected handleBeforeCellRangeSelected(e: SlickEventData, cell: { row: number; cell: number }): boolean | void {
let isRowMoveColumn = false;
if (this.gridOptions.enableRowMoveManager) {
isRowMoveColumn = this.isHandlerColumn(cell.cell) ?? false;
}
if (this._grid.getEditorLock().isActive() || isRowMoveColumn) {
e.stopPropagation();
return false;
}
this._grid.setActiveCell(cell.row, cell.cell);
}
protected handleCellRangeSelected(_e: SlickEventData, args: { range: SlickRange }): boolean | void {
if (!this.gridOptions.multiSelect || !this.addonOptions.selectActiveRow) {
return false;
}
this.setSelectedRanges([new SlickRange(args.range.fromRow, 0, args.range.toRow, this._grid.getColumns().length - 1)]);
}
protected handleActiveCellChange(_e: SlickEventData, args: OnActiveCellChangedEventArgs): void {
if (this._options.selectActiveRow && args.row !== null) {
this.setSelectedRanges([new SlickRange(args.row, 0, args.row, this._grid.getColumns().length - 1)]);
}
}
protected handleClick(e: SlickEventData): boolean | void {
const cell = this._grid.getCellFromEvent(e);
if (!cell || !this._grid.canCellBeActive(cell.row, cell.cell)) {
return false;
}
if (!this.gridOptions.multiSelect || (!e.ctrlKey && !e.shiftKey && !e.metaKey)) {
return false;
}
let selection = this.rangesToRows(this._ranges);
const idx = selection.indexOf(cell.row);
if (idx === -1 && (e.ctrlKey || e.metaKey)) {
selection.push(cell.row);
this._grid.setActiveCell(cell.row, cell.cell);
} else if (idx !== -1 && (e.ctrlKey || e.metaKey)) {
selection = selection.filter((o: number) => o !== cell.row);
this._grid.setActiveCell(cell.row, cell.cell);
} else if (selection.length && e.shiftKey) {
const last = selection.pop();
const from = Math.min(cell.row, last as number);
const to = Math.max(cell.row, last as number);
selection = [];
for (let i = from; i <= to; i++) {
if (i !== last) {
selection.push(i);
}
}
selection.push(last as number);
this._grid.setActiveCell(cell.row, cell.cell);
}
const tempRanges = this.rowsToRanges(selection);
this.setSelectedRanges(tempRanges);
e.stopImmediatePropagation();
return true;
}
protected handleKeyDown(e: SlickEventData): void {
const activeRow = this._grid.getActiveCell();
if (
this.gridOptions.multiSelect &&
activeRow &&
e.shiftKey &&
!e.ctrlKey &&
!e.altKey &&
!e.metaKey &&
(e.key === 'ArrowUp' || e.key === 'ArrowDown')
) {
let selectedRows = this.getSelectedRows();
selectedRows.sort((x: number, y: number) => x - y);
if (!selectedRows.length) {
selectedRows = [activeRow.row];
}
let active: number;
let top = selectedRows[0];
let bottom = selectedRows[selectedRows.length - 1];
if (e.key === 'ArrowDown') {
active = activeRow.row < bottom || top === bottom ? ++bottom : ++top;
} else {
active = activeRow.row < bottom ? --bottom : --top;
}
if (active >= 0 && active < this._grid.getDataLength()) {
this._grid.scrollRowIntoView(active);
const tempRanges = this.rowsToRanges(this.getRowsRange(top, bottom));
this.setSelectedRanges(tempRanges);
}
e.preventDefault();
e.stopPropagation();
}
}
/** is the column a column Row Move OR Select Row Move */
isHandlerColumn(columnIndex: number): boolean {
const columns = this._grid.getColumns();
const col = columns[columnIndex].behavior || '';
return /move|selectAndMove/.test(col);
}
protected rangesToRows(ranges: SlickRange[]): number[] {
const rows = [];
for (let i = 0; i < ranges.length; i++) {
for (let j = ranges[i].fromRow; j <= ranges[i].toRow; j++) {
rows.push(j);
}
}
return rows;
}
protected rowsToRanges(rows: number[]): SlickRange[] {
const ranges: SlickRange[] = [];
const lastCell = this._grid.getColumns().length - 1;
rows.forEach((row) => ranges.push(new SlickRange(row, 0, row, lastCell)));
return ranges;
}
}