-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathGridSelectionMouseHandler.ts
More file actions
296 lines (265 loc) · 8.46 KB
/
GridSelectionMouseHandler.ts
File metadata and controls
296 lines (265 loc) · 8.46 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
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
import { EventHandlerResult } from '../EventHandlerResult';
import Grid from '../Grid';
import GridMouseHandler, { GridMouseEvent } from '../GridMouseHandler';
import GridRange from '../GridRange';
import GridUtils, { GridPoint } from '../GridUtils';
const DEFAULT_INTERVAL_MS = 100;
class GridSelectionMouseHandler extends GridMouseHandler {
private startPoint?: GridPoint;
private hasExtendedFloating = false;
// Timer used when holding a drag past the end of the grid
private timer?: ReturnType<typeof setTimeout>;
private lastTriggerTime?: number;
onDown(
gridPoint: GridPoint,
grid: Grid,
event: GridMouseEvent
): EventHandlerResult {
const { x, y, column, row } = gridPoint;
const { metrics } = grid;
if (!metrics) throw new Error('metrics not set');
const { gridX, gridY, maxX, maxY } = metrics;
const gridMouseX = x - gridX;
const gridMouseY = y - gridY;
if (
gridMouseX < 0 ||
gridMouseY < 0 ||
(column == null && gridMouseX > maxX) ||
(row == null && gridMouseY > maxY)
) {
return false;
}
const isModifierKey = GridUtils.isModifierKeyDown(event);
const isShiftKey = event.shiftKey;
if (!isModifierKey) {
if (isShiftKey) {
grid.trimSelectedRanges();
} else {
grid.clearSelectedRanges();
}
}
const theme = grid.getTheme();
const { autoSelectRow, autoSelectColumn } = theme;
// If they click a column/row header, don't want to select the whole table if auto select column/row is on
if (
(column !== null || !autoSelectColumn) &&
(row !== null || !autoSelectRow)
) {
grid.focus();
grid.moveCursorToPosition(
column,
row,
isShiftKey,
false,
isShiftKey && isModifierKey
);
}
this.startPoint = gridPoint;
this.hasExtendedFloating = false;
return true;
}
onDrag(gridPoint: GridPoint, grid: Grid): EventHandlerResult {
if (this.startPoint === undefined) {
return false;
}
this.stopTimer();
const { row: startRow, column: startColumn } = this.startPoint;
const { x, y } = gridPoint;
let { row, column } = gridPoint;
const { metrics } = grid;
if (!metrics) throw new Error('metrics not set');
const { left, lastLeft, top, lastTop, columnWidth, rowHeight } = metrics;
const dragBounds = GridUtils.getScrollDragBounds(
metrics,
startRow,
startColumn
);
// If we're dragging outside of the grid entirely, then we should start scrolling
let deltaX = 0;
let deltaY = 0;
if (left < lastLeft) {
if (x < dragBounds.x1) {
deltaX = x - dragBounds.x1;
} else if (x > dragBounds.x2) {
deltaX = x - dragBounds.x2;
}
}
if (top < lastTop) {
if (y < dragBounds.y1) {
deltaY = y - dragBounds.y1;
} else if (y > dragBounds.y2) {
deltaY = y - dragBounds.y2;
}
}
if (deltaX !== 0 || deltaY !== 0) {
// Have it go faster depending on how far out they've dragged
this.startTimer(
grid,
gridPoint,
deltaX > 0
? Math.ceil(deltaX / columnWidth)
: Math.floor(deltaX / columnWidth),
deltaY > 0
? Math.ceil(deltaY / rowHeight)
: Math.floor(deltaY / rowHeight)
);
} else if (row != null && column != null) {
const {
floatingTopRowCount,
floatingBottomRowCount,
floatingLeftColumnCount,
floatingRightColumnCount,
columnCount,
rowCount,
bottom,
right,
topVisible,
bottomVisible,
leftVisible,
rightVisible,
} = metrics;
// When selection crosses from a floating area to a non floating area, we need to scroll instead of jumping to the floating area
// So when that happens, just adjust the point to be past the new boundary
if (!this.hasExtendedFloating) {
if (
startRow !== null &&
startRow < floatingTopRowCount &&
row >= floatingTopRowCount
) {
// Extending from floating top into the view
row = floatingTopRowCount;
this.hasExtendedFloating = true;
} else if (
startRow !== null &&
startRow >= rowCount - floatingBottomRowCount &&
row < rowCount - floatingBottomRowCount
) {
// Extending from floating bottom into the view
row = rowCount - floatingBottomRowCount - 1;
this.hasExtendedFloating = true;
}
if (
startColumn !== null &&
startColumn < floatingLeftColumnCount &&
column >= floatingLeftColumnCount
) {
// Extending from floating left into the view
column = floatingLeftColumnCount;
this.hasExtendedFloating = true;
} else if (
startColumn !== null &&
startColumn >= columnCount - floatingRightColumnCount &&
column < columnCount - floatingRightColumnCount
) {
// Extending from floating right into the view
column = columnCount - floatingRightColumnCount - 1;
this.hasExtendedFloating = true;
}
}
// When a selection is dragging from within the main area to over a floating area, scroll.
if (
startRow !== null &&
!GridUtils.isFloatingRow(startRow, metrics) &&
GridUtils.isFloatingRow(row, metrics)
) {
// Need to scroll
if (startRow > row && row < top) {
row = topVisible - 1;
} else if (startRow < row && row > bottom) {
row = bottomVisible + 1;
}
}
if (
startColumn !== null &&
!GridUtils.isFloatingColumn(startColumn, metrics) &&
GridUtils.isFloatingColumn(column, metrics)
) {
if (startColumn > column && column < left) {
column = leftVisible - 1;
} else if (startColumn < column && column > right) {
column = rightVisible + 1;
}
}
grid.moveCursorToPosition(column, row, true, true);
}
return true;
}
onUp(gridPoint: GridPoint, grid: Grid): EventHandlerResult {
if (this.startPoint !== undefined) {
this.startPoint = undefined;
this.stopTimer();
grid.commitSelection();
}
return false;
}
onContextMenu(
gridPoint: GridPoint,
grid: Grid,
event: GridMouseEvent
): EventHandlerResult {
// check if the selected is already in the selected range
const selectedRanges = grid.getSelectedRanges();
const isInRange = GridRange.containsCell(
selectedRanges,
gridPoint.column,
gridPoint.row
);
// only change the selected range if the selected cell is not in the selected range
if (!isInRange && gridPoint.row !== null) {
this.startPoint = undefined;
this.stopTimer();
grid.clearSelectedRanges();
grid.moveCursorToPosition(gridPoint.column, gridPoint.row);
}
return false;
}
moveSelection(
grid: Grid,
gridPoint: GridPoint,
deltaX: number,
deltaY: number
): void {
const { row, column } = gridPoint;
const { metrics } = grid;
if (!metrics) throw new Error('metrics not set');
const { selectionEndRow, selectionEndColumn } = grid.state;
if (selectionEndRow == null || selectionEndColumn == null) {
throw new Error('selection not set');
}
const { rowCount, columnCount } = metrics;
const minX = deltaX < 0 && column != null ? column : 0;
const maxX = deltaX > 0 && column != null ? column : columnCount - 1;
const minY = deltaY < 0 && row != null ? row : 0;
const maxY = deltaY > 0 && row != null ? row : rowCount - 1;
grid.moveCursorToPosition(
Math.min(Math.max(minX, selectionEndColumn + deltaX), maxX),
Math.min(Math.max(minY, selectionEndRow + deltaY), maxY),
true
);
this.lastTriggerTime = Date.now();
}
startTimer(
grid: Grid,
gridPoint: GridPoint,
deltaX: number,
deltaY: number
): void {
this.stopTimer();
const timeout =
this.lastTriggerTime != null
? DEFAULT_INTERVAL_MS -
Math.min(DEFAULT_INTERVAL_MS, Date.now() - this.lastTriggerTime)
: 0;
this.timer = setTimeout(() => {
this.moveSelection(grid, gridPoint, deltaX, deltaY);
this.startTimer(grid, gridPoint, deltaX, deltaY);
}, timeout);
}
stopTimer(): void {
if (this.timer !== undefined) {
clearTimeout(this.timer);
this.timer = undefined;
}
}
}
export default GridSelectionMouseHandler;