This repository was archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathmouse.js
More file actions
155 lines (143 loc) · 4.35 KB
/
mouse.js
File metadata and controls
155 lines (143 loc) · 4.35 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import dispatcher from './dispatcher';
var pointermap = dispatcher.pointermap;
// radius around touchend that swallows mouse events
var DEDUP_DIST = 25;
// left, middle, right, back, forward
var BUTTON_TO_BUTTONS = [1, 4, 2, 8, 16];
var HAS_BUTTONS = false;
try {
HAS_BUTTONS = new MouseEvent('test', { buttons: 1 }).buttons === 1;
} catch (e) {}
// handler block for native mouse events
var mouseEvents = {
POINTER_ID: 1,
POINTER_TYPE: 'mouse',
events: [
'mousedown',
'mousemove',
'mouseup',
'mouseover',
'mouseout'
],
register: function(target) {
dispatcher.listen(target, this.events);
},
unregister: function(target) {
dispatcher.unlisten(target, this.events);
},
lastTouches: [],
// collide with the global mouse listener
isEventSimulatedFromTouch: function(inEvent) {
var lts = this.lastTouches;
var x = inEvent.clientX;
var y = inEvent.clientY;
for (var i = 0, l = lts.length, t; i < l && (t = lts[i]); i++) {
// simulated mouse events will be swallowed near a primary touchend
var dx = Math.abs(x - t.x);
var dy = Math.abs(y - t.y);
if (dx <= DEDUP_DIST && dy <= DEDUP_DIST) {
return true;
}
}
},
prepareEvent: function(inEvent) {
var e = dispatcher.cloneEvent(inEvent);
// forward mouse preventDefault
var pd = e.preventDefault;
e.preventDefault = function() {
inEvent.preventDefault();
pd();
};
e.pointerId = this.POINTER_ID;
e.isPrimary = true;
e.pointerType = this.POINTER_TYPE;
return e;
},
prepareButtonsForMove: function(e, inEvent) {
var p = pointermap.get(this.POINTER_ID);
// Update buttons state after possible out-of-document mouseup.
if (inEvent.which === 0 || !p) {
e.buttons = 0;
} else {
e.buttons = p.buttons;
}
inEvent.buttons = e.buttons;
},
mousedown: function(inEvent) {
if (!this.isEventSimulatedFromTouch(inEvent)) {
var p = pointermap.get(this.POINTER_ID);
var e = this.prepareEvent(inEvent);
if (!HAS_BUTTONS) {
e.buttons = BUTTON_TO_BUTTONS[e.button];
if (p) { e.buttons |= p.buttons; }
inEvent.buttons = e.buttons;
}
pointermap.set(this.POINTER_ID, inEvent);
if (!p || p.buttons === 0) {
dispatcher.down(e);
} else {
dispatcher.move(e);
}
}
},
mousemove: function(inEvent) {
if (!this.isEventSimulatedFromTouch(inEvent)) {
var e = this.prepareEvent(inEvent);
if (!HAS_BUTTONS) { this.prepareButtonsForMove(e, inEvent); }
e.button = -1;
pointermap.set(this.POINTER_ID, inEvent);
dispatcher.move(e);
}
},
mouseup: function(inEvent) {
if (!this.isEventSimulatedFromTouch(inEvent)) {
var p = pointermap.get(this.POINTER_ID);
var e = this.prepareEvent(inEvent);
if (!HAS_BUTTONS) {
var up = BUTTON_TO_BUTTONS[e.button];
// Produces wrong state of buttons in Browsers without `buttons` support
// when a mouse button that was pressed outside the document is released
// inside and other buttons are still pressed down.
e.buttons = p ? p.buttons & ~up : 0;
inEvent.buttons = e.buttons;
}
pointermap.set(this.POINTER_ID, inEvent);
// Support: Firefox <=44 only
// FF Ubuntu includes the lifted button in the `buttons` property on
// mouseup.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1223366
e.buttons &= ~BUTTON_TO_BUTTONS[e.button];
if (e.buttons === 0) {
dispatcher.up(e);
} else {
dispatcher.move(e);
}
}
},
mouseover: function(inEvent) {
if (!this.isEventSimulatedFromTouch(inEvent)) {
var e = this.prepareEvent(inEvent);
if (!HAS_BUTTONS) { this.prepareButtonsForMove(e, inEvent); }
e.button = -1;
pointermap.set(this.POINTER_ID, inEvent);
dispatcher.enterOver(e);
}
},
mouseout: function(inEvent) {
if (!this.isEventSimulatedFromTouch(inEvent)) {
var e = this.prepareEvent(inEvent);
if (!HAS_BUTTONS) { this.prepareButtonsForMove(e, inEvent); }
e.button = -1;
dispatcher.leaveOut(e);
}
},
cancel: function(inEvent) {
var e = this.prepareEvent(inEvent);
dispatcher.cancel(e);
this.deactivateMouse();
},
deactivateMouse: function() {
pointermap.delete(this.POINTER_ID);
}
};
export default mouseEvents;