1 | /*
|
---|
2 | * file: Mouse.js
|
---|
3 | *
|
---|
4 | * @BEGINLICENSE
|
---|
5 | * Copyright 2010 Brook Novak (email : [email protected])
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
17 | * @ENDLICENSE
|
---|
18 | */
|
---|
19 | bootstrap.provides("events.Mouse");
|
---|
20 |
|
---|
21 | (function() {
|
---|
22 |
|
---|
23 | var LEFT_BUTTON = "1",
|
---|
24 | RIGHT_BUTTON = "2",
|
---|
25 | toNormalizedIDMap, // Platform dependant map for transating to platform independant values
|
---|
26 | draggingScrollBars = false,
|
---|
27 | // The keys are button values in dom mouse event objects.
|
---|
28 | // The values are booleans, where true means they are down
|
---|
29 | mouseStateMap = {};
|
---|
30 |
|
---|
31 | $enqueueInit("events.Mouse", function(){
|
---|
32 |
|
---|
33 | // Ensure that the document always captures mouse events to track mouse state
|
---|
34 | //_addHandler(document, "mousedown", function() {}); // DEPRECIATED: Selection will always have these events
|
---|
35 | // _addHandler(document, "mouseup", function() {});
|
---|
36 |
|
---|
37 | // Setup mouse-button map for translating button/which values into a normalized/agreed value for all platforms
|
---|
38 | if (_browser == _Platform.IE) {
|
---|
39 | toNormalizedIDMap = {
|
---|
40 | "1": LEFT_BUTTON,
|
---|
41 | "2": RIGHT_BUTTON
|
---|
42 | };
|
---|
43 | } else {// DOM Complient
|
---|
44 | // DOM have screwed up their specification. There is no way to tell
|
---|
45 | // reliably whether a left click is down since their button value
|
---|
46 | toNormalizedIDMap = {
|
---|
47 | "0": LEFT_BUTTON,
|
---|
48 | "2": RIGHT_BUTTON
|
---|
49 | };
|
---|
50 | }
|
---|
51 |
|
---|
52 | mouseStateMap[LEFT_BUTTON] = mouseStateMap[RIGHT_BUTTON] = false;
|
---|
53 |
|
---|
54 | });
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * @class A singleton that provides cross-browser/platform mouse-state facilties.
|
---|
58 | * @author Brook Jesse Novak
|
---|
59 | */
|
---|
60 | de.events.Mouse = {
|
---|
61 |
|
---|
62 | sniffMouseDownEvent : function(e) {
|
---|
63 |
|
---|
64 | // Update mouse button state
|
---|
65 | var normalizedClickID = toNormalizedIDMap[e.button];
|
---|
66 |
|
---|
67 | if (normalizedClickID) {
|
---|
68 | // Left click + ctrl = right click on macs
|
---|
69 | if (_os == _Platform.MAC && normalizedClickID == LEFT_BUTTON && e.ctrlKey)
|
---|
70 | normalizedClickID = RIGHT_CLICK;
|
---|
71 | mouseStateMap[normalizedClickID] = true;
|
---|
72 | }
|
---|
73 |
|
---|
74 | // Filter out mouse events on the document when the mouse is on the scroll bars
|
---|
75 | if (isOnScrollBars(e)) {
|
---|
76 | draggingScrollBars = this.isLeftDown();
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | return true;
|
---|
81 | },
|
---|
82 |
|
---|
83 | sniffMouseUpEvent: function(e){
|
---|
84 |
|
---|
85 | // Update mouse button state
|
---|
86 | var normalizedClickID = toNormalizedIDMap[e.button];
|
---|
87 |
|
---|
88 | if (normalizedClickID) {
|
---|
89 | // Left click + ctrl = right click on macs. However- the user may have depressed the ctrl key before the
|
---|
90 | // key up event. Therefore, if this is a mac, and if this button up event is a left button, then set both left and right
|
---|
91 | // states to no longer being down.
|
---|
92 | if (_os == _Platform.MAC && normalizedClickID == LEFT_BUTTON)
|
---|
93 | // Sure this isn't perfect but there is no way to tell - and its not the end of the world,
|
---|
94 | // I don't think the use of two buttons at the same time is very useful for text-editors anyway!
|
---|
95 | this.clearDownStates();
|
---|
96 | else
|
---|
97 | mouseStateMap[normalizedClickID] = false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | draggingScrollBars = this.isLeftDown();
|
---|
101 |
|
---|
102 | // Filter out mouse events on the document when the mouse is on the scroll bars
|
---|
103 | return !isOnScrollBars(e);
|
---|
104 | },
|
---|
105 |
|
---|
106 | sniffMouseMoveEvent: function(e){
|
---|
107 | // Filter out mouse move events within the document (including the actual document)
|
---|
108 | // if the user is scrolling.
|
---|
109 | return !(this.isLeftDown() && draggingScrollBars);
|
---|
110 | },
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * @return {Boolean} True if the left mouse button is currently down.
|
---|
114 | *
|
---|
115 | * <br>
|
---|
116 | * <b>Note</b>: On a mac, if the user left-clicks while ctrl is down, this seen a right click
|
---|
117 | * gesture for their single button mouse design - but also does the same thing for mouses
|
---|
118 | * with multiple buttons.
|
---|
119 | *
|
---|
120 | */
|
---|
121 | isLeftDown: function(){
|
---|
122 | return mouseStateMap[LEFT_BUTTON];
|
---|
123 | },
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * @return {Boolean} True if the right mouse button is currently down.
|
---|
127 | */
|
---|
128 | isRightDown: function(){
|
---|
129 | return mouseStateMap[RIGHT_BUTTON];
|
---|
130 | },
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Clears the button down states.
|
---|
134 | * Useful if mouse module is unable to capture/sniff mouse up events in a particulat environment ...
|
---|
135 | * this provides a way to manually clear mousedown states.
|
---|
136 | */
|
---|
137 | clearDownStates : function() {
|
---|
138 | mouseStateMap[LEFT_BUTTON] = false;
|
---|
139 | mouseStateMap[RIGHT_BUTTON] = false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | }; // End mouse singleton
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Determines whether mouse event targetted at the document is over scroll bars
|
---|
146 | * @param {Object} e
|
---|
147 | * @return {Boolean} Evaluate true if mouse event was on scrollbars
|
---|
148 | */
|
---|
149 | function isOnScrollBars(e) {
|
---|
150 |
|
---|
151 | var target = de.events.getEventTarget(e);
|
---|
152 |
|
---|
153 | if (target == window || target == document || target == document.documentElement) {
|
---|
154 |
|
---|
155 | // Opera never raises events on document scrollbars
|
---|
156 | if (_engine == _Platform.PRESTO)
|
---|
157 | return;
|
---|
158 |
|
---|
159 | var pos = de.events.getXYInWindowFromEvent(e),
|
---|
160 | viewportSize = _getViewPortSize();
|
---|
161 | if (pos.x >= viewportSize.width)
|
---|
162 | return 1;
|
---|
163 |
|
---|
164 | return pos.y >= viewportSize.height;
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 | })();
|
---|