]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/MouseDefaults.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Control / MouseDefaults.js
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. */
4
5 /**
6  * @requires OpenLayers/Control.js
7  */
8
9 /**
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> 
13  * instead!!!
14  *
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!!!
17  *
18  * Inherits from:
19  *  - <OpenLayers.Control>
20  */
21 OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, {
22
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!!! */
26
27     /** 
28      * Property: performedDrag
29      * {Boolean}
30      */
31     performedDrag: false,
32
33     /** 
34      * Property: wheelObserver 
35      * {Function}
36      */
37     wheelObserver: null,
38
39     /** 
40      * Constructor: OpenLayers.Control.MouseDefaults
41      */
42     initialize: function() {
43         OpenLayers.Control.prototype.initialize.apply(this, arguments);
44     },
45
46     /**
47      * APIMethod: destroy
48      */    
49     destroy: function() {
50         
51         if (this.handler) {
52             this.handler.destroy();
53         }
54         this.handler = null;
55
56         this.map.events.un({
57             "click": this.defaultClick,
58             "dblclick": this.defaultDblClick,
59             "mousedown": this.defaultMouseDown,
60             "mouseup": this.defaultMouseUp,
61             "mousemove": this.defaultMouseMove,
62             "mouseout": this.defaultMouseOut,
63             scope: this
64         });
65
66         //unregister mousewheel events specifically on the window and document
67         OpenLayers.Event.stopObserving(window, "DOMMouseScroll", 
68                                         this.wheelObserver);
69         OpenLayers.Event.stopObserving(window, "mousewheel", 
70                                         this.wheelObserver);
71         OpenLayers.Event.stopObserving(document, "mousewheel", 
72                                         this.wheelObserver);
73         this.wheelObserver = null;
74                       
75         OpenLayers.Control.prototype.destroy.apply(this, arguments);        
76     },
77
78     /**
79      * Method: draw
80      */
81     draw: function() {
82         this.map.events.on({
83             "click": this.defaultClick,
84             "dblclick": this.defaultDblClick,
85             "mousedown": this.defaultMouseDown,
86             "mouseup": this.defaultMouseUp,
87             "mousemove": this.defaultMouseMove,
88             "mouseout": this.defaultMouseOut,
89             scope: this
90         });
91
92         this.registerWheelEvents();
93
94     },
95
96     /**
97      * Method: registerWheelEvents
98      */
99     registerWheelEvents: function() {
100
101         this.wheelObserver = OpenLayers.Function.bindAsEventListener(
102             this.onWheelEvent, this
103         );
104         
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);
109     },
110
111     /**
112      * Method: defaultClick
113      * 
114      * Parameters:
115      * evt - {Event} 
116      *
117      * Returns:
118      * {Boolean}
119      */
120     defaultClick: function (evt) {
121         if (!OpenLayers.Event.isLeftClick(evt)) {
122             return;
123         }
124         var notAfterDrag = !this.performedDrag;
125         this.performedDrag = false;
126         return notAfterDrag;
127     },
128
129     /**
130      * Method: defaultDblClick
131      * 
132      * Parameters:
133      * evt - {Event} 
134      */
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);
139         return false;
140     },
141
142     /**
143      * Method: defaultMouseDown
144      * 
145      * Parameters:
146      * evt - {Event} 
147      */
148     defaultMouseDown: function (evt) {
149         if (!OpenLayers.Event.isLeftClick(evt)) {
150             return;
151         }
152         this.mouseDragStart = evt.xy.clone();
153         this.performedDrag  = false;
154         if (evt.shiftKey) {
155             this.map.div.style.cursor = "crosshair";
156             this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
157                                                      this.mouseDragStart,
158                                                      null,
159                                                      null,
160                                                      "absolute",
161                                                      "2px solid red");
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);
168         }
169         document.onselectstart=function() { return false; };
170         OpenLayers.Event.stop(evt);
171     },
172
173     /**
174      * Method: defaultMouseMove
175      *
176      * Parameters:
177      * evt - {Event} 
178      */
179     defaultMouseMove: function (evt) {
180         // record the mouse position, used in onWheelEvent
181         this.mousePosition = evt.xy.clone();
182
183         if (this.mouseDragStart != null) {
184             if (this.zoomBox) {
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";
191                 }
192                 if (evt.xy.y < this.mouseDragStart.y) {
193                     this.zoomBox.style.top = evt.xy.y+"px";
194                 }
195             } else {
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";
205             }
206             this.performedDrag = true;
207         }
208     },
209
210     /**
211      * Method: defaultMouseUp
212      * 
213      * Parameters:
214      * evt - {<OpenLayers.Event>} 
215      */
216     defaultMouseUp: function (evt) {
217         if (!OpenLayers.Event.isLeftClick(evt)) {
218             return;
219         }
220         if (this.zoomBox) {
221             this.zoomBoxEnd(evt);    
222         } else {
223             if (this.performedDrag) {
224                 this.map.setCenter(this.map.center);
225             }
226         }
227         document.onselectstart=null;
228         this.mouseDragStart = null;
229         this.map.div.style.cursor = "";
230     },
231
232     /**
233      * Method: defaultMouseOut
234      * 
235      * Parameters:
236      * evt - {Event} 
237      */
238     defaultMouseOut: function (evt) {
239         if (this.mouseDragStart != null && 
240             OpenLayers.Util.mouseLeft(evt, this.map.div)) {
241             if (this.zoomBox) {
242                 this.removeZoomBox();
243             }
244             this.mouseDragStart = null;
245         }
246     },
247
248
249     /** 
250      * Method: defaultWheelUp
251      * User spun scroll wheel up
252      * 
253      */
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);
258         }
259     },
260
261     /**
262      * Method: defaultWheelDown
263      * User spun scroll wheel down
264      */
265     defaultWheelDown: function(evt) {
266         if (this.map.getZoom() > 0) {
267             this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
268                                this.map.getZoom() - 1);
269         }
270     },
271
272     /**
273      * Method: zoomBoxEnd
274      * Zoombox function. 
275      */
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);
288             } else {
289                 var end = this.map.getLonLatFromViewPortPx( evt.xy );
290                 this.map.setCenter(new OpenLayers.LonLat(
291                   (end.lon),
292                   (end.lat)
293                  ), this.map.getZoom() + 1);
294             }    
295             this.removeZoomBox();
296        }
297     },
298
299     /**
300      * Method: removeZoomBox
301      * Remove the zoombox from the screen and nullify our reference to it.
302      */
303     removeZoomBox: function() {
304         this.map.viewPortDiv.removeChild(this.zoomBox);
305         this.zoomBox = null;
306     },
307
308
309 /**
310  *  Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/
311  */
312
313
314     /**
315      * Method: onWheelEvent
316      * Catch the wheel event and handle it xbrowserly
317      *
318      * Parameters: 
319      * e - {Event} 
320      */
321     onWheelEvent: function(e){
322     
323         // first determine whether or not the wheeling was inside the map
324         var inMap = false;
325         var elem = OpenLayers.Event.element(e);
326         while(elem != null) {
327             if (this.map && elem == this.map.div) {
328                 inMap = true;
329                 break;
330             }
331             elem = elem.parentNode;
332         }
333         
334         if (inMap) {
335             
336             var delta = 0;
337             if (!e) {
338                 e = window.event;
339             }
340             if (e.wheelDelta) {
341                 delta = e.wheelDelta/120; 
342                 if (window.opera && window.opera.version() < 9.2) {
343                     delta = -delta;
344                 }
345             } else if (e.detail) {
346                 delta = -e.detail / 3;
347             }
348             if (delta) {
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;
353
354                 if (delta < 0) {
355                    this.defaultWheelDown(e);
356                 } else {
357                    this.defaultWheelUp(e);
358                 }
359             }
360             
361             //only wheel the map, not the window
362             OpenLayers.Event.stop(e);
363         }
364     },
365
366     CLASS_NAME: "OpenLayers.Control.MouseDefaults"
367 });