]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/Navigation.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Control / Navigation.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/ZoomBox.js
7  * @requires OpenLayers/Control/DragPan.js
8  * @requires OpenLayers/Handler/MouseWheel.js
9  * @requires OpenLayers/Handler/Click.js
10  */
11
12 /**
13  * Class: OpenLayers.Control.Navigation
14  * The navigation control handles map browsing with mouse events (dragging,
15  *     double-clicking, and scrolling the wheel).  Create a new navigation 
16  *     control with the <OpenLayers.Control.Navigation> control.  
17  * 
18  *     Note that this control is added to the map by default (if no controls 
19  *     array is sent in the options object to the <OpenLayers.Map> 
20  *     constructor).
21  * 
22  * Inherits:
23  *  - <OpenLayers.Control>
24  */
25 OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
26
27     /** 
28      * Property: dragPan
29      * {<OpenLayers.Control.DragPan>} 
30      */
31     dragPan: null,
32
33     /**
34      * APIProprety: dragPanOptions
35      * {Object} Options passed to the DragPan control.
36      */
37     dragPanOptions: null,
38
39     /** 
40      * Property: zoomBox
41      * {<OpenLayers.Control.ZoomBox>}
42      */
43     zoomBox: null,
44
45     /**
46      * APIProperty: zoomWheelEnabled
47      * {Boolean} Whether the mousewheel should zoom the map
48      */
49     zoomWheelEnabled: true, 
50
51     /**
52      * APIProperty: handleRightClicks
53      * {Boolean} Whether or not to handle right clicks. Default is false.
54      */
55     handleRightClicks: false,
56
57     /**
58      * APIProperty: zoomBoxKeyMask
59      * {Integer} <OpenLayers.Handler> key code of the key, which has to be
60      *    pressed, while drawing the zoom box with the mouse on the screen. 
61      *    You should probably set handleRightClicks to true if you use this
62      *    with MOD_CTRL, to disable the context menu for machines which use
63      *    CTRL-Click as a right click.
64      * Default: <OpenLayers.Handler.MOD_SHIFT
65      */
66     zoomBoxKeyMask: OpenLayers.Handler.MOD_SHIFT,
67     
68     /**
69      * Constructor: OpenLayers.Control.Navigation
70      * Create a new navigation control
71      * 
72      * Parameters:
73      * options - {Object} An optional object whose properties will be set on
74      *                    the control
75      */
76     initialize: function(options) {
77         this.handlers = {};
78         OpenLayers.Control.prototype.initialize.apply(this, arguments);
79     },
80
81     /**
82      * Method: destroy
83      * The destroy method is used to perform any clean up before the control
84      * is dereferenced.  Typically this is where event listeners are removed
85      * to prevent memory leaks.
86      */
87     destroy: function() {
88         this.deactivate();
89
90         if (this.dragPan) {
91             this.dragPan.destroy();
92         }
93         this.dragPan = null;
94
95         if (this.zoomBox) {
96             this.zoomBox.destroy();
97         }
98         this.zoomBox = null;
99         OpenLayers.Control.prototype.destroy.apply(this,arguments);
100     },
101     
102     /**
103      * Method: activate
104      */
105     activate: function() {
106         this.dragPan.activate();
107         if (this.zoomWheelEnabled) {
108             this.handlers.wheel.activate();
109         }    
110         this.handlers.click.activate();
111         this.zoomBox.activate();
112         return OpenLayers.Control.prototype.activate.apply(this,arguments);
113     },
114
115     /**
116      * Method: deactivate
117      */
118     deactivate: function() {
119         this.zoomBox.deactivate();
120         this.dragPan.deactivate();
121         this.handlers.click.deactivate();
122         this.handlers.wheel.deactivate();
123         return OpenLayers.Control.prototype.deactivate.apply(this,arguments);
124     },
125     
126     /**
127      * Method: draw
128      */
129     draw: function() {
130         // disable right mouse context menu for support of right click events
131         if (this.handleRightClicks) {
132             this.map.viewPortDiv.oncontextmenu = function () { return false;};
133         }
134
135         var clickCallbacks = { 
136             'dblclick': this.defaultDblClick, 
137             'dblrightclick': this.defaultDblRightClick 
138         };
139         var clickOptions = {
140             'double': true, 
141             'stopDouble': true
142         };
143         this.handlers.click = new OpenLayers.Handler.Click(
144             this, clickCallbacks, clickOptions
145         );
146         this.dragPan = new OpenLayers.Control.DragPan(
147             OpenLayers.Util.extend({map: this.map}, this.dragPanOptions)
148         );
149         this.zoomBox = new OpenLayers.Control.ZoomBox(
150                     {map: this.map, keyMask: this.zoomBoxKeyMask});
151         this.dragPan.draw();
152         this.zoomBox.draw();
153         this.handlers.wheel = new OpenLayers.Handler.MouseWheel(
154                                     this, {"up"  : this.wheelUp,
155                                            "down": this.wheelDown} );
156         this.activate();
157     },
158
159     /**
160      * Method: defaultDblClick 
161      * 
162      * Parameters:
163      * evt - {Event} 
164      */
165     defaultDblClick: function (evt) {
166         var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
167         this.map.setCenter(newCenter, this.map.zoom + 1);
168     },
169
170     /**
171      * Method: defaultDblRightClick 
172      * 
173      * Parameters:
174      * evt - {Event} 
175      */
176     defaultDblRightClick: function (evt) {
177         var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
178         this.map.setCenter(newCenter, this.map.zoom - 1);
179     },
180     
181     /**
182      * Method: wheelChange  
183      *
184      * Parameters:
185      * evt - {Event}
186      * deltaZ - {Integer}
187      */
188     wheelChange: function(evt, deltaZ) {
189         var newZoom = this.map.getZoom() + deltaZ;
190         if (!this.map.isValidZoomLevel(newZoom)) {
191             return;
192         }
193         var size    = this.map.getSize();
194         var deltaX  = size.w/2 - evt.xy.x;
195         var deltaY  = evt.xy.y - size.h/2;
196         var newRes  = this.map.baseLayer.getResolutionForZoom(newZoom);
197         var zoomPoint = this.map.getLonLatFromPixel(evt.xy);
198         var newCenter = new OpenLayers.LonLat(
199                             zoomPoint.lon + deltaX * newRes,
200                             zoomPoint.lat + deltaY * newRes );
201         this.map.setCenter( newCenter, newZoom );
202     },
203
204     /** 
205      * Method: wheelUp
206      * User spun scroll wheel up
207      * 
208      * Parameters:
209      * evt - {Event}
210      */
211     wheelUp: function(evt) {
212         this.wheelChange(evt, 1);
213     },
214
215     /** 
216      * Method: wheelDown
217      * User spun scroll wheel down
218      * 
219      * Parameters:
220      * evt - {Event}
221      */
222     wheelDown: function(evt) {
223         this.wheelChange(evt, -1);
224     },
225     
226     /**
227      * Method: disableZoomWheel
228      */
229     
230     disableZoomWheel : function() {
231         this.zoomWheelEnabled = false;
232         this.handlers.wheel.deactivate();       
233     },
234     
235     /**
236      * Method: enableZoomWheel
237      */
238     
239     enableZoomWheel : function() {
240         this.zoomWheelEnabled = true;
241         if (this.active) {
242             this.handlers.wheel.activate();
243         }    
244     },
245
246     CLASS_NAME: "OpenLayers.Control.Navigation"
247 });