-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
130 lines (119 loc) · 3.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* bind event
* @param target window | HTMLElement
* @param type event type
* @param handler event handler
* @param capture if capture phase
*/
export function on(target: Window | HTMLElement, type: string, handler: EventHandler, capture: boolean | AddEventListenerOptions = false) {
target.addEventListener(type, handler, capture)
}
/**
* unbind event
* @param target window | HTMLElement
* @param type event type
* @param handler event handler
* @param capture if capture phase
*/
export function off(target: Window | HTMLElement, type: string, handler: EventHandler, capture: boolean | AddEventListenerOptions = false) {
target.removeEventListener(type, handler, capture);
}
/**
* if position is out of visible screen
* @param x x coordinate
* @param y y coordinate
*/
export function withinBoundry(x: number, y: number) {
return x >= 0 && x <= window.innerWidth && y >= 0 && y <= window.innerHeight;
}
/**
* To bind event
* @param el taget element. required.
* @param options event handlers and other configration. required.
*/
export default function XTouch(el: Window | HTMLElement, options: IOptions) {
if (Object.prototype.toString.call(options) !== '[object Object]') {
throw new Error('[xtouch]: argument `options` is missing or illegal.')
}
const { onStart, onMove, onEnd, capture } = options;
let startTarget: EventTarget = null;
const _onStart: EventHandler = function (e) {
if (e.type === 'mousedown') {
startTarget = e.target;
// @ts-ignore
e.identifier = 0;
// @ts-ignore
e.touches = e.changedTouches = [e];
// @ts-ignore
e.targetTouches = [e];
}
onStart(e);
}, _onMove: EventHandler = function (e) {
if (e.type === 'mousemove') {
// @ts-ignore
e.identifier = 0;
// @ts-ignore
e.touches = e.changedTouches = [e];
// @ts-ignore
e.targetTouches = e.target === startTarget ? [e] : [];
}
onMove(e);
}, _onEnd: EventHandler = function (e) {
if (e.type === 'mouseup') {
// @ts-ignore
e.identifier = 0;
// @ts-ignore
e.touches = [];
// @ts-ignore
e.changedTouches = [e];
// @ts-ignore
e.targetTouches = e.target === startTarget ? [e] : [];
}
onEnd(e);
};
if (onStart) {
on(el, 'touchstart', _onStart, capture);
on(el, 'mousedown', _onStart, capture);
}
if (onMove) {
on(window, 'touchmove', _onMove, capture);
on(window, 'mousemove', _onMove, capture);
}
if (onEnd) {
on(window, 'touchend', _onEnd, capture);
on(window, 'mouseup', _onEnd, capture);
}
return function unbind() {
off(el, 'touchstart', _onStart, capture);
off(window, 'touchmove', _onMove, capture);
off(window, 'touchend', _onEnd, capture);
off(el, 'mousedown', _onStart, capture);
off(window, 'mousemove', _onMove, capture);
off(window, 'mouseup', _onEnd, capture);
};
}
/**
* configration options
*/
export interface IOptions {
/**
* the third arg for `addEventListenner`
*/
capture?: boolean | AddEventListenerOptions
/**
* touchstart/mousedown event handler
*/
onStart: EventHandler;
/**
* touchmove/mousemove event handler
*/
onMove: EventHandler;
/**
* touchend/mouseup event handler
*/
onEnd: EventHandler;
}
/**
* event callback.
*/
export type EventHandler = (e: TouchEvent) => void;