forked from davoclavo/Mouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouse.js
225 lines (206 loc) · 6.16 KB
/
Mouse.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* mouse.js
*
* Provides a `Mouse` class, which in most cases there should only be a single of.
* `mouse` is automatically instantiated on the window and tracks the mouse state independent of specific elements
* This file also shims in missing behavior such as `buttons`
*/
void function(){
// figure out what we're checking for scroll offsets
var scroll = 'pageXOffset' in window
? { x: 'pageXOffset', y: 'pageYOffset' }
: { x: 'scrollX', y: 'scrollY' };
// strip 'mouse' from mouse events to avoid redundency
var translate = {
down: 'down',
up: 'up',
move: 'move',
over: 'over',
out: 'out',
grab: 'grab',
drag: 'drag',
drop: 'drop',
click: 'click',
wheel: 'wheel',
contextmenu: 'click',
dblclick: 'dblclick',
mousedown: 'down',
mouseup: 'up',
mousemove: 'move',
mouseover: 'over',
mouseout: 'out',
mousewheel: 'wheel',
};
var states = [], keybinds = Object.create(null);
void function(i, name){
while (i--) {
states[i] = Object.freeze({
left: (i & 1) > 0,
middle: (i & 2) > 0,
right: (i & 4) > 0
});
name = [];
states[i].left && name.push('left');
states[i].middle && name.push('middle');
states[i].right && name.push('right');
keybinds[name.join('+')] = i;
}
}(8);
function interpret(input){
if (input == null) return 0;
switch (typeof input) {
case 'number': return input < 8 ? input : 0;
case 'string': return input in keybinds ? keybinds[input] : 0;
case 'boolean': return +input;
case 'function':
case 'object': return (input.left ? 1 : 0) | (input.middle ? 2 : 0) | (input.right ? 4 : 0);
}
}
var allMouse = 'move over out down up wheel click dblclick grab drag drop';
function listen(view, handlers){
for (var k in handlers)
view.addEventListener(k, handlers[k], true);
}
function Mouse(view){
if (!(this instanceof Mouse))
return new Mouse(view);
var self = this;
var update = this.update.bind(this);
var count = 0;
this.view = view;
this.x = 0;
this.y = 0;
this.buttons = 0;
this.buttonStates = states[0];
this.active = false;
this.listeners = Object.create(null);
this.lastActivity = Date.now();
this.dragging = false;
function dispatch(e){
self.update(e);
self.emit(e);
}
var events = {
mousewheel: dispatch,
dblclick: dispatch,
mouseup: dispatch,
click: function click(e){
self.update(e);
if (self.dragging) {
self.lastType = 'drop';
self.emit(e);
self.lastType = 'click';
self.dragging = false;
}
self.emit(e);
// remove button's bit from mask
self.buttons &= ~(1 << e.button);
self.buttonStates = states[self.buttons % 8];
},
mouseover: function over(e){
if (e.relatedTarget === null) {
self.update(e);
self.emit(e);
}
},
mouseout: function out(e){
if (e.relatedTarget === null) {
self.update(e);
if (self.dragging) {
self.lastType = 'drop';
self.emit(e);
}
self.update(e);
self.emit(e);
}
},
mousedown: function down(e){
// add button's bit to mask
self.buttons |= 1 << e.button;
self.buttonStates = states[self.buttons % 8];
self.update(e);
self.emit(e);
},
contextmenu: function rightclick(e){
if (self.buttons !== 4 || self.dragging)
e.preventDefault();
events.click(e);
},
mousemove: function move(e){
if (self.dragging) {
self.update(e);
self.lastType = 'drag';
self.emit(e);
} else if (self.buttons && self.lastType === 'down') {
self.update(self.last);
self.dragging = true;
self.lastType = 'grab';
self.emit(self.last);
}
self.update(e);
self.emit(e);
}
};
listen(view, events);
}
function findHandler(buttons, handler){
if (!handler && typeof buttons === 'function')
handler = buttons, buttons = 'buttons' in handler ? handler.buttons : 0;
else
buttons = interpret(buttons);
handler.buttons = buttons;
return handler;
}
// Mouse re-implements the event handlers because it isn't a DOM element nor an EventTarget nor any tangible object
Mouse.prototype = {
constructor: Mouse,
emit: function emit(event){
var listeners = this.listeners[this.lastType];
if (listeners)
for (var i=0; i < listeners.length; i++)
if (!listeners[i].buttons || listeners[i].buttons === this.buttons)
listeners[i].call(this, event);
},
on: function on(types, buttons, handler){
types === '*' && (types = allMouse);
handler = findHandler(buttons, handler);
types.split(/\s+/).forEach(function(type){
type = translate[type];
this[type] || (this[type] = []);
this[type].push(handler);
}, this.listeners);
},
once: function once(types, buttons, handler){
handler = findHandler(buttons, handler);
this.on(types, handler.buttons, function single(e){
handler.call(this, e);
this.off(types, single);
});
},
off: function off(types, handler){
types === '*' && (types = allMouse);
types.split(/\s+/).forEach(function(type){
var listeners = this[translate[type]];
if (listeners) {
var index = listeners.indexOf(handler);
if (~index)
listeners.splice(index, 1);
}
}, this.listeners);
},
update: function update(e){
e.name = this.lastType = translate[e.type];
e.buttons = this.buttons;
e.buttonStates = this.buttonStates;
this.x = e.clientX;
this.y = e.clientY;
this.lastActivity = e.timeStamp;
this.last = e;
}
};
Object.keys(Mouse.prototype).forEach(function(key){
Object.defineProperty(Mouse.prototype, key, { enumerable: false });
});
if (typeof Window !== 'undefined')
Window.prototype.Mouse = Mouse;
window.mouse = new Mouse(window);
}();