-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathbasicmousehandler.ts
1073 lines (911 loc) · 26.9 KB
/
basicmousehandler.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 { IDisposable } from '@lumino/disposable';
import { Platform } from '@lumino/domutils';
import { Drag } from '@lumino/dragdrop';
import { DataGrid } from './datagrid';
import { HyperlinkRenderer } from './hyperlinkrenderer';
import { DataModel } from './datamodel';
import { SelectionModel } from './selectionmodel';
import { CellEditor } from './celleditor';
import { CellGroup } from './cellgroup';
import { CellRenderer } from './cellrenderer';
import { TextRenderer } from './textrenderer';
/**
* A basic implementation of a data grid mouse handler.
*
* #### Notes
* This class may be subclassed and customized as needed.
*/
export class BasicMouseHandler implements DataGrid.IMouseHandler {
/**
* Dispose of the resources held by the mouse handler.
*/
dispose(): void {
// Bail early if the handler is already disposed.
if (this._disposed) {
return;
}
// Release any held resources.
this.release();
// Mark the handler as disposed.
this._disposed = true;
}
/**
* Whether the mouse handler is disposed.
*/
get isDisposed(): boolean {
return this._disposed;
}
/**
* Release the resources held by the handler.
*/
release(): void {
// Bail early if the is no press data.
if (!this._pressData) {
return;
}
// Clear the autoselect timeout.
if (this._pressData.type === 'select') {
this._pressData.timeout = -1;
}
// Clear the press data.
this._pressData.override.dispose();
this._pressData = null;
}
/**
* Handle the mouse hover event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse hover event of interest.
*/
onMouseHover(grid: DataGrid, event: MouseEvent): void {
// Hit test the grid.
let hit = grid.hitTest(event.clientX, event.clientY);
// Get the resize handle for the hit test.
let handle = Private.resizeHandleForHitTest(hit);
// Fetch the cursor for the handle.
let cursor = this.cursorForHandle(handle);
// Hyperlink logic.
const config = Private.createCellConfigObject(grid, hit);
if (config) {
// Retrieve renderer for hovered cell.
const renderer = grid.cellRenderers.get(config);
if (renderer instanceof HyperlinkRenderer) {
cursor = this.cursorForHandle('hyperlink');
}
}
// Update the viewport cursor based on the part.
grid.viewport.node.style.cursor = cursor;
// TODO support user-defined hover items
}
/**
* Handle the mouse leave event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse hover event of interest.
*/
onMouseLeave(grid: DataGrid, event: MouseEvent): void {
// TODO support user-defined hover popups.
// Clear the viewport cursor.
grid.viewport.node.style.cursor = '';
}
/**
* Handle the mouse down event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse down event of interest.
*/
onMouseDown(grid: DataGrid, event: MouseEvent): void {
// Unpack the event.
let { clientX, clientY } = event;
// Hit test the grid.
let hit = grid.hitTest(clientX, clientY);
// Unpack the hit test.
const { region, row, column } = hit;
// Bail if the hit test is on an uninteresting region.
if (region === 'void') {
return;
}
// Fetch the modifier flags.
let shift = event.shiftKey;
let accel = Platform.accelKey(event);
// Hyperlink logic.
if (grid) {
// Create cell config object.
const config = Private.createCellConfigObject(grid, hit);
// Retrieve cell renderer.
let renderer = grid.cellRenderers.get(config!);
// Only process hyperlink renderers.
if (renderer instanceof HyperlinkRenderer) {
// Use the url param if it exists.
let url = CellRenderer.resolveOption(renderer.url, config!);
// Otherwise assume cell value is the URL.
if (!url) {
const format = TextRenderer.formatGeneric();
url = format(config!);
}
// Open the hyperlink only if user hit Ctrl+Click.
if (accel) {
window.open(url);
// Reset cursor default after clicking
const cursor = this.cursorForHandle('none');
grid.viewport.node.style.cursor = cursor;
// Not applying selections if navigating away.
return;
}
}
}
// If the hit test is the body region, the only option is select.
if (region === 'body') {
// Fetch the selection model.
let model = grid.selectionModel;
// Bail early if there is no selection model.
if (!model) {
return;
}
// Override the document cursor.
let override = Drag.overrideCursor('default');
// Set up the press data.
this._pressData = {
type: 'select',
region,
row,
column,
override,
localX: -1,
localY: -1,
timeout: -1
};
// Set up the selection variables.
let r1: number;
let c1: number;
let r2: number;
let c2: number;
let cursorRow: number;
let cursorColumn: number;
let clear: SelectionModel.ClearMode;
// Accel == new selection, keep old selections.
if (accel) {
r1 = row;
r2 = row;
c1 = column;
c2 = column;
cursorRow = row;
cursorColumn = column;
clear = 'none';
} else if (shift) {
r1 = model.cursorRow;
r2 = row;
c1 = model.cursorColumn;
c2 = column;
cursorRow = model.cursorRow;
cursorColumn = model.cursorColumn;
clear = 'current';
} else {
r1 = row;
r2 = row;
c1 = column;
c2 = column;
cursorRow = row;
cursorColumn = column;
clear = 'all';
}
// Make the selection.
model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
// Done.
return;
}
// Otherwise, the hit test is on a header region.
// Convert the hit test into a part.
let handle = Private.resizeHandleForHitTest(hit);
// Fetch the cursor for the handle.
let cursor = this.cursorForHandle(handle);
// Handle horizontal resize.
if (handle === 'left' || handle === 'right') {
// Set up the resize data type.
const type = 'column-resize';
// Determine the column region.
let rgn: DataModel.ColumnRegion =
region === 'column-header' ? 'body' : 'row-header';
// Determine the section index.
let index = handle === 'left' ? column - 1 : column;
// Fetch the section size.
let size = grid.columnSize(rgn, index);
// Override the document cursor.
let override = Drag.overrideCursor(cursor);
// Create the temporary press data.
this._pressData = { type, region: rgn, index, size, clientX, override };
// Done.
return;
}
// Handle vertical resize
if (handle === 'top' || handle === 'bottom') {
// Set up the resize data type.
const type = 'row-resize';
// Determine the row region.
let rgn: DataModel.RowRegion =
region === 'row-header' ? 'body' : 'column-header';
// Determine the section index.
let index = handle === 'top' ? row - 1 : row;
// Fetch the section size.
let size = grid.rowSize(rgn, index);
// Override the document cursor.
let override = Drag.overrideCursor(cursor);
// Create the temporary press data.
this._pressData = { type, region: rgn, index, size, clientY, override };
// Done.
return;
}
// Otherwise, the only option is select.
// Fetch the selection model.
let model = grid.selectionModel;
// Bail if there is no selection model.
if (!model) {
return;
}
// Override the document cursor.
let override = Drag.overrideCursor('default');
// Set up the press data.
this._pressData = {
type: 'select',
region,
row,
column,
override,
localX: -1,
localY: -1,
timeout: -1
};
// Set up the selection variables.
let r1: number;
let c1: number;
let r2: number;
let c2: number;
let cursorRow: number;
let cursorColumn: number;
let clear: SelectionModel.ClearMode;
// Compute the selection based on the pressed region.
if (region === 'corner-header') {
r1 = 0;
r2 = Infinity;
c1 = 0;
c2 = Infinity;
cursorRow = accel ? 0 : shift ? model.cursorRow : 0;
cursorColumn = accel ? 0 : shift ? model.cursorColumn : 0;
clear = accel ? 'none' : shift ? 'current' : 'all';
} else if (region === 'row-header') {
r1 = accel ? row : shift ? model.cursorRow : row;
r2 = row;
const selectionGroup: CellGroup = { r1: r1, c1: 0, r2: r2, c2: 0 };
const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
grid.dataModel!,
['row-header', 'body'],
'row',
selectionGroup
);
// Check if there are any merges
if (joinedGroup.r1 != Number.MAX_VALUE) {
r1 = joinedGroup.r1;
r2 = joinedGroup.r2;
}
c1 = 0;
c2 = Infinity;
cursorRow = accel ? row : shift ? model.cursorRow : row;
cursorColumn = accel ? 0 : shift ? model.cursorColumn : 0;
clear = accel ? 'none' : shift ? 'current' : 'all';
} else if (region === 'column-header') {
r1 = 0;
r2 = Infinity;
c1 = accel ? column : shift ? model.cursorColumn : column;
c2 = column;
const selectionGroup: CellGroup = { r1: 0, c1: c1, r2: 0, c2: c2 };
const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
grid.dataModel!,
['column-header', 'body'],
'column',
selectionGroup
);
// Check if there are any merges
if (joinedGroup.c1 != Number.MAX_VALUE) {
c1 = joinedGroup.c1;
c2 = joinedGroup.c2;
}
cursorRow = accel ? 0 : shift ? model.cursorRow : 0;
cursorColumn = accel ? column : shift ? model.cursorColumn : column;
clear = accel ? 'none' : shift ? 'current' : 'all';
} else {
r1 = accel ? row : shift ? model.cursorRow : row;
r2 = row;
c1 = accel ? column : shift ? model.cursorColumn : column;
c2 = column;
cursorRow = accel ? row : shift ? model.cursorRow : row;
cursorColumn = accel ? column : shift ? model.cursorColumn : column;
clear = accel ? 'none' : shift ? 'current' : 'all';
}
// Make the selection.
model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
}
/**
* Handle the mouse move event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse move event of interest.
*/
onMouseMove(grid: DataGrid, event: MouseEvent): void {
// Fetch the press data.
const data = this._pressData;
// Bail early if there is no press data.
if (!data) {
return;
}
// Handle a row resize.
if (data.type === 'row-resize') {
let dy = event.clientY - data.clientY;
grid.resizeRow(data.region, data.index, data.size + dy);
return;
}
// Handle a column resize.
if (data.type === 'column-resize') {
let dx = event.clientX - data.clientX;
grid.resizeColumn(data.region, data.index, data.size + dx);
return;
}
// Otherwise, it's a select.
// Mouse moves during a corner header press are a no-op.
if (data.region === 'corner-header') {
return;
}
// Fetch the selection model.
let model = grid.selectionModel;
// Bail early if the selection model was removed.
if (!model) {
return;
}
// Map to local coordinates.
let { lx, ly } = grid.mapToLocal(event.clientX, event.clientY);
// Update the local mouse coordinates in the press data.
data.localX = lx;
data.localY = ly;
// Fetch the grid geometry.
let hw = grid.headerWidth;
let hh = grid.headerHeight;
let vpw = grid.viewportWidth;
let vph = grid.viewportHeight;
let sx = grid.scrollX;
let sy = grid.scrollY;
let msx = grid.maxScrollY;
let msy = grid.maxScrollY;
// Fetch the selection mode.
let mode = model.selectionMode;
// Set up the timeout variable.
let timeout = -1;
// Compute the timemout based on hit region and mouse position.
if (data.region === 'row-header' || mode === 'row') {
if (ly < hh && sy > 0) {
timeout = Private.computeTimeout(hh - ly);
} else if (ly >= vph && sy < msy) {
timeout = Private.computeTimeout(ly - vph);
}
} else if (data.region === 'column-header' || mode === 'column') {
if (lx < hw && sx > 0) {
timeout = Private.computeTimeout(hw - lx);
} else if (lx >= vpw && sx < msx) {
timeout = Private.computeTimeout(lx - vpw);
}
} else {
if (lx < hw && sx > 0) {
timeout = Private.computeTimeout(hw - lx);
} else if (lx >= vpw && sx < msx) {
timeout = Private.computeTimeout(lx - vpw);
} else if (ly < hh && sy > 0) {
timeout = Private.computeTimeout(hh - ly);
} else if (ly >= vph && sy < msy) {
timeout = Private.computeTimeout(ly - vph);
}
}
// Update or initiate the autoselect if needed.
if (timeout >= 0) {
if (data.timeout < 0) {
data.timeout = timeout;
setTimeout(() => {
Private.autoselect(grid, data);
}, timeout);
} else {
data.timeout = timeout;
}
return;
}
// Otherwise, clear the autoselect timeout.
data.timeout = -1;
// Map the position to virtual coordinates.
let { vx, vy } = grid.mapToVirtual(event.clientX, event.clientY);
// Clamp the coordinates to the limits.
vx = Math.max(0, Math.min(vx, grid.bodyWidth - 1));
vy = Math.max(0, Math.min(vy, grid.bodyHeight - 1));
// Set up the selection variables.
let r1: number;
let c1: number;
let r2: number;
let c2: number;
let cursorRow = model.cursorRow;
let cursorColumn = model.cursorColumn;
let clear: SelectionModel.ClearMode = 'current';
// Compute the selection based pressed region.
if (data.region === 'row-header' || mode === 'row') {
r1 = data.row;
r2 = grid.rowAt('body', vy);
const selectionGroup: CellGroup = { r1: r1, c1: 0, r2: r2, c2: 0 };
const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
grid.dataModel!,
['row-header', 'body'],
'row',
selectionGroup
);
// Check if there are any merges
if (joinedGroup.r1 != Number.MAX_VALUE) {
r1 = Math.min(r1, joinedGroup.r1);
r2 = Math.max(r2, joinedGroup.r2);
}
c1 = 0;
c2 = Infinity;
} else if (data.region === 'column-header' || mode === 'column') {
r1 = 0;
r2 = Infinity;
c1 = data.column;
c2 = grid.columnAt('body', vx);
const selectionGroup: CellGroup = { r1: 0, c1: c1, r2: 0, c2: c2 };
const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
grid.dataModel!,
['column-header', 'body'],
'column',
selectionGroup
);
// Check if there are any merges
if (joinedGroup.c1 != Number.MAX_VALUE) {
c1 = joinedGroup.c1;
c2 = joinedGroup.c2;
}
} else {
r1 = cursorRow;
r2 = grid.rowAt('body', vy);
c1 = cursorColumn;
c2 = grid.columnAt('body', vx);
}
// Make the selection.
model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
}
/**
* Handle the mouse up event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse up event of interest.
*/
onMouseUp(grid: DataGrid, event: MouseEvent): void {
this.release();
}
/**
* Handle the mouse double click event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The mouse up event of interest.
*/
onMouseDoubleClick(grid: DataGrid, event: MouseEvent): void {
if (!grid.dataModel) {
this.release();
return;
}
// Unpack the event.
let { clientX, clientY } = event;
// Hit test the grid.
let hit = grid.hitTest(clientX, clientY);
// Unpack the hit test.
let { region, row, column } = hit;
if (region === 'void') {
this.release();
return;
}
if (region === 'body') {
if (grid.editable) {
const cell: CellEditor.CellConfig = {
grid: grid,
row: row,
column: column
};
grid.editorController!.edit(cell);
}
}
this.release();
}
/**
* Handle the context menu event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The context menu event of interest.
*/
onContextMenu(grid: DataGrid, event: MouseEvent): void {
// TODO support user-defined context menus
}
/**
* Handle the wheel event for the data grid.
*
* @param grid - The data grid of interest.
*
* @param event - The wheel event of interest.
*/
onWheel(grid: DataGrid, event: WheelEvent): void {
// Bail if a mouse press is in progress.
if (this._pressData) {
return;
}
// Extract the delta X and Y movement.
let dx = event.deltaX;
let dy = event.deltaY;
// Convert the delta values to pixel values.
switch (event.deltaMode) {
case 0: // DOM_DELTA_PIXEL
break;
case 1: {
// DOM_DELTA_LINE
let ds = grid.defaultSizes;
dx *= ds.columnWidth;
dy *= ds.rowHeight;
break;
}
case 2: // DOM_DELTA_PAGE
dx *= grid.pageWidth;
dy *= grid.pageHeight;
break;
default:
throw 'unreachable';
}
// Only scroll and stop the event propagation if needed.
if (
// Scrolling left and not reached min already
(dx < 0 && grid.scrollX !== 0) ||
// Scrolling right and not reached max already
(dx > 0 && grid.scrollX !== grid.maxScrollX) ||
// Scrolling top and not reached min already
(dy < 0 && grid.scrollY !== 0) ||
// Scrolling down and not reached max already
(dy > 0 && grid.scrollY !== grid.maxScrollY)
) {
event.preventDefault();
event.stopPropagation();
// Scroll by the desired amount.
grid.scrollBy(dx, dy);
}
}
/**
* Convert a resize handle into a cursor.
*/
cursorForHandle(handle: ResizeHandle): string {
return Private.cursorMap[handle];
}
/**
* Get the current pressData
*/
get pressData(): PressData.PressData | null {
return this._pressData;
}
private _disposed = false;
protected _pressData: PressData.PressData | null = null;
}
/**
* A type alias for the resize handle types.
*/
export type ResizeHandle =
| 'top'
| 'left'
| 'right'
| 'bottom'
| 'none'
| 'hyperlink';
/**
* The namespace for the pressdata.
*/
export namespace PressData {
/**
* A type alias for the row resize data.
*/
export type RowResizeData = {
/**
* The descriminated type for the data.
*/
readonly type: 'row-resize';
/**
* The row region which holds the section being resized.
*/
readonly region: DataModel.RowRegion;
/**
* The index of the section being resized.
*/
readonly index: number;
/**
* The original size of the section.
*/
readonly size: number;
/**
* The original client Y position of the mouse.
*/
readonly clientY: number;
/**
* The disposable to clear the cursor override.
*/
readonly override: IDisposable;
};
/**
* A type alias for the column resize data.
*/
export type ColumnResizeData = {
/**
* The descriminated type for the data.
*/
readonly type: 'column-resize';
/**
* The column region which holds the section being resized.
*/
readonly region: DataModel.ColumnRegion;
/**
* The index of the section being resized.
*/
readonly index: number;
/**
* The original size of the section.
*/
readonly size: number;
/**
* The original client X position of the mouse.
*/
readonly clientX: number;
/**
* The disposable to clear the cursor override.
*/
readonly override: IDisposable;
};
/**
* A type alias for the select data.
*/
export type SelectData = {
/**
* The descriminated type for the data.
*/
readonly type: 'select';
/**
* The original region for the mouse press.
*/
readonly region: DataModel.CellRegion;
/**
* The original row that was selected.
*/
readonly row: number;
/**
* The original column that was selected.
*/
readonly column: number;
/**
* The disposable to clear the cursor override.
*/
readonly override: IDisposable;
/**
* The current local X position of the mouse.
*/
localX: number;
/**
* The current local Y position of the mouse.
*/
localY: number;
/**
* The timeout delay for the autoselect loop.
*/
timeout: number;
};
/**
* A type alias for the resize handler press data.
*/
export type PressData = RowResizeData | ColumnResizeData | SelectData;
}
/**
* The namespace for the module implementation details.
*/
namespace Private {
/**
* Creates a CellConfig object from a hit region.
*/
export function createCellConfigObject(
grid: DataGrid,
hit: DataGrid.HitTestResult
): CellRenderer.CellConfig | undefined {
const { region, row, column } = hit;
// Terminate call if region is void.
if (region === 'void') {
return undefined;
}
// Augment hit region params with value and metadata.
const value = grid.dataModel!.data(region, row, column);
const metadata = grid.dataModel!.metadata(region, row, column);
// Create cell config object to retrieve cell renderer.
const config = {
...hit,
value: value,
metadata: metadata
} as CellRenderer.CellConfig;
return config;
}
/**
* Get the resize handle for a grid hit test.
*/
export function resizeHandleForHitTest(
hit: DataGrid.HitTestResult
): ResizeHandle {
// Fetch the row and column.
let r = hit.row;
let c = hit.column;
// Fetch the leading and trailing sizes.
let lw = hit.x;
let lh = hit.y;
let tw = hit.width - hit.x;
let th = hit.height - hit.y;
// Set up the result variable.
let result: ResizeHandle;
// Dispatch based on hit test region.
switch (hit.region) {
case 'corner-header':
if (c > 0 && lw <= 5) {
result = 'left';
} else if (tw <= 6) {
result = 'right';
} else if (r > 0 && lh <= 5) {
result = 'top';
} else if (th <= 6) {
result = 'bottom';
} else {
result = 'none';
}
break;
case 'column-header':
if (c > 0 && lw <= 5) {
result = 'left';
} else if (tw <= 6) {
result = 'right';
} else if (r > 0 && lh <= 5) {
result = 'top';
} else if (th <= 6) {
result = 'bottom';
} else {
result = 'none';
}
break;
case 'row-header':
if (c > 0 && lw <= 5) {
result = 'left';
} else if (tw <= 6) {
result = 'right';
} else if (r > 0 && lh <= 5) {
result = 'top';
} else if (th <= 6) {
result = 'bottom';
} else {
result = 'none';
}
break;
case 'body':
result = 'none';
break;
case 'void':
result = 'none';
break;
default:
throw 'unreachable';
}
// Return the result.
return result;
}
/**
* A timer callback for the autoselect loop.
*
* @param grid - The datagrid of interest.
*
* @param data - The select data of interest.
*/
export function autoselect(grid: DataGrid, data: PressData.SelectData): void {
// Bail early if the timeout has been reset.
if (data.timeout < 0) {
return;
}
// Fetch the selection model.
let model = grid.selectionModel;
// Bail early if the selection model has been removed.
if (!model) {
return;
}
// Fetch the current selection.
let cs = model.currentSelection();
// Bail early if there is no current selection.
if (!cs) {
return;
}
// Fetch local X and Y coordinates of the mouse.
let lx = data.localX;
let ly = data.localY;
// Set up the selection variables.
let r1 = cs.r1;
let c1 = cs.c1;
let r2 = cs.r2;