-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
basicselectionmodel.ts
341 lines (297 loc) · 9.45 KB
/
basicselectionmodel.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { DataModel } from './datamodel';
import { SelectionModel } from './selectionmodel';
/**
* A basic selection model implementation.
*
* #### Notes
* This selection model is sufficient for most use cases where
* structural knowledge of the data source is *not* required.
*/
export class BasicSelectionModel extends SelectionModel {
/**
* Whether the selection model is empty.
*/
get isEmpty(): boolean {
return this._selections.length === 0;
}
/**
* The row index of the cursor.
*/
get cursorRow(): number {
return this._cursorRow;
}
/**
* The column index of the cursor.
*/
get cursorColumn(): number {
return this._cursorColumn;
}
/**
* Move cursor down/up/left/right while making sure it remains
* within the bounds of selected rectangles
*
* @param direction - The direction of the movement.
*/
moveCursorWithinSelections(
direction: SelectionModel.CursorMoveDirection
): void {
// Bail early if there are no selections or no existing cursor
if (this.isEmpty || this.cursorRow === -1 || this._cursorColumn === -1) {
return;
}
// Bail early if only single cell is selected
const firstSelection = this._selections[0];
if (
this._selections.length === 1 &&
firstSelection.r1 === firstSelection.r2 &&
firstSelection.c1 === firstSelection.c2
) {
return;
}
// start from last selection rectangle
if (this._cursorRectIndex === -1) {
this._cursorRectIndex = this._selections.length - 1;
}
let cursorRect = this._selections[this._cursorRectIndex];
const dr = direction === 'down' ? 1 : direction === 'up' ? -1 : 0;
const dc = direction === 'right' ? 1 : direction === 'left' ? -1 : 0;
let newRow = this._cursorRow + dr;
let newColumn = this._cursorColumn + dc;
const r1 = Math.min(cursorRect.r1, cursorRect.r2);
const r2 = Math.max(cursorRect.r1, cursorRect.r2);
const c1 = Math.min(cursorRect.c1, cursorRect.c2);
const c2 = Math.max(cursorRect.c1, cursorRect.c2);
const moveToNextRect = () => {
this._cursorRectIndex =
(this._cursorRectIndex + 1) % this._selections.length;
cursorRect = this._selections[this._cursorRectIndex];
newRow = Math.min(cursorRect.r1, cursorRect.r2);
newColumn = Math.min(cursorRect.c1, cursorRect.c2);
};
const moveToPreviousRect = () => {
this._cursorRectIndex =
this._cursorRectIndex === 0
? this._selections.length - 1
: this._cursorRectIndex - 1;
cursorRect = this._selections[this._cursorRectIndex];
newRow = Math.max(cursorRect.r1, cursorRect.r2);
newColumn = Math.max(cursorRect.c1, cursorRect.c2);
};
if (newRow > r2) {
newRow = r1;
newColumn += 1;
if (newColumn > c2) {
moveToNextRect();
}
} else if (newRow < r1) {
newRow = r2;
newColumn -= 1;
if (newColumn < c1) {
moveToPreviousRect();
}
} else if (newColumn > c2) {
newColumn = c1;
newRow += 1;
if (newRow > r2) {
moveToNextRect();
}
} else if (newColumn < c1) {
newColumn = c2;
newRow -= 1;
if (newRow < r1) {
moveToPreviousRect();
}
}
this._cursorRow = newRow;
this._cursorColumn = newColumn;
// Emit the changed signal.
this.emitChanged();
}
/**
* Get the current selection in the selection model.
*
* @returns The current selection or `null`.
*
* #### Notes
* This is the selection which holds the cursor.
*/
currentSelection(): SelectionModel.Selection | null {
return this._selections[this._selections.length - 1] || null;
}
/**
* Get an iterator of the selections in the model.
*
* @returns A new iterator of the current selections.
*
* #### Notes
* The data grid will render the selections in order.
*/
*selections(): IterableIterator<SelectionModel.Selection> {
yield* this._selections;
}
/**
* Select the specified cells.
*
* @param args - The arguments for the selection.
*/
select(args: SelectionModel.SelectArgs): void {
// Fetch the current row and column counts;
let rowCount = this.dataModel.rowCount('body');
let columnCount = this.dataModel.columnCount('body');
// Bail early if there is no content.
if (rowCount <= 0 || columnCount <= 0) {
return;
}
// Unpack the arguments.
let { r1, c1, r2, c2, cursorRow, cursorColumn, clear } = args;
// Clear the necessary selections.
if (clear === 'all') {
this._selections.length = 0;
} else if (clear === 'current') {
this._selections.pop();
}
// Clamp to the data model bounds.
r1 = Math.max(0, Math.min(r1, rowCount - 1));
r2 = Math.max(0, Math.min(r2, rowCount - 1));
c1 = Math.max(0, Math.min(c1, columnCount - 1));
c2 = Math.max(0, Math.min(c2, columnCount - 1));
// Indicate if a row/column has already been selected.
let alreadySelected = false;
// Handle the selection mode.
if (this.selectionMode === 'row') {
c1 = 0;
c2 = columnCount - 1;
alreadySelected =
this._selections.filter(selection => selection.r1 === r1).length !== 0;
// Remove from selections if already selected.
this._selections = alreadySelected
? this._selections.filter(selection => selection.r1 !== r1)
: this._selections;
} else if (this.selectionMode === 'column') {
r1 = 0;
r2 = rowCount - 1;
alreadySelected =
this._selections.filter(selection => selection.c1 === c1).length !== 0;
// Remove from selections if already selected.
this._selections = alreadySelected
? this._selections.filter(selection => selection.c1 !== c1)
: this._selections;
}
// Alias the cursor row and column.
let cr = cursorRow;
let cc = cursorColumn;
// Compute the new cursor location.
if (cr < 0 || (cr < r1 && cr < r2) || (cr > r1 && cr > r2)) {
cr = r1;
}
if (cc < 0 || (cc < c1 && cc < c2) || (cc > c1 && cc > c2)) {
cc = c1;
}
// Update the cursor.
this._cursorRow = cr;
this._cursorColumn = cc;
this._cursorRectIndex = this._selections.length;
// Add the new selection if it wasn't already selected.
if (!alreadySelected) {
this._selections.push({ r1, c1, r2, c2 });
}
// Emit the changed signal.
this.emitChanged();
}
/**
* Clear all selections in the selection model.
*/
clear(): void {
// Bail early if there are no selections.
if (this._selections.length === 0) {
return;
}
// Reset the internal state.
this._cursorRow = -1;
this._cursorColumn = -1;
this._cursorRectIndex = -1;
this._selections.length = 0;
// Emit the changed signal.
this.emitChanged();
}
/**
* A signal handler for the data model `changed` signal.
*
* @param args - The arguments for the signal.
*/
protected onDataModelChanged(
sender: DataModel,
args: DataModel.ChangedArgs
): void {
// Bail early if the model has no current selections.
if (this._selections.length === 0) {
return;
}
// Bail early if the cells have changed in place.
if (args.type === 'cells-changed') {
return;
}
// Bail early if there is no change to the row or column count.
if (args.type === 'rows-moved' || args.type === 'columns-moved') {
return;
}
// Fetch the last row and column index.
let lr = sender.rowCount('body') - 1;
let lc = sender.columnCount('body') - 1;
// Bail early if the data model is empty.
if (lr < 0 || lc < 0) {
this._selections.length = 0;
this.emitChanged();
return;
}
// Fetch the selection mode.
let mode = this.selectionMode;
// Set up the assignment index variable.
let j = 0;
// Iterate over the current selections.
for (let i = 0, n = this._selections.length; i < n; ++i) {
// Unpack the selection.
let { r1, c1, r2, c2 } = this._selections[i];
// Skip the selection if it will disappear.
if ((lr < r1 && lr < r2) || (lc < c1 && lc < c2)) {
continue;
}
// Modify the bounds based on the selection mode.
if (mode === 'row') {
r1 = Math.max(0, Math.min(r1, lr));
r2 = Math.max(0, Math.min(r2, lr));
c1 = 0;
c2 = lc;
} else if (mode === 'column') {
r1 = 0;
r2 = lr;
c1 = Math.max(0, Math.min(c1, lc));
c2 = Math.max(0, Math.min(c2, lc));
} else {
r1 = Math.max(0, Math.min(r1, lr));
r2 = Math.max(0, Math.min(r2, lr));
c1 = Math.max(0, Math.min(c1, lc));
c2 = Math.max(0, Math.min(c2, lc));
}
// Assign the modified selection to the array.
this._selections[j++] = { r1, c1, r2, c2 };
}
// Remove the stale selections.
this._selections.length = j;
// Emit the changed signal.
this.emitChanged();
}
private _cursorRow = -1;
private _cursorColumn = -1;
private _cursorRectIndex = -1;
private _selections: SelectionModel.Selection[] = [];
}