Skip to content

Commit dc4bdc4

Browse files
authored
Merge pull request #1187 from visualjerk/task/1186-support-touch-event
#1186@minor: Add support for TouchEvent.
2 parents b8bd6e1 + 0eb1fd8 commit dc4bdc4

File tree

8 files changed

+241
-75
lines changed

8 files changed

+241
-75
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import IEventTarget from './IEventTarget.js';
2+
3+
export default interface ITouchInit {
4+
identifier: number;
5+
target: IEventTarget;
6+
clientX?: number;
7+
clientY?: number;
8+
screenX?: number;
9+
screenY?: number;
10+
pageX?: number;
11+
pageY?: number;
12+
radiusX?: number;
13+
radiusY?: number;
14+
rotationAngle?: number;
15+
force?: number;
16+
}

packages/happy-dom/src/event/Touch.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import IEventTarget from './IEventTarget.js';
2+
import ITouchInit from './ITouchInit.js';
3+
4+
/**
5+
*
6+
*/
7+
export default class Touch {
8+
public readonly identifier: number;
9+
public readonly target: IEventTarget;
10+
public readonly clientX: number;
11+
public readonly clientY: number;
12+
public readonly screenX: number;
13+
public readonly screenY: number;
14+
public readonly pageX: number;
15+
public readonly pageY: number;
16+
public readonly radiusX: number;
17+
public readonly radiusY: number;
18+
public readonly rotationAngle: number;
19+
public readonly force: number;
20+
21+
/**
22+
* Constructor.
23+
*
24+
* @param [touchInit] Touch init.
25+
*/
26+
constructor(touchInit: ITouchInit) {
27+
this.identifier = touchInit.identifier;
28+
this.target = touchInit.target;
29+
this.clientX = touchInit.clientX ?? 0;
30+
this.clientY = touchInit.clientY ?? 0;
31+
this.screenX = touchInit.screenX ?? 0;
32+
this.screenY = touchInit.screenY ?? 0;
33+
this.pageX = touchInit.pageX ?? 0;
34+
this.pageY = touchInit.pageY ?? 0;
35+
this.radiusX = touchInit.radiusX ?? 0;
36+
this.radiusY = touchInit.radiusY ?? 0;
37+
this.rotationAngle = touchInit.rotationAngle ?? 0;
38+
this.force = touchInit.force ?? 0;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import IUIEventInit from '../IUIEventInit.js';
2+
import Touch from '../Touch.js';
3+
4+
export default interface ITouchEventInit extends IUIEventInit {
5+
touches?: Touch[] | null;
6+
targetTouches?: Touch[] | null;
7+
changedTouches?: Touch[] | null;
8+
ctrlKey?: boolean | null;
9+
shiftKey?: boolean | null;
10+
altKey?: boolean | null;
11+
metaKey?: boolean | null;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import ITouchEventInit from './ITouchEventInit.js';
2+
import UIEvent from '../UIEvent.js';
3+
import Touch from '../Touch.js';
4+
5+
/**
6+
*
7+
*/
8+
export default class TouchEvent extends UIEvent {
9+
public readonly altKey: boolean;
10+
public readonly changedTouches: Touch[];
11+
public readonly ctrlKey: boolean;
12+
public readonly metaKey: boolean;
13+
public readonly shiftKey: boolean;
14+
public readonly targetTouches: Touch[];
15+
public readonly touches: Touch[];
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param type Event type.
21+
* @param [eventInit] Event init.
22+
*/
23+
constructor(type: string, eventInit: ITouchEventInit | null = null) {
24+
super(type, eventInit);
25+
26+
this.altKey = eventInit?.altKey ?? false;
27+
this.changedTouches = eventInit?.changedTouches ?? [];
28+
this.ctrlKey = eventInit?.ctrlKey ?? false;
29+
this.metaKey = eventInit?.metaKey ?? false;
30+
this.shiftKey = eventInit?.shiftKey ?? false;
31+
this.targetTouches = eventInit?.targetTouches ?? [];
32+
this.touches = eventInit?.touches ?? [];
33+
}
34+
}

0 commit comments

Comments
 (0)