forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouch-device.js
More file actions
83 lines (69 loc) · 2.68 KB
/
touch-device.js
File metadata and controls
83 lines (69 loc) · 2.68 KB
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
import { EventHandler } from '../core/event-handler.js';
import { TouchEvent } from './touch-event.js';
/**
* @class
* @name TouchDevice
* @augments EventHandler
* @classdesc Attach a TouchDevice to an element and it will receive and fire events when the element is touched.
* See also {@link Touch} and {@link TouchEvent}.
* @description Create a new touch device and attach it to an element.
* @param {Element} element - The element to attach listen for events on.
*/
class TouchDevice extends EventHandler {
constructor(element) {
super();
this._element = null;
this._startHandler = this._handleTouchStart.bind(this);
this._endHandler = this._handleTouchEnd.bind(this);
this._moveHandler = this._handleTouchMove.bind(this);
this._cancelHandler = this._handleTouchCancel.bind(this);
this.attach(element);
}
/**
* @function
* @name TouchDevice#attach
* @description Attach a device to an element in the DOM.
* If the device is already attached to an element this method will detach it first.
* @param {Element} element - The element to attach to.
*/
attach(element) {
if (this._element) {
this.detach();
}
this._element = element;
this._element.addEventListener('touchstart', this._startHandler, false);
this._element.addEventListener('touchend', this._endHandler, false);
this._element.addEventListener('touchmove', this._moveHandler, false);
this._element.addEventListener('touchcancel', this._cancelHandler, false);
}
/**
* @function
* @name TouchDevice#detach
* @description Detach a device from the element it is attached to.
*/
detach() {
if (this._element) {
this._element.removeEventListener('touchstart', this._startHandler, false);
this._element.removeEventListener('touchend', this._endHandler, false);
this._element.removeEventListener('touchmove', this._moveHandler, false);
this._element.removeEventListener('touchcancel', this._cancelHandler, false);
}
this._element = null;
}
_handleTouchStart(e) {
this.fire('touchstart', new TouchEvent(this, e));
}
_handleTouchEnd(e) {
this.fire('touchend', new TouchEvent(this, e));
}
_handleTouchMove(e) {
// call preventDefault to avoid issues in Chrome Android:
// http://wilsonpage.co.uk/touch-events-in-chrome-android/
e.preventDefault();
this.fire('touchmove', new TouchEvent(this, e));
}
_handleTouchCancel(e) {
this.fire('touchcancel', new TouchEvent(this, e));
}
}
export { TouchDevice };