Debian Codegraph @

packages/datagrid/src/basicmousehandler.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2019, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10 import { IDisposable } from '@lumino/disposable';
11
12 import { Platform } from '@lumino/domutils';
13
14 import { Drag } from '@lumino/dragdrop';
15
16 import { DataGrid } from './datagrid';
17
18 import { HyperlinkRenderer } from './hyperlinkrenderer';
19
20 import { DataModel } from './datamodel';
21
22 import { SelectionModel } from './selectionmodel';
23
24 import { CellEditor } from './celleditor';
25
26 import { CellGroup } from './cellgroup';
27
28 import { CellRenderer } from './cellrenderer';
29
30 import { TextRenderer } from './textrenderer';
31
32 /**
33 * A basic implementation of a data grid mouse handler.
34 *
35 * #### Notes
36 * This class may be subclassed and customized as needed.
37 */
38 export class BasicMouseHandler implements DataGrid.IMouseHandler {
39 /**
40 * Dispose of the resources held by the mouse handler.
41 */
42 dispose(): void {
43 // Bail early if the handler is already disposed.
44 if (this._disposed) {
45 return;
46 }
47
48 // Release any held resources.
49 this.release();
50
51 // Mark the handler as disposed.
52 this._disposed = true;
53 }
54
55 /**
56 * Whether the mouse handler is disposed.
57 */
58 get isDisposed(): boolean {
59 return this._disposed;
60 }
61
62 /**
63 * Release the resources held by the handler.
64 */
65 release(): void {
66 // Bail early if the is no press data.
67 if (!this._pressData) {
68 return;
69 }
70
71 // Clear the autoselect timeout.
72 if (this._pressData.type === 'select') {
73 this._pressData.timeout = -1;
74 }
75
76 // Clear the press data.
77 this._pressData.override.dispose();
78 this._pressData = null;
79 }
80
81 /**
82 * Handle the mouse hover event for the data grid.
83 *
84 * @param grid - The data grid of interest.
85 *
86 * @param event - The mouse hover event of interest.
87 */
88 onMouseHover(grid: DataGrid, event: MouseEvent): void {
89 // Hit test the grid.
90 let hit = grid.hitTest(event.clientX, event.clientY);
91
92 // Get the resize handle for the hit test.
93 let handle = Private.resizeHandleForHitTest(hit);
94
95 // Fetch the cursor for the handle.
96 let cursor = this.cursorForHandle(handle);
97
98 // Hyperlink logic.
99 const config = Private.createCellConfigObject(grid, hit);
100
101 if (config) {
102 // Retrieve renderer for hovered cell.
103 const renderer = grid.cellRenderers.get(config);
104 if (renderer instanceof HyperlinkRenderer) {
105 cursor = this.cursorForHandle('hyperlink');
106 }
107 }
108
109 // Update the viewport cursor based on the part.
110 grid.viewport.node.style.cursor = cursor;
111
112 // TODO support user-defined hover items
113 }
114
115 /**
116 * Handle the mouse leave event for the data grid.
117 *
118 * @param grid - The data grid of interest.
119 *
120 * @param event - The mouse hover event of interest.
121 */
122 onMouseLeave(grid: DataGrid, event: MouseEvent): void {
123 // TODO support user-defined hover popups.
124
125 // Clear the viewport cursor.
126 grid.viewport.node.style.cursor = '';
127 }
128
129 /**
130 * Handle the mouse down event for the data grid.
131 *
132 * @param grid - The data grid of interest.
133 *
134 * @param event - The mouse down event of interest.
135 */
136 onMouseDown(grid: DataGrid, event: MouseEvent): void {
137 // Unpack the event.
138 let { clientX, clientY } = event;
139
140 // Hit test the grid.
141 let hit = grid.hitTest(clientX, clientY);
142
143 // Unpack the hit test.
144 const { region, row, column } = hit;
145
146 // Bail if the hit test is on an uninteresting region.
147 if (region === 'void') {
148 return;
149 }
150
151 // Fetch the modifier flags.
152 let shift = event.shiftKey;
153 let accel = Platform.accelKey(event);
154
155 // Hyperlink logic.
156 if (grid) {
157 // Create cell config object.
158 const config = Private.createCellConfigObject(grid, hit);
159
160 // Retrieve cell renderer.
161 let renderer = grid.cellRenderers.get(config!);
162
163 // Only process hyperlink renderers.
164 if (renderer instanceof HyperlinkRenderer) {
165 // Use the url param if it exists.
166 let url = CellRenderer.resolveOption(renderer.url, config!);
167 // Otherwise assume cell value is the URL.
168 if (!url) {
169 const format = TextRenderer.formatGeneric();
170 url = format(config!);
171 }
172
173 // Open the hyperlink only if user hit Ctrl+Click.
174 if (accel) {
175 window.open(url);
176 // Reset cursor default after clicking
177 const cursor = this.cursorForHandle('none');
178 grid.viewport.node.style.cursor = cursor;
179 // Not applying selections if navigating away.
180 return;
181 }
182 }
183 }
184
185 // If the hit test is the body region, the only option is select.
186 if (region === 'body') {
187 // Fetch the selection model.
188 let model = grid.selectionModel;
189
190 // Bail early if there is no selection model.
191 if (!model) {
192 return;
193 }
194
195 // Override the document cursor.
196 let override = Drag.overrideCursor('default');
197
198 // Set up the press data.
199 this._pressData = {
200 type: 'select',
201 region,
202 row,
203 column,
204 override,
205 localX: -1,
206 localY: -1,
207 timeout: -1
208 };
209
210 // Set up the selection variables.
211 let r1: number;
212 let c1: number;
213 let r2: number;
214 let c2: number;
215 let cursorRow: number;
216 let cursorColumn: number;
217 let clear: SelectionModel.ClearMode;
218
219 // Accel == new selection, keep old selections.
220 if (accel) {
221 r1 = row;
222 r2 = row;
223 c1 = column;
224 c2 = column;
225 cursorRow = row;
226 cursorColumn = column;
227 clear = 'none';
228 } else if (shift) {
229 r1 = model.cursorRow;
230 r2 = row;
231 c1 = model.cursorColumn;
232 c2 = column;
233 cursorRow = model.cursorRow;
234 cursorColumn = model.cursorColumn;
235 clear = 'current';
236 } else {
237 r1 = row;
238 r2 = row;
239 c1 = column;
240 c2 = column;
241 cursorRow = row;
242 cursorColumn = column;
243 clear = 'all';
244 }
245
246 // Make the selection.
247 model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
248
249 // Done.
250 return;
251 }
252
253 // Otherwise, the hit test is on a header region.
254
255 // Convert the hit test into a part.
256 let handle = Private.resizeHandleForHitTest(hit);
257
258 // Fetch the cursor for the handle.
259 let cursor = this.cursorForHandle(handle);
260
261 // Handle horizontal resize.
262 if (handle === 'left' || handle === 'right') {
263 // Set up the resize data type.
264 const type = 'column-resize';
265
266 // Determine the column region.
267 let rgn: DataModel.ColumnRegion =
268 region === 'column-header' ? 'body' : 'row-header';
269
270 // Determine the section index.
271 let index = handle === 'left' ? column - 1 : column;
272
273 // Fetch the section size.
274 let size = grid.columnSize(rgn, index);
275
276 // Override the document cursor.
277 let override = Drag.overrideCursor(cursor);
278
279 // Create the temporary press data.
280 this._pressData = { type, region: rgn, index, size, clientX, override };
281
282 // Done.
283 return;
284 }
285
286 // Handle vertical resize
287 if (handle === 'top' || handle === 'bottom') {
288 // Set up the resize data type.
289 const type = 'row-resize';
290
291 // Determine the row region.
292 let rgn: DataModel.RowRegion =
293 region === 'row-header' ? 'body' : 'column-header';
294
295 // Determine the section index.
296 let index = handle === 'top' ? row - 1 : row;
297
298 // Fetch the section size.
299 let size = grid.rowSize(rgn, index);
300
301 // Override the document cursor.
302 let override = Drag.overrideCursor(cursor);
303
304 // Create the temporary press data.
305 this._pressData = { type, region: rgn, index, size, clientY, override };
306
307 // Done.
308 return;
309 }
310
311 // Otherwise, the only option is select.
312
313 // Fetch the selection model.
314 let model = grid.selectionModel;
315
316 // Bail if there is no selection model.
317 if (!model) {
318 return;
319 }
320
321 // Override the document cursor.
322 let override = Drag.overrideCursor('default');
323
324 // Set up the press data.
325 this._pressData = {
326 type: 'select',
327 region,
328 row,
329 column,
330 override,
331 localX: -1,
332 localY: -1,
333 timeout: -1
334 };
335
336 // Set up the selection variables.
337 let r1: number;
338 let c1: number;
339 let r2: number;
340 let c2: number;
341 let cursorRow: number;
342 let cursorColumn: number;
343 let clear: SelectionModel.ClearMode;
344
345 // Compute the selection based on the pressed region.
346 if (region === 'corner-header') {
347 r1 = 0;
348 r2 = Infinity;
349 c1 = 0;
350 c2 = Infinity;
351 cursorRow = accel ? 0 : shift ? model.cursorRow : 0;
352 cursorColumn = accel ? 0 : shift ? model.cursorColumn : 0;
353 clear = accel ? 'none' : shift ? 'current' : 'all';
354 } else if (region === 'row-header') {
355 r1 = accel ? row : shift ? model.cursorRow : row;
356 r2 = row;
357
358 const selectionGroup: CellGroup = { r1: r1, c1: 0, r2: r2, c2: 0 };
359 const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
360 grid.dataModel!,
361 ['row-header', 'body'],
362 'row',
363 selectionGroup
364 );
365 // Check if there are any merges
366 if (joinedGroup.r1 != Number.MAX_VALUE) {
367 r1 = joinedGroup.r1;
368 r2 = joinedGroup.r2;
369 }
370
371 c1 = 0;
372 c2 = Infinity;
373 cursorRow = accel ? row : shift ? model.cursorRow : row;
374 cursorColumn = accel ? 0 : shift ? model.cursorColumn : 0;
375 clear = accel ? 'none' : shift ? 'current' : 'all';
376 } else if (region === 'column-header') {
377 r1 = 0;
378 r2 = Infinity;
379 c1 = accel ? column : shift ? model.cursorColumn : column;
380 c2 = column;
381
382 const selectionGroup: CellGroup = { r1: 0, c1: c1, r2: 0, c2: c2 };
383 const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
384 grid.dataModel!,
385 ['column-header', 'body'],
386 'column',
387 selectionGroup
388 );
389 // Check if there are any merges
390 if (joinedGroup.c1 != Number.MAX_VALUE) {
391 c1 = joinedGroup.c1;
392 c2 = joinedGroup.c2;
393 }
394
395 cursorRow = accel ? 0 : shift ? model.cursorRow : 0;
396 cursorColumn = accel ? column : shift ? model.cursorColumn : column;
397 clear = accel ? 'none' : shift ? 'current' : 'all';
398 } else {
399 r1 = accel ? row : shift ? model.cursorRow : row;
400 r2 = row;
401 c1 = accel ? column : shift ? model.cursorColumn : column;
402 c2 = column;
403 cursorRow = accel ? row : shift ? model.cursorRow : row;
404 cursorColumn = accel ? column : shift ? model.cursorColumn : column;
405 clear = accel ? 'none' : shift ? 'current' : 'all';
406 }
407
408 // Make the selection.
409 model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
410 }
411
412 /**
413 * Handle the mouse move event for the data grid.
414 *
415 * @param grid - The data grid of interest.
416 *
417 * @param event - The mouse move event of interest.
418 */
419 onMouseMove(grid: DataGrid, event: MouseEvent): void {
420 // Fetch the press data.
421 const data = this._pressData;
422
423 // Bail early if there is no press data.
424 if (!data) {
425 return;
426 }
427
428 // Handle a row resize.
429 if (data.type === 'row-resize') {
430 let dy = event.clientY - data.clientY;
431 grid.resizeRow(data.region, data.index, data.size + dy);
432 return;
433 }
434
435 // Handle a column resize.
436 if (data.type === 'column-resize') {
437 let dx = event.clientX - data.clientX;
438 grid.resizeColumn(data.region, data.index, data.size + dx);
439 return;
440 }
441
442 // Otherwise, it's a select.
443
444 // Mouse moves during a corner header press are a no-op.
445 if (data.region === 'corner-header') {
446 return;
447 }
448
449 // Fetch the selection model.
450 let model = grid.selectionModel;
451
452 // Bail early if the selection model was removed.
453 if (!model) {
454 return;
455 }
456
457 // Map to local coordinates.
458 let { lx, ly } = grid.mapToLocal(event.clientX, event.clientY);
459
460 // Update the local mouse coordinates in the press data.
461 data.localX = lx;
462 data.localY = ly;
463
464 // Fetch the grid geometry.
465 let hw = grid.headerWidth;
466 let hh = grid.headerHeight;
467 let vpw = grid.viewportWidth;
468 let vph = grid.viewportHeight;
469 let sx = grid.scrollX;
470 let sy = grid.scrollY;
471 let msx = grid.maxScrollY;
472 let msy = grid.maxScrollY;
473
474 // Fetch the selection mode.
475 let mode = model.selectionMode;
476
477 // Set up the timeout variable.
478 let timeout = -1;
479
480 // Compute the timemout based on hit region and mouse position.
481 if (data.region === 'row-header' || mode === 'row') {
482 if (ly < hh && sy > 0) {
483 timeout = Private.computeTimeout(hh - ly);
484 } else if (ly >= vph && sy < msy) {
485 timeout = Private.computeTimeout(ly - vph);
486 }
487 } else if (data.region === 'column-header' || mode === 'column') {
488 if (lx < hw && sx > 0) {
489 timeout = Private.computeTimeout(hw - lx);
490 } else if (lx >= vpw && sx < msx) {
491 timeout = Private.computeTimeout(lx - vpw);
492 }
493 } else {
494 if (lx < hw && sx > 0) {
495 timeout = Private.computeTimeout(hw - lx);
496 } else if (lx >= vpw && sx < msx) {
497 timeout = Private.computeTimeout(lx - vpw);
498 } else if (ly < hh && sy > 0) {
499 timeout = Private.computeTimeout(hh - ly);
500 } else if (ly >= vph && sy < msy) {
501 timeout = Private.computeTimeout(ly - vph);
502 }
503 }
504
505 // Update or initiate the autoselect if needed.
506 if (timeout >= 0) {
507 if (data.timeout < 0) {
508 data.timeout = timeout;
509 setTimeout(() => {
510 Private.autoselect(grid, data);
511 }, timeout);
512 } else {
513 data.timeout = timeout;
514 }
515 return;
516 }
517
518 // Otherwise, clear the autoselect timeout.
519 data.timeout = -1;
520
521 // Map the position to virtual coordinates.
522 let { vx, vy } = grid.mapToVirtual(event.clientX, event.clientY);
523
524 // Clamp the coordinates to the limits.
525 vx = Math.max(0, Math.min(vx, grid.bodyWidth - 1));
526 vy = Math.max(0, Math.min(vy, grid.bodyHeight - 1));
527
528 // Set up the selection variables.
529 let r1: number;
530 let c1: number;
531 let r2: number;
532 let c2: number;
533 let cursorRow = model.cursorRow;
534 let cursorColumn = model.cursorColumn;
535 let clear: SelectionModel.ClearMode = 'current';
536
537 // Compute the selection based pressed region.
538 if (data.region === 'row-header' || mode === 'row') {
539 r1 = data.row;
540 r2 = grid.rowAt('body', vy);
541
542 const selectionGroup: CellGroup = { r1: r1, c1: 0, r2: r2, c2: 0 };
543 const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
544 grid.dataModel!,
545 ['row-header', 'body'],
546 'row',
547 selectionGroup
548 );
549 // Check if there are any merges
550 if (joinedGroup.r1 != Number.MAX_VALUE) {
551 r1 = Math.min(r1, joinedGroup.r1);
552 r2 = Math.max(r2, joinedGroup.r2);
553 }
554
555 c1 = 0;
556 c2 = Infinity;
557 } else if (data.region === 'column-header' || mode === 'column') {
558 r1 = 0;
559 r2 = Infinity;
560 c1 = data.column;
561 c2 = grid.columnAt('body', vx);
562
563 const selectionGroup: CellGroup = { r1: 0, c1: c1, r2: 0, c2: c2 };
564 const joinedGroup = CellGroup.joinCellGroupsIntersectingAtAxis(
565 grid.dataModel!,
566 ['column-header', 'body'],
567 'column',
568 selectionGroup
569 );
570 // Check if there are any merges
571 if (joinedGroup.c1 != Number.MAX_VALUE) {
572 c1 = joinedGroup.c1;
573 c2 = joinedGroup.c2;
574 }
575 } else {
576 r1 = cursorRow;
577 r2 = grid.rowAt('body', vy);
578 c1 = cursorColumn;
579 c2 = grid.columnAt('body', vx);
580 }
581
582 // Make the selection.
583 model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
584 }
585
586 /**
587 * Handle the mouse up event for the data grid.
588 *
589 * @param grid - The data grid of interest.
590 *
591 * @param event - The mouse up event of interest.
592 */
593 onMouseUp(grid: DataGrid, event: MouseEvent): void {
594 this.release();
595 }
596
597 /**
598 * Handle the mouse double click event for the data grid.
599 *
600 * @param grid - The data grid of interest.
601 *
602 * @param event - The mouse up event of interest.
603 */
604 onMouseDoubleClick(grid: DataGrid, event: MouseEvent): void {
605 if (!grid.dataModel) {
606 this.release();
607 return;
608 }
609
610 // Unpack the event.
611 let { clientX, clientY } = event;
612
613 // Hit test the grid.
614 let hit = grid.hitTest(clientX, clientY);
615
616 // Unpack the hit test.
617 let { region, row, column } = hit;
618
619 if (region === 'void') {
620 this.release();
621 return;
622 }
623
624 if (region === 'column-header' || region === 'corner-header') {
625 // Convert the hit test into a part.
626 const handle = Private.resizeHandleForHitTest(hit);
627
628 if (handle === 'left' || handle === 'right') {
629 let colIndex = handle === 'left' ? column - 1 : column;
630
631 let colRegion: DataModel.ColumnRegion =
632 region === 'column-header' ? 'body' : 'row-header';
633
634 if (colIndex < 0) {
635 if (region === 'column-header') {
636 // If the column is -1, it means we are in the corner header
637 colIndex = grid.dataModel.columnCount('row-header') - 1;
638 colRegion = 'row-header';
639 } else {
640 // If we are on the left edge of the row header, do nothing
641 return;
642 }
643 }
644 const cs = grid.selectionModel?.currentSelection();
645 const cv = grid.currentViewport;
646 const rowCount = grid.selectionModel?.dataModel.rowCount('body') ?? 0;
647 if (
648 colRegion == 'body' &&
649 cs != null &&
650 cv != null &&
651 cs.r1 == 0 &&
652 cs.r2 == rowCount - 1
653 ) {
654 // One or more columns are selected
655 let c1 = Math.max(Math.min(cs.c1, cs.c2), cv.firstColumn);
656 let c2 = Math.min(Math.max(cs.c1, cs.c2), cv.lastColumn);
657 if (c1 <= colIndex && colIndex <= c2) {
658 // When we double-click one of the selected column headers, resize all visible selected columns.
659 for (let ci = c1; ci <= c2; ci++) {
660 grid.resizeColumn(colRegion, ci, null);
661 }
662 } else {
663 // When we double-click the column header outside the selection, resize only the clicked column.
664 grid.resizeColumn(colRegion, colIndex, null);
665 }
666 } else {
667 // When no columns are selected, resize only the clicked column.
668 grid.resizeColumn(colRegion, colIndex, null);
669 }
670 }
671 }
672
673 if (region === 'body') {
674 if (grid.editable) {
675 const cell: CellEditor.CellConfig = {
676 grid: grid,
677 row: row,
678 column: column
679 };
680 grid.editorController!.edit(cell);
681 }
682 }
683
684 this.release();
685 }
686
687 /**
688 * Handle the context menu event for the data grid.
689 *
690 * @param grid - The data grid of interest.
691 *
692 * @param event - The context menu event of interest.
693 */
694 onContextMenu(grid: DataGrid, event: MouseEvent): void {
695 // TODO support user-defined context menus
696 }
697
698 /**
699 * Handle the wheel event for the data grid.
700 *
701 * @param grid - The data grid of interest.
702 *
703 * @param event - The wheel event of interest.
704 */
705 onWheel(grid: DataGrid, event: WheelEvent): void {
706 // Bail if a mouse press is in progress.
707 if (this._pressData) {
708 return;
709 }
710
711 // Extract the delta X and Y movement.
712 let dx = event.deltaX;
713 let dy = event.deltaY;
714
715 // Convert the delta values to pixel values.
716 switch (event.deltaMode) {
717 case 0: // DOM_DELTA_PIXEL
718 break;
719 case 1: {
720 // DOM_DELTA_LINE
721 let ds = grid.defaultSizes;
722 dx *= ds.columnWidth;
723 dy *= ds.rowHeight;
724 break;
725 }
726 case 2: // DOM_DELTA_PAGE
727 dx *= grid.pageWidth;
728 dy *= grid.pageHeight;
729 break;
730 default:
731 throw 'unreachable';
732 }
733
734 // Only scroll and stop the event propagation if needed.
735 if (
736 // Scrolling left and not reached min already
737 (dx < 0 && grid.scrollX !== 0) ||
738 // Scrolling right and not reached max already
739 (dx > 0 && grid.scrollX !== grid.maxScrollX) ||
740 // Scrolling top and not reached min already
741 (dy < 0 && grid.scrollY !== 0) ||
742 // Scrolling down and not reached max already
743 (dy > 0 && grid.scrollY !== grid.maxScrollY)
744 ) {
745 event.preventDefault();
746 event.stopPropagation();
747
748 // Scroll by the desired amount.
749 grid.scrollBy(dx, dy);
750 }
751 }
752
753 /**
754 * Convert a resize handle into a cursor.
755 */
756 cursorForHandle(handle: ResizeHandle): string {
757 return Private.cursorMap[handle];
758 }
759
760 /**
761 * Get the current pressData
762 */
763 get pressData(): PressData.PressData | null {
764 return this._pressData;
765 }
766
767 private _disposed = false;
768 protected _pressData: PressData.PressData | null = null;
769 }
770
771 /**
772 * A type alias for the resize handle types.
773 */
774 export type ResizeHandle =
775 | 'top'
776 | 'left'
777 | 'right'
778 | 'bottom'
779 | 'none'
780 | 'hyperlink';
781
782 /**
783 * The namespace for the pressdata.
784 */
785 export namespace PressData {
786 /**
787 * A type alias for the row resize data.
788 */
789 export type RowResizeData = {
790 /**
791 * The descriminated type for the data.
792 */
793 readonly type: 'row-resize';
794
795 /**
796 * The row region which holds the section being resized.
797 */
798 readonly region: DataModel.RowRegion;
799
800 /**
801 * The index of the section being resized.
802 */
803 readonly index: number;
804
805 /**
806 * The original size of the section.
807 */
808 readonly size: number;
809
810 /**
811 * The original client Y position of the mouse.
812 */
813 readonly clientY: number;
814
815 /**
816 * The disposable to clear the cursor override.
817 */
818 readonly override: IDisposable;
819 };
820
821 /**
822 * A type alias for the column resize data.
823 */
824 export type ColumnResizeData = {
825 /**
826 * The descriminated type for the data.
827 */
828 readonly type: 'column-resize';
829
830 /**
831 * The column region which holds the section being resized.
832 */
833 readonly region: DataModel.ColumnRegion;
834
835 /**
836 * The index of the section being resized.
837 */
838 readonly index: number;
839
840 /**
841 * The original size of the section.
842 */
843 readonly size: number;
844
845 /**
846 * The original client X position of the mouse.
847 */
848 readonly clientX: number;
849
850 /**
851 * The disposable to clear the cursor override.
852 */
853 readonly override: IDisposable;
854 };
855
856 /**
857 * A type alias for the select data.
858 */
859 export type SelectData = {
860 /**
861 * The descriminated type for the data.
862 */
863 readonly type: 'select';
864
865 /**
866 * The original region for the mouse press.
867 */
868 readonly region: DataModel.CellRegion;
869
870 /**
871 * The original row that was selected.
872 */
873 readonly row: number;
874
875 /**
876 * The original column that was selected.
877 */
878 readonly column: number;
879
880 /**
881 * The disposable to clear the cursor override.
882 */
883 readonly override: IDisposable;
884
885 /**
886 * The current local X position of the mouse.
887 */
888 localX: number;
889
890 /**
891 * The current local Y position of the mouse.
892 */
893 localY: number;
894
895 /**
896 * The timeout delay for the autoselect loop.
897 */
898 timeout: number;
899 };
900
901 /**
902 * A type alias for the resize handler press data.
903 */
904 export type PressData = RowResizeData | ColumnResizeData | SelectData;
905 }
906
907 /**
908 * The namespace for the module implementation details.
909 */
910 namespace Private {
911 /**
912 * Creates a CellConfig object from a hit region.
913 */
914 export function createCellConfigObject(
915 grid: DataGrid,
916 hit: DataGrid.HitTestResult
917 ): CellRenderer.CellConfig | undefined {
918 const { region, row, column } = hit;
919
920 // Terminate call if region is void.
921 if (region === 'void') {
922 return undefined;
923 }
924
925 // Augment hit region params with value and metadata.
926 const value = grid.dataModel!.data(region, row, column);
927 const metadata = grid.dataModel!.metadata(region, row, column);
928
929 // Create cell config object to retrieve cell renderer.
930 const config = {
931 ...hit,
932 value: value,
933 metadata: metadata
934 } as CellRenderer.CellConfig;
935
936 return config;
937 }
938
939 /**
940 * Get the resize handle for a grid hit test.
941 */
942 export function resizeHandleForHitTest(
943 hit: DataGrid.HitTestResult
944 ): ResizeHandle {
945 // Fetch the row and column.
946 let r = hit.row;
947 let c = hit.column;
948
949 // Fetch the leading and trailing sizes.
950 let lw = hit.x;
951 let lh = hit.y;
952 let tw = hit.width - hit.x;
953 let th = hit.height - hit.y;
954
955 // Set up the result variable.
956 let result: ResizeHandle;
957
958 // Dispatch based on hit test region.
959 switch (hit.region) {
960 case 'corner-header':
961 if (c > 0 && lw <= 5) {
962 result = 'left';
963 } else if (tw <= 6) {
964 result = 'right';
965 } else if (r > 0 && lh <= 5) {
966 result = 'top';
967 } else if (th <= 6) {
968 result = 'bottom';
969 } else {
970 result = 'none';
971 }
972 break;
973 case 'column-header':
974 if (c > 0 && lw <= 5) {
975 result = 'left';
976 } else if (tw <= 6) {
977 result = 'right';
978 } else if (r > 0 && lh <= 5) {
979 result = 'top';
980 } else if (th <= 6) {
981 result = 'bottom';
982 } else {
983 result = 'none';
984 }
985 break;
986 case 'row-header':
987 if (c > 0 && lw <= 5) {
988 result = 'left';
989 } else if (tw <= 6) {
990 result = 'right';
991 } else if (r > 0 && lh <= 5) {
992 result = 'top';
993 } else if (th <= 6) {
994 result = 'bottom';
995 } else {
996 result = 'none';
997 }
998 break;
999 case 'body':
1000 result = 'none';
1001 break;
1002 case 'void':
1003 result = 'none';
1004 break;
1005 default:
1006 throw 'unreachable';
1007 }
1008
1009 // Return the result.
1010 return result;
1011 }
1012
1013 /**
1014 * A timer callback for the autoselect loop.
1015 *
1016 * @param grid - The datagrid of interest.
1017 *
1018 * @param data - The select data of interest.
1019 */
1020 export function autoselect(grid: DataGrid, data: PressData.SelectData): void {
1021 // Bail early if the timeout has been reset.
1022 if (data.timeout < 0) {
1023 return;
1024 }
1025
1026 // Fetch the selection model.
1027 let model = grid.selectionModel;
1028
1029 // Bail early if the selection model has been removed.
1030 if (!model) {
1031 return;
1032 }
1033
1034 // Fetch the current selection.
1035 let cs = model.currentSelection();
1036
1037 // Bail early if there is no current selection.
1038 if (!cs) {
1039 return;
1040 }
1041
1042 // Fetch local X and Y coordinates of the mouse.
1043 let lx = data.localX;
1044 let ly = data.localY;
1045
1046 // Set up the selection variables.
1047 let r1 = cs.r1;
1048 let c1 = cs.c1;
1049 let r2 = cs.r2;
1050 let c2 = cs.c2;
1051 let cursorRow = model.cursorRow;
1052 let cursorColumn = model.cursorColumn;
1053 let clear: SelectionModel.ClearMode = 'current';
1054
1055 // Fetch the grid geometry.
1056 let hw = grid.headerWidth;
1057 let hh = grid.headerHeight;
1058 let vpw = grid.viewportWidth;
1059 let vph = grid.viewportHeight;
1060
1061 // Fetch the selection mode.
1062 let mode = model.selectionMode;
1063
1064 // Update the selection based on the hit region.
1065 if (data.region === 'row-header' || mode === 'row') {
1066 r2 += ly <= hh ? -1 : ly >= vph ? 1 : 0;
1067 } else if (data.region === 'column-header' || mode === 'column') {
1068 c2 += lx <= hw ? -1 : lx >= vpw ? 1 : 0;
1069 } else {
1070 r2 += ly <= hh ? -1 : ly >= vph ? 1 : 0;
1071 c2 += lx <= hw ? -1 : lx >= vpw ? 1 : 0;
1072 }
1073
1074 // Update the current selection.
1075 model.select({ r1, c1, r2, c2, cursorRow, cursorColumn, clear });
1076
1077 // Re-fetch the current selection.
1078 cs = model.currentSelection();
1079
1080 // Bail if there is no selection.
1081 if (!cs) {
1082 return;
1083 }
1084
1085 // Scroll the grid based on the hit region.
1086 if (data.region === 'row-header' || mode === 'row') {
1087 grid.scrollToRow(cs.r2);
1088 } else if (data.region === 'column-header' || mode == 'column') {
1089 grid.scrollToColumn(cs.c2);
1090 } else if (mode === 'cell') {
1091 grid.scrollToCell(cs.r2, cs.c2);
1092 }
1093
1094 // Schedule the next call with the current timeout.
1095 setTimeout(() => {
1096 autoselect(grid, data);
1097 }, data.timeout);
1098 }
1099
1100 /**
1101 * Compute the scroll timeout for the given delta distance.
1102 *
1103 * @param delta - The delta pixels from the origin.
1104 *
1105 * @returns The scaled timeout in milliseconds.
1106 */
1107 export function computeTimeout(delta: number): number {
1108 return 5 + 120 * (1 - Math.min(128, Math.abs(delta)) / 128);
1109 }
1110
1111 /**
1112 * A mapping of resize handle to cursor.
1113 */
1114 export const cursorMap = {
1115 top: 'ns-resize',
1116 left: 'ew-resize',
1117 right: 'ew-resize',
1118 bottom: 'ns-resize',
1119 hyperlink: 'pointer',
1120 none: 'default'
1121 };
1122 }