-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathpointerevent.js
103 lines (88 loc) · 2.57 KB
/
pointerevent.js
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
import {
INPUT_START,
INPUT_END,
INPUT_CANCEL,
INPUT_MOVE,
INPUT_TYPE_TOUCH,
INPUT_TYPE_MOUSE,
INPUT_TYPE_PEN,
INPUT_TYPE_KINECT
} from '../inputjs/input-consts';
import Input from '../inputjs/input-constructor';
import inArray from '../utils/in-array';
const POINTER_INPUT_MAP = {
pointerdown: INPUT_START,
pointermove: INPUT_MOVE,
pointerup: INPUT_END,
pointercancel: INPUT_CANCEL,
pointerout: INPUT_CANCEL
};
// in IE10 the pointer types is defined as an enum
const IE10_POINTER_TYPE_ENUM = {
2: INPUT_TYPE_TOUCH,
3: INPUT_TYPE_PEN,
4: INPUT_TYPE_MOUSE,
5: INPUT_TYPE_KINECT // see https://twitter.com/jacobrossi/status/480596438489890816
};
let POINTER_ELEMENT_EVENTS = 'pointerdown';
let POINTER_WINDOW_EVENTS = 'pointermove pointerup pointercancel';
// IE10 has prefixed support, and case-sensitive
if (window.MSPointerEvent && !window.PointerEvent) {
POINTER_ELEMENT_EVENTS = 'MSPointerDown';
POINTER_WINDOW_EVENTS = 'MSPointerMove MSPointerUp MSPointerCancel';
}
/**
* @private
* Pointer events input
* @constructor
* @extends Input
*/
export default class PointerEventInput extends Input {
constructor() {
super(...arguments);
this.evEl = POINTER_ELEMENT_EVENTS;
this.evWin = POINTER_WINDOW_EVENTS;
this.init();
this.store = (this.manager.session.pointerEvents = []);
}
/**
* @private
* handle mouse events
* @param {Object} ev
*/
handler(ev) {
let { store } = this;
let removePointer = false;
let eventTypeNormalized = ev.type.toLowerCase().replace('ms', '');
let eventType = POINTER_INPUT_MAP[eventTypeNormalized];
let pointerType = IE10_POINTER_TYPE_ENUM[ev.pointerType] || ev.pointerType;
let isTouch = (pointerType === INPUT_TYPE_TOUCH);
// get index of the event in the store
let storeIndex = inArray(store, ev.pointerId, 'pointerId');
// start and mouse must be down
if (eventType & INPUT_START && (ev.button === 0 || isTouch)) {
if (storeIndex < 0) {
store.push(ev);
storeIndex = store.length - 1;
}
} else if (eventType & (INPUT_END | INPUT_CANCEL)) {
removePointer = true;
}
// it not found, so the pointer hasn't been down (so it's probably a hover)
if (storeIndex < 0) {
return;
}
// update the event in the store
store[storeIndex] = ev;
this.callback(this.manager, eventType, {
pointers: store,
changedPointers: [ev],
pointerType,
srcEvent: ev
});
if (removePointer) {
// remove from the store
store.splice(storeIndex, 1);
}
}
}