From a33129b0ce1443443e7dcebb3562ffd538b6a731 Mon Sep 17 00:00:00 2001 From: Ghislain B Date: Sun, 10 Dec 2023 01:20:08 -0500 Subject: [PATCH] fix(core): SlickEventHandler handler args should have Types (#1261) --- .../src/examples/example07.ts | 5 ++--- .../src/examples/example19.ts | 4 ++-- packages/common/src/core/slickCore.ts | 14 +++++++------- .../src/extensions/slickCellRangeSelector.ts | 7 ++++--- .../src/extensions/slickCheckboxSelectColumn.ts | 4 ++-- .../common/src/extensions/slickRowMoveManager.ts | 4 ++-- packages/common/src/interfaces/drag.interface.ts | 1 + .../src/interfaces/rowMoveManager.interface.ts | 6 +++--- packages/common/src/services/gridEvent.service.ts | 4 ++-- 9 files changed, 25 insertions(+), 24 deletions(-) diff --git a/examples/vite-demo-vanilla-bundle/src/examples/example07.ts b/examples/vite-demo-vanilla-bundle/src/examples/example07.ts index b5414a85c..14a877935 100644 --- a/examples/vite-demo-vanilla-bundle/src/examples/example07.ts +++ b/examples/vite-demo-vanilla-bundle/src/examples/example07.ts @@ -6,7 +6,6 @@ import { Formatters, type GridOption, OperatorType, - SlickEventData, } from '@slickgrid-universal/common'; import { BindingEventService } from '@slickgrid-universal/binding'; import { ExcelExportService } from '@slickgrid-universal/excel-export'; @@ -488,7 +487,7 @@ export default class Example07 { return collection.sort((item1, item2) => item1.value - item2.value); } - onBeforeMoveRow(e: MouseEvent | TouchEvent | SlickEventData, data: { rows: number[]; insertBefore: number; }) { + onBeforeMoveRow(e: MouseEvent | TouchEvent, data: { rows: number[]; insertBefore: number; }) { for (const rowIdx of data.rows) { // no point in moving before or after itself if (rowIdx === data.insertBefore || (rowIdx === data.insertBefore - 1 && ((data.insertBefore - 1) !== this.sgb.dataView?.getItemCount()))) { @@ -499,7 +498,7 @@ export default class Example07 { return true; } - onMoveRows(_e: MouseEvent | TouchEvent | SlickEventData, args: { rows: number[]; insertBefore: number; }) { + onMoveRows(_e: MouseEvent | TouchEvent, args: { rows: number[]; insertBefore: number; }) { // rows and insertBefore references, // note that these references are assuming that the dataset isn't filtered at all // which is not always the case so we will recalcualte them and we won't use these reference afterward diff --git a/examples/vite-demo-vanilla-bundle/src/examples/example19.ts b/examples/vite-demo-vanilla-bundle/src/examples/example19.ts index dc53b5476..e0a16cf6a 100644 --- a/examples/vite-demo-vanilla-bundle/src/examples/example19.ts +++ b/examples/vite-demo-vanilla-bundle/src/examples/example19.ts @@ -1,4 +1,4 @@ -import { type Column, type GridOption, SlickEventHandler, type SlickRange } from '@slickgrid-universal/common'; +import { type Column, type GridOption, SlickEventHandler } from '@slickgrid-universal/common'; import { Slicker, SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle'; import { ExampleGridOptions } from './example-grid-options'; import './example19.scss'; @@ -28,7 +28,7 @@ export default class Example19 { // bind any of the grid events const cellSelectionModel = this.sgb.slickGrid!.getSelectionModel(); - this._eventHandler.subscribe(cellSelectionModel!.onSelectedRangesChanged, (_e, args: SlickRange[]) => { + this._eventHandler.subscribe(cellSelectionModel!.onSelectedRangesChanged, (_e, args) => { const targetRange = document.querySelector('#selectionRange') as HTMLSpanElement; targetRange.textContent = ''; diff --git a/packages/common/src/core/slickCore.ts b/packages/common/src/core/slickCore.ts index d2f5b11e5..3b68fc8ba 100644 --- a/packages/common/src/core/slickCore.ts +++ b/packages/common/src/core/slickCore.ts @@ -201,21 +201,21 @@ export class SlickEvent { } } -export class SlickEventHandler { - protected handlers: Array<{ event: SlickEvent; handler: Handler; }> = []; +export class SlickEventHandler { + protected handlers: Array<{ event: SlickEvent; handler: Handler; }> = []; get subscriberCount() { return this.handlers.length; } - subscribe(event: SlickEvent, handler: Handler) { + subscribe(event: SlickEvent, handler: Handler) { this.handlers.push({ event, handler }); event.subscribe(handler); - return this; // allow chaining + return this as SlickEventHandler; // allow chaining } - unsubscribe(event: SlickEvent, handler: Handler) { + unsubscribe(event: SlickEvent, handler: Handler) { let i = this.handlers.length; while (i--) { if (this.handlers[i].event === event && @@ -226,7 +226,7 @@ export class SlickEventHandler { } } - return this; // allow chaining + return this as SlickEventHandler; // allow chaining } unsubscribeAll() { @@ -236,7 +236,7 @@ export class SlickEventHandler { } this.handlers = []; - return this; // allow chaining + return this as SlickEventHandler; // allow chaining } } diff --git a/packages/common/src/extensions/slickCellRangeSelector.ts b/packages/common/src/extensions/slickCellRangeSelector.ts index 4fb0e527d..f7fa3dbf4 100644 --- a/packages/common/src/extensions/slickCellRangeSelector.ts +++ b/packages/common/src/extensions/slickCellRangeSelector.ts @@ -5,6 +5,7 @@ import type { DOMMouseOrTouchEvent, DragPosition, DragRange, + DragRowMove, GridOption, MouseOffsetViewport, OnScrollEventArgs, @@ -170,7 +171,7 @@ export class SlickCellRangeSelector { // protected functions // --------------------- - protected handleDrag(evt: SlickEventData, dd: DragPosition) { + protected handleDrag(evt: SlickEventData, dd: DragRowMove) { if (!this._dragging && !this._gridOptions.enableRowMoveManager) { return; } @@ -296,7 +297,7 @@ export class SlickCellRangeSelector { } } - protected handleDragEnd(e: any, dd: DragPosition) { + protected handleDragEnd(e: any, dd: DragRowMove) { this._decorator.hide(); if (this._dragging) { @@ -348,7 +349,7 @@ export class SlickCellRangeSelector { } } - protected handleDragStart(e: DOMMouseOrTouchEvent, dd: DragPosition) { + protected handleDragStart(e: DOMMouseOrTouchEvent, dd: DragRowMove) { const cellObj = this._grid.getCellFromEvent(e); if (cellObj && this.onBeforeCellRangeSelected.notify(cellObj).getReturnValue() !== false && this._grid.canCellBeSelected(cellObj.row, cellObj.cell)) { this._dragging = true; diff --git a/packages/common/src/extensions/slickCheckboxSelectColumn.ts b/packages/common/src/extensions/slickCheckboxSelectColumn.ts index fc54757ff..2374d8ee8 100644 --- a/packages/common/src/extensions/slickCheckboxSelectColumn.ts +++ b/packages/common/src/extensions/slickCheckboxSelectColumn.ts @@ -2,7 +2,7 @@ import { BindingEventService } from '@slickgrid-universal/binding'; import type { BasePubSubService } from '@slickgrid-universal/event-pub-sub'; import { type SlickDataView, SlickEventHandler, type SlickGrid } from '../core/index'; -import type { CheckboxSelectorOption, Column, DOMMouseOrTouchEvent, GridOption, SelectableOverrideCallback } from '../interfaces/index'; +import type { CheckboxSelectorOption, Column, DOMMouseOrTouchEvent, GridOption, OnHeaderClickEventArgs, SelectableOverrideCallback } from '../interfaces/index'; import { SlickRowSelectionModel } from './slickRowSelectionModel'; import { createDomElement, emptyElement } from '../services/domUtilities'; import { SelectionModel } from '../enums/index'; @@ -383,7 +383,7 @@ export class SlickCheckboxSelectColumn { } } - protected handleHeaderClick(e: DOMMouseOrTouchEvent, args: { column: Column; node: HTMLDivElement; grid: SlickGrid; }) { + protected handleHeaderClick(e: DOMMouseOrTouchEvent, args: OnHeaderClickEventArgs) { if (args.column.id === this._addonOptions.columnId && e.target.type === 'checkbox') { e.target.ariaChecked = String(e.target.checked); diff --git a/packages/common/src/extensions/slickRowMoveManager.ts b/packages/common/src/extensions/slickRowMoveManager.ts index c42d8b6bd..b081ecb14 100644 --- a/packages/common/src/extensions/slickRowMoveManager.ts +++ b/packages/common/src/extensions/slickRowMoveManager.ts @@ -157,12 +157,12 @@ export class SlickRowMoveManager { // protected functions // ------------------ - protected handleDragInit(e: SlickEventData) { + protected handleDragInit(e: MouseEvent) { // prevent the grid from cancelling drag'n'drop by default e.stopImmediatePropagation(); } - protected handleDragEnd(e: SlickEventData, dd: DragRowMove) { + protected handleDragEnd(e: MouseEvent, dd: DragRowMove) { if (!this._dragging) { return; } diff --git a/packages/common/src/interfaces/drag.interface.ts b/packages/common/src/interfaces/drag.interface.ts index 6132bc9c3..606d3c640 100644 --- a/packages/common/src/interfaces/drag.interface.ts +++ b/packages/common/src/interfaces/drag.interface.ts @@ -49,4 +49,5 @@ export interface DragRowMove { selectedRows: number[]; startX: number; startY: number; + range: DragRange; } \ No newline at end of file diff --git a/packages/common/src/interfaces/rowMoveManager.interface.ts b/packages/common/src/interfaces/rowMoveManager.interface.ts index 3d06e7e60..ce610c7f6 100644 --- a/packages/common/src/interfaces/rowMoveManager.interface.ts +++ b/packages/common/src/interfaces/rowMoveManager.interface.ts @@ -1,6 +1,6 @@ import type { RowMoveManagerOption } from './index'; import type { SlickRowMoveManager } from '../extensions/slickRowMoveManager'; -import type { SlickEventData, SlickGrid } from '../core/index'; +import type { SlickGrid } from '../core/index'; export interface RowMoveManager extends RowMoveManagerOption { // @@ -10,8 +10,8 @@ export interface RowMoveManager extends RowMoveManagerOption { onExtensionRegistered?: (plugin: SlickRowMoveManager) => void; /** SlickGrid Event fired before the row is moved. */ - onBeforeMoveRows?: (e: MouseEvent | TouchEvent | SlickEventData, args: { grid: SlickGrid; rows: number[]; insertBefore: number; }) => boolean | void; + onBeforeMoveRows?: (e: MouseEvent | TouchEvent, args: { grid: SlickGrid; rows: number[]; insertBefore: number; }) => boolean | void; /** SlickGrid Event fired while the row is moved. */ - onMoveRows?: (e: SlickEventData, args: { grid: SlickGrid; rows: number[]; insertBefore: number; }) => void; + onMoveRows?: (e: MouseEvent | TouchEvent, args: { grid: SlickGrid; rows: number[]; insertBefore: number; }) => void; } diff --git a/packages/common/src/services/gridEvent.service.ts b/packages/common/src/services/gridEvent.service.ts index 6e36ac5cf..1c5bc7238 100644 --- a/packages/common/src/services/gridEvent.service.ts +++ b/packages/common/src/services/gridEvent.service.ts @@ -31,12 +31,12 @@ export class GridEventService { if (typeof column.onBeforeEditCell === 'function') { // add to the output gridOptions & dataView since we'll need them inside the AJAX column.onBeforeEditCell const returnedArgs: OnEventArgs = { - row: args.row, + row: args.row!, cell: args.cell, dataView, grid, columnDef: column, - dataContext: grid.getDataItem(args.row) + dataContext: grid.getDataItem(args.row!) }; // finally call up the Slick column.onBeforeEditCells.... function