-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmousehelper.js
67 lines (57 loc) · 1.92 KB
/
mousehelper.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
MineSweeper.MouseHelper = function($scope) {
var leftButtonDown = false;
var middleButtonDown = false;
var rightButtonDown = false;
var callbacks = {};
var isFirefox = typeof InstallTrigger !== 'undefined';
$scope.on("mouseup", function(event) {
runAsyncCallbacksForEvent(event);
if (event.which === 1) {
leftButtonDown = false;
} else if (event.which === 2) {
middleButtonDown = false;
} else if (event.which === 3) {
rightButtonDown = false;
}
}).on("mousedown", function(event) {
if (event.which === 1) {
leftButtonDown = true;
} else if (event.which === 2) {
middleButtonDown = true;
if (isFirefox) event.preventDefault(); // Workaround for Firefox autoscrolling
} else if (event.which === 3) {
rightButtonDown = true;
}
runAsyncCallbacksForEvent(event);
});
var addAsyncCallbackForEvent = function(event, callback) {
callbacks[event.timeStamp] = callback.bind(event.currentTarget, event);
}
var runAsyncCallbacksForEvent = function(event) {
for (timeStamp in callbacks) {
if (timeStamp<=event.timeStamp) {
callbacks[timeStamp]();
delete callbacks[timeStamp];
}
}
}
var isLeftButtonDown = function() {
return leftButtonDown;
}
var isRightButtonDown = function() {
return rightButtonDown;
}
var isMiddleButtonDown = function() {
return middleButtonDown;
}
var areLeftAndRightButtonDown = function() {
return isLeftButtonDown() && isRightButtonDown();
}
return {
leftDown: isLeftButtonDown,
rightDown: isRightButtonDown,
middleDown: isMiddleButtonDown,
leftAndRightDown: areLeftAndRightButtonDown,
waitFor: addAsyncCallbackForEvent
}
}