1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Control.js
10 * Class: OpenLayers.Control.MouseDefaults
11 * This class is DEPRECATED in 2.4 and will be removed by 3.0.
12 * If you need this functionality, use <OpenLayers.Control.Navigation>
15 * This class is DEPRECATED in 2.4 and will be removed by 3.0.
16 * If you need this functionality, use Control.Navigation instead!!!
19 * - <OpenLayers.Control>
21 OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, {
23 /** WARNING WARNING WARNING!!!
24 This class is DEPRECATED in 2.4 and will be removed by 3.0.
25 If you need this functionality, use Control.Navigation instead!!! */
28 * Property: performedDrag
34 * Property: wheelObserver
40 * Constructor: OpenLayers.Control.MouseDefaults
42 initialize: function() {
43 OpenLayers.Control.prototype.initialize.apply(this, arguments);
52 this.handler.destroy();
57 "click": this.defaultClick,
58 "dblclick": this.defaultDblClick,
59 "mousedown": this.defaultMouseDown,
60 "mouseup": this.defaultMouseUp,
61 "mousemove": this.defaultMouseMove,
62 "mouseout": this.defaultMouseOut,
66 //unregister mousewheel events specifically on the window and document
67 OpenLayers.Event.stopObserving(window, "DOMMouseScroll",
69 OpenLayers.Event.stopObserving(window, "mousewheel",
71 OpenLayers.Event.stopObserving(document, "mousewheel",
73 this.wheelObserver = null;
75 OpenLayers.Control.prototype.destroy.apply(this, arguments);
83 "click": this.defaultClick,
84 "dblclick": this.defaultDblClick,
85 "mousedown": this.defaultMouseDown,
86 "mouseup": this.defaultMouseUp,
87 "mousemove": this.defaultMouseMove,
88 "mouseout": this.defaultMouseOut,
92 this.registerWheelEvents();
97 * Method: registerWheelEvents
99 registerWheelEvents: function() {
101 this.wheelObserver = OpenLayers.Function.bindAsEventListener(
102 this.onWheelEvent, this
105 //register mousewheel events specifically on the window and document
106 OpenLayers.Event.observe(window, "DOMMouseScroll", this.wheelObserver);
107 OpenLayers.Event.observe(window, "mousewheel", this.wheelObserver);
108 OpenLayers.Event.observe(document, "mousewheel", this.wheelObserver);
112 * Method: defaultClick
120 defaultClick: function (evt) {
121 if (!OpenLayers.Event.isLeftClick(evt)) {
124 var notAfterDrag = !this.performedDrag;
125 this.performedDrag = false;
130 * Method: defaultDblClick
135 defaultDblClick: function (evt) {
136 var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
137 this.map.setCenter(newCenter, this.map.zoom + 1);
138 OpenLayers.Event.stop(evt);
143 * Method: defaultMouseDown
148 defaultMouseDown: function (evt) {
149 if (!OpenLayers.Event.isLeftClick(evt)) {
152 this.mouseDragStart = evt.xy.clone();
153 this.performedDrag = false;
155 this.map.div.style.cursor = "crosshair";
156 this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
162 this.zoomBox.style.backgroundColor = "white";
163 this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
164 this.zoomBox.style.opacity = "0.50";
165 this.zoomBox.style.fontSize = "1px";
166 this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
167 this.map.viewPortDiv.appendChild(this.zoomBox);
169 document.onselectstart=function() { return false; };
170 OpenLayers.Event.stop(evt);
174 * Method: defaultMouseMove
179 defaultMouseMove: function (evt) {
180 // record the mouse position, used in onWheelEvent
181 this.mousePosition = evt.xy.clone();
183 if (this.mouseDragStart != null) {
185 var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
186 var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
187 this.zoomBox.style.width = Math.max(1, deltaX) + "px";
188 this.zoomBox.style.height = Math.max(1, deltaY) + "px";
189 if (evt.xy.x < this.mouseDragStart.x) {
190 this.zoomBox.style.left = evt.xy.x+"px";
192 if (evt.xy.y < this.mouseDragStart.y) {
193 this.zoomBox.style.top = evt.xy.y+"px";
196 var deltaX = this.mouseDragStart.x - evt.xy.x;
197 var deltaY = this.mouseDragStart.y - evt.xy.y;
198 var size = this.map.getSize();
199 var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
200 size.h / 2 + deltaY);
201 var newCenter = this.map.getLonLatFromViewPortPx( newXY );
202 this.map.setCenter(newCenter, null, true);
203 this.mouseDragStart = evt.xy.clone();
204 this.map.div.style.cursor = "move";
206 this.performedDrag = true;
211 * Method: defaultMouseUp
214 * evt - {<OpenLayers.Event>}
216 defaultMouseUp: function (evt) {
217 if (!OpenLayers.Event.isLeftClick(evt)) {
221 this.zoomBoxEnd(evt);
223 if (this.performedDrag) {
224 this.map.setCenter(this.map.center);
227 document.onselectstart=null;
228 this.mouseDragStart = null;
229 this.map.div.style.cursor = "";
233 * Method: defaultMouseOut
238 defaultMouseOut: function (evt) {
239 if (this.mouseDragStart != null &&
240 OpenLayers.Util.mouseLeft(evt, this.map.div)) {
242 this.removeZoomBox();
244 this.mouseDragStart = null;
250 * Method: defaultWheelUp
251 * User spun scroll wheel up
254 defaultWheelUp: function(evt) {
255 if (this.map.getZoom() <= this.map.getNumZoomLevels()) {
256 this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
257 this.map.getZoom() + 1);
262 * Method: defaultWheelDown
263 * User spun scroll wheel down
265 defaultWheelDown: function(evt) {
266 if (this.map.getZoom() > 0) {
267 this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
268 this.map.getZoom() - 1);
276 zoomBoxEnd: function(evt) {
277 if (this.mouseDragStart != null) {
278 if (Math.abs(this.mouseDragStart.x - evt.xy.x) > 5 ||
279 Math.abs(this.mouseDragStart.y - evt.xy.y) > 5) {
280 var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart );
281 var end = this.map.getLonLatFromViewPortPx( evt.xy );
282 var top = Math.max(start.lat, end.lat);
283 var bottom = Math.min(start.lat, end.lat);
284 var left = Math.min(start.lon, end.lon);
285 var right = Math.max(start.lon, end.lon);
286 var bounds = new OpenLayers.Bounds(left, bottom, right, top);
287 this.map.zoomToExtent(bounds);
289 var end = this.map.getLonLatFromViewPortPx( evt.xy );
290 this.map.setCenter(new OpenLayers.LonLat(
293 ), this.map.getZoom() + 1);
295 this.removeZoomBox();
300 * Method: removeZoomBox
301 * Remove the zoombox from the screen and nullify our reference to it.
303 removeZoomBox: function() {
304 this.map.viewPortDiv.removeChild(this.zoomBox);
310 * Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/
315 * Method: onWheelEvent
316 * Catch the wheel event and handle it xbrowserly
321 onWheelEvent: function(e){
323 // first determine whether or not the wheeling was inside the map
325 var elem = OpenLayers.Event.element(e);
326 while(elem != null) {
327 if (this.map && elem == this.map.div) {
331 elem = elem.parentNode;
341 delta = e.wheelDelta/120;
342 if (window.opera && window.opera.version() < 9.2) {
345 } else if (e.detail) {
346 delta = -e.detail / 3;
349 // add the mouse position to the event because mozilla has a bug
350 // with clientX and clientY (see https://bugzilla.mozilla.org/show_bug.cgi?id=352179)
351 // getLonLatFromViewPortPx(e) returns wrong values
352 e.xy = this.mousePosition;
355 this.defaultWheelDown(e);
357 this.defaultWheelUp(e);
361 //only wheel the map, not the window
362 OpenLayers.Event.stop(e);
366 CLASS_NAME: "OpenLayers.Control.MouseDefaults"