-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.js
175 lines (161 loc) · 4.91 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
define(["heya-has/sniff", "heya-dom/dom", "./on"], function(has, dom, on){
"use strict";
// module:
// dojo/mouse
has.add("dom-quirks", function(global, doc){
return doc && doc.compatMode === "BackCompat";
});
has.add("events-mouseenter", function(global, doc, element){
return element && "onmouseenter" in element;
});
has.add("events-mousewheel", function(global, doc){
return doc && "onmousewheel" in doc;
});
var mouseButtons;
if((has("dom-quirks") && has("ie")) || !has("dom-addeventlistener")){
mouseButtons = {
LEFT: 1,
MIDDLE: 4,
RIGHT: 2,
// helper functions
isButton: function(e, button){ return e.button & button; },
isLeft: function(e){ return e.button & 1; },
isMiddle: function(e){ return e.button & 4; },
isRight: function(e){ return e.button & 2; }
};
}else{
mouseButtons = {
LEFT: 0,
MIDDLE: 1,
RIGHT: 2,
// helper functions
isButton: function(e, button){ return e.button == button; },
isLeft: function(e){ return e.button == 0; },
isMiddle: function(e){ return e.button == 1; },
isRight: function(e){ return e.button == 2; }
};
}
/*=====
dojo.mouseButtons = {
// LEFT: Number
// Numeric value of the left mouse button for the platform.
LEFT: 0,
// MIDDLE: Number
// Numeric value of the middle mouse button for the platform.
MIDDLE: 1,
// RIGHT: Number
// Numeric value of the right mouse button for the platform.
RIGHT: 2,
isButton: function(e, button){
// summary:
// Checks an event object for a pressed button
// e: Event
// Event object to examine
// button: Number
// The button value (example: dojo.mouseButton.LEFT)
return e.button == button; // Boolean
},
isLeft: function(e){
// summary:
// Checks an event object for the pressed left button
// e: Event
// Event object to examine
return e.button == 0; // Boolean
},
isMiddle: function(e){
// summary:
// Checks an event object for the pressed middle button
// e: Event
// Event object to examine
return e.button == 1; // Boolean
},
isRight: function(e){
// summary:
// Checks an event object for the pressed right button
// e: Event
// Event object to examine
return e.button == 2; // Boolean
}
};
=====*/
function eventHandler(type){
// emulation of mouseenter/leave with mouseover/out using descendant checking
function handler(node){
return on(node, type, function(evt, sink){
return !dom.isDescendant(evt.relatedTarget, node) ? evt : sink.noValue;
});
};
handler.bubble = function(select){
return eventHandler(type, function(evt, sink){
// using a selector, use the select function to determine
// if the mouse moved inside the selector and was previously
// outside the selector
var target = select(evt.target), relatedTarget = evt.relatedTarget;
if(target && (target !== (relatedTarget && relatedTarget.nodeType == 1 && select(relatedTarget)))){
return evt;
}
return sink.noValue;
});
};
return handler;
}
var wheel;
if(has("events-mousewheel")){
wheel = "mousewheel";
}else{ //firefox
wheel = function(node){
return on(node, "DOMMouseScroll", function(evt){
evt.wheelDelta = -evt.detail;
return evt;
});
};
}
return {
// summary:
// This module provide mouse event handling utility functions and exports
// mouseenter and mouseleave event emulation.
// example:
// To use these events, you register a mouseenter like this:
// | define(["dojo/on", dojo/mouse"], function(on, mouse){
// | on(targetNode, mouse.enter, function(event){
// | dojo.addClass(targetNode, "highlighted");
// | });
// | on(targetNode, mouse.leave, function(event){
// | dojo.removeClass(targetNode, "highlighted");
// | });
_eventHandler: eventHandler, // for dojo/touch
// enter: Synthetic Event
// This is an extension event for the mouseenter that IE provides, emulating the
// behavior on other browsers.
enter: eventHandler("mouseover"),
// leave: Synthetic Event
// This is an extension event for the mouseleave that IE provides, emulating the
// behavior on other browsers.
leave: eventHandler("mouseout"),
// wheel: Normalized Mouse Wheel Event
// This is an extension event for the mousewheel that non-Mozilla browsers provide,
// emulating the behavior on Mozilla based browsers.
wheel: wheel,
isLeft: mouseButtons.isLeft,
/*=====
isLeft: function(){
// summary:
// Test an event object (from a mousedown event) to see if the left button was pressed.
},
=====*/
isMiddle: mouseButtons.isMiddle,
/*=====
isMiddle: function(){
// summary:
// Test an event object (from a mousedown event) to see if the middle button was pressed.
},
=====*/
isRight: mouseButtons.isRight
/*=====
, isRight: function(){
// summary:
// Test an event object (from a mousedown event) to see if the right button was pressed.
}
=====*/
};
});