Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): SlickEventHandler should infer handler args Types #1261

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/vite-demo-vanilla-bundle/src/examples/example07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()))) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/vite-demo-vanilla-bundle/src/examples/example19.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 = '';

Expand Down
14 changes: 7 additions & 7 deletions packages/common/src/core/slickCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,21 +201,21 @@ export class SlickEvent<ArgType = any> {
}
}

export class SlickEventHandler<ArgType = any> {
protected handlers: Array<{ event: SlickEvent; handler: Handler<ArgType>; }> = [];
export class SlickEventHandler {
protected handlers: Array<{ event: SlickEvent; handler: Handler<any>; }> = [];

get subscriberCount() {
return this.handlers.length;
}

subscribe(event: SlickEvent, handler: Handler<ArgType>) {
subscribe<T = any>(event: SlickEvent<T>, handler: Handler<T>) {
this.handlers.push({ event, handler });
event.subscribe(handler);

return this; // allow chaining
return this as SlickEventHandler; // allow chaining
}

unsubscribe(event: SlickEvent, handler: Handler<ArgType>) {
unsubscribe<T = any>(event: SlickEvent, handler: Handler<T>) {
let i = this.handlers.length;
while (i--) {
if (this.handlers[i].event === event &&
Expand All @@ -226,7 +226,7 @@ export class SlickEventHandler<ArgType = any> {
}
}

return this; // allow chaining
return this as SlickEventHandler; // allow chaining
}

unsubscribeAll() {
Expand All @@ -236,7 +236,7 @@ export class SlickEventHandler<ArgType = any> {
}
this.handlers = [];

return this; // allow chaining
return this as SlickEventHandler; // allow chaining
}
}

Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/extensions/slickCellRangeSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
DOMMouseOrTouchEvent,
DragPosition,
DragRange,
DragRowMove,
GridOption,
MouseOffsetViewport,
OnScrollEventArgs,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -348,7 +349,7 @@ export class SlickCellRangeSelector {
}
}

protected handleDragStart(e: DOMMouseOrTouchEvent<HTMLDivElement>, dd: DragPosition) {
protected handleDragStart(e: DOMMouseOrTouchEvent<HTMLDivElement>, 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;
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/extensions/slickCheckboxSelectColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -383,7 +383,7 @@ export class SlickCheckboxSelectColumn<T = any> {
}
}

protected handleHeaderClick(e: DOMMouseOrTouchEvent<HTMLInputElement>, args: { column: Column; node: HTMLDivElement; grid: SlickGrid; }) {
protected handleHeaderClick(e: DOMMouseOrTouchEvent<HTMLInputElement>, args: OnHeaderClickEventArgs) {
if (args.column.id === this._addonOptions.columnId && e.target.type === 'checkbox') {
e.target.ariaChecked = String(e.target.checked);

Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/extensions/slickRowMoveManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/interfaces/drag.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,5 @@ export interface DragRowMove {
selectedRows: number[];
startX: number;
startY: number;
range: DragRange;
}
6 changes: 3 additions & 3 deletions packages/common/src/interfaces/rowMoveManager.interface.ts
Original file line number Diff line number Diff line change
@@ -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 {
//
Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions packages/common/src/services/gridEvent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down