/
mouse.js
219 lines (177 loc) · 6.1 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
(function(oCanvas, window, document, undefined){
// Define the class
var mouse = function () {
// Return an object when instantiated
return {
x: 0,
y: 0,
buttonState: "up",
canvasFocused: false,
canvasHovered: false,
cursorValue: "default",
init: function () {
this.core.events.addEventTypes("mouse", {
move: "mousemove",
enter: "mouseenter",
leave: "mouseleave",
down: "mousedown",
up: "mouseup",
singleClick: "click",
doubleClick: "dblclick"
});
this.types = {
"mousemove": "move",
"mousedown": "down",
"mouseup": "up",
"dblclick": "doubleClick"
};
this.core.pointer = this;
// Only bind events for mouse if touch is not available
// This is to enable developers to bind to both touch and mouse,
// but still only trigger handlers once (for the right input device)
if (!this.core.touch || !this.core.touch.isTouch) {
this.bindHandlers();
}
},
bindHandlers: function () {
var self, canvasElement, type;
self = this;
canvasElement = this.core.canvasElement;
for (type in this.types) {
// Add event listeners to the canvas element
canvasElement.addEventListener(type, function (e) {
self.canvasHandler(e);
}, false);
// Add event listeners to the document (used for setting states and trigger mouseup events)
if (type === "mousemove") {
type = "mouseover";
}
document.addEventListener(type, function (e) {
self.docHandler(e);
}, false);
if (window.parent !== window) {
window.parent.document.addEventListener(type, function (e) {
self.docHandler(e);
}, false);
}
}
},
canvasHandler: function (e, fromDoc) {
var events, onCanvas, type, frontObject;
events = this.core.events;
onCanvas = this.onCanvas(e);
// Trigger only mouseup events if pointer is outside the canvas
if (e.type === "mouseup" && !onCanvas && !this.canvasUpEventTriggered) {
events.triggerPointerEvent(this.types["mouseup"], events.frontObject, "mouse", e);
events.triggerPointerEvent(this.types["mouseup"], this.core.canvasElement, "mouse", e);
this.canvasUpEventTriggered = true;
return;
}
// Abort the handler if the pointer started inside the canvas and is now outside
if (!fromDoc && !onCanvas) {
return;
}
type = (fromDoc && e.type === "mouseover") ? "mousemove" : e.type;
if (!fromDoc) {
this.canvasHovered = true;
}
if (type === "mousedown") {
this.canvasUpEventTriggered = false;
this.canvasFocused = true;
this.buttonState = "down";
}
if (type === "mouseup") {
this.buttonState = "up";
}
// Get the front object for pointer position, among all added objects
frontObject = (fromDoc || !onCanvas) ? undefined : events.getFrontObject("mouse");
// Trigger events
if (fromDoc && events.frontObject) {
events.triggerChain(events.getParentChain(events.frontObject, true, true), ["mouseleave"]);
events.frontObject = null;
} else if (fromDoc) {
events.triggerHandlers(this.core.canvasElement, ["mouseleave"]);
} else {
events.triggerPointerEvent(this.types[type], frontObject, "mouse", e);
}
},
docHandler: function (e) {
var onCanvas = this.onCanvas(e);
if (!onCanvas) {
if (this.core.canvasElement.events.hasEntered) {
if (e.type === "mouseover") {
this.canvasHandler(e, true);
}
} else {
if (e.type === "mouseup") {
if (this.buttonState === "down") {
this.canvasHandler(e, true);
}
}
if (e.type === "mousedown") {
this.canvasFocused = false;
}
}
}
},
getPos: function (e) {
var x, y,
boundingRect = this.core.canvasElement.getBoundingClientRect(),
node = document.documentElement || document.body.parentNode,
scrollElem = (node && (typeof node.ScrollTop === "number") ? node : document.body),
scrollX = window.scrollX !== undefined ? window.scrollX : (window.pageXOffset !== undefined ? window.pageXOffset : scrollElem.ScrollLeft),
scrollY = window.scrollY !== undefined ? window.scrollY : (window.pageYOffset !== undefined ? window.pageYOffset : scrollElem.ScrollTop);
// Browsers supporting pageX/pageY
if (e.pageX !== undefined && e.pageY !== undefined) {
x = e.pageX - scrollX - Math.round(boundingRect.left);
y = e.pageY - scrollY - Math.round(boundingRect.top);
}
// Browsers not supporting pageX/pageY
else if (e.clientX !== undefined && e.clientY !== undefined) {
x = e.clientX + scrollX - Math.round(boundingRect.left);
y = e.clientY + scrollY - Math.round(boundingRect.top);
}
return { x: x, y: y };
},
updatePos: function (e) {
var pos = this.getPos(e);
this.x = pos.x;
this.y = pos.y;
},
onCanvas: function (e) {
e = e || (this.core.events.lastPointerEventObject && this.core.events.lastPointerEventObject.originalEvent);
// Get pointer position
var pos = e ? this.getPos(e) : { x: this.x, y: this.y };
// Check boundaries => (left) && (right) && (top) && (bottom)
if ( (pos.x >= 0) && (pos.x <= this.core.width) && (pos.y >= 0) && (pos.y <= this.core.height) ) {
this.canvasHovered = true;
this.updatePos(e);
return true;
} else {
this.canvasHovered = false;
return false;
}
},
cancel: function () {
this.core.events.lastDownObject = null;
},
hide: function () {
this.core.canvasElement.style.cursor = "none";
},
show: function () {
this.core.canvasElement.style.cursor = this.cursorValue;
},
cursor: function (value) {
if (~value.indexOf("url(")) {
var m = /url\((.*?)\)(\s(.*?)\s(.*?)|)($|,.*?$)/.exec(value),
options = m[5] ? m[5] : "";
value = "url(" + m[1] + ") " + (m[3] ? m[3] : 0) + " " + (m[4] ? m[4] : 0) + (options !== "" ? options : ", default");
}
this.core.canvasElement.style.cursor = value;
this.cursorValue = value;
}
};
};
// Register the module
oCanvas.registerModule("mouse", mouse, "init");
})(oCanvas, window, document);