Skip to content

Commit

Permalink
Merge pull request #1187 from visualjerk/task/1186-support-touch-event
Browse files Browse the repository at this point in the history
#1186@minor: Add support for TouchEvent.
  • Loading branch information
capricorn86 committed Jan 29, 2024
2 parents b8bd6e1 + 0eb1fd8 commit dc4bdc4
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 75 deletions.
16 changes: 16 additions & 0 deletions packages/happy-dom/src/event/ITouchInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import IEventTarget from './IEventTarget.js';

export default interface ITouchInit {
identifier: number;
target: IEventTarget;
clientX?: number;
clientY?: number;
screenX?: number;
screenY?: number;
pageX?: number;
pageY?: number;
radiusX?: number;
radiusY?: number;
rotationAngle?: number;
force?: number;
}
40 changes: 40 additions & 0 deletions packages/happy-dom/src/event/Touch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import IEventTarget from './IEventTarget.js';
import ITouchInit from './ITouchInit.js';

/**
*
*/
export default class Touch {
public readonly identifier: number;
public readonly target: IEventTarget;
public readonly clientX: number;
public readonly clientY: number;
public readonly screenX: number;
public readonly screenY: number;
public readonly pageX: number;
public readonly pageY: number;
public readonly radiusX: number;
public readonly radiusY: number;
public readonly rotationAngle: number;
public readonly force: number;

/**
* Constructor.
*
* @param [touchInit] Touch init.
*/
constructor(touchInit: ITouchInit) {
this.identifier = touchInit.identifier;
this.target = touchInit.target;
this.clientX = touchInit.clientX ?? 0;
this.clientY = touchInit.clientY ?? 0;
this.screenX = touchInit.screenX ?? 0;
this.screenY = touchInit.screenY ?? 0;
this.pageX = touchInit.pageX ?? 0;
this.pageY = touchInit.pageY ?? 0;
this.radiusX = touchInit.radiusX ?? 0;
this.radiusY = touchInit.radiusY ?? 0;
this.rotationAngle = touchInit.rotationAngle ?? 0;
this.force = touchInit.force ?? 0;
}
}
12 changes: 12 additions & 0 deletions packages/happy-dom/src/event/events/ITouchEventInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import IUIEventInit from '../IUIEventInit.js';
import Touch from '../Touch.js';

export default interface ITouchEventInit extends IUIEventInit {
touches?: Touch[] | null;
targetTouches?: Touch[] | null;
changedTouches?: Touch[] | null;
ctrlKey?: boolean | null;
shiftKey?: boolean | null;
altKey?: boolean | null;
metaKey?: boolean | null;
}
34 changes: 34 additions & 0 deletions packages/happy-dom/src/event/events/TouchEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ITouchEventInit from './ITouchEventInit.js';
import UIEvent from '../UIEvent.js';
import Touch from '../Touch.js';

/**
*
*/
export default class TouchEvent extends UIEvent {
public readonly altKey: boolean;
public readonly changedTouches: Touch[];
public readonly ctrlKey: boolean;
public readonly metaKey: boolean;
public readonly shiftKey: boolean;
public readonly targetTouches: Touch[];
public readonly touches: Touch[];

/**
* Constructor.
*
* @param type Event type.
* @param [eventInit] Event init.
*/
constructor(type: string, eventInit: ITouchEventInit | null = null) {
super(type, eventInit);

this.altKey = eventInit?.altKey ?? false;
this.changedTouches = eventInit?.changedTouches ?? [];
this.ctrlKey = eventInit?.ctrlKey ?? false;
this.metaKey = eventInit?.metaKey ?? false;
this.shiftKey = eventInit?.shiftKey ?? false;
this.targetTouches = eventInit?.targetTouches ?? [];
this.touches = eventInit?.touches ?? [];
}
}
Loading

0 comments on commit dc4bdc4

Please sign in to comment.