Project

General

Profile

Download (9.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
2
 * full list of contributors). Published under the 2-clause BSD license.
3
 * See license.txt in the OpenLayers distribution or repository for the
4
 * full text of the license. */
5

    
6
/**
7
 * @requires OpenLayers/Control/ZoomBox.js
8
 * @requires OpenLayers/Control/DragPan.js
9
 * @requires OpenLayers/Handler/MouseWheel.js
10
 * @requires OpenLayers/Handler/Click.js
11
 */
12

    
13
/**
14
 * Class: OpenLayers.Control.Navigation
15
 * The navigation control handles map browsing with mouse events (dragging,
16
 *     double-clicking, and scrolling the wheel).  Create a new navigation 
17
 *     control with the <OpenLayers.Control.Navigation> control.  
18
 * 
19
 *     Note that this control is added to the map by default (if no controls 
20
 *     array is sent in the options object to the <OpenLayers.Map> 
21
 *     constructor).
22
 * 
23
 * Inherits:
24
 *  - <OpenLayers.Control>
25
 */
26
OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
27

    
28
    /** 
29
     * Property: dragPan
30
     * {<OpenLayers.Control.DragPan>} 
31
     */
32
    dragPan: null,
33

    
34
    /**
35
     * APIProperty: dragPanOptions
36
     * {Object} Options passed to the DragPan control.
37
     */
38
    dragPanOptions: null,
39

    
40
    /**
41
     * Property: pinchZoom
42
     * {<OpenLayers.Control.PinchZoom>}
43
     */
44
    pinchZoom: null,
45

    
46
    /**
47
     * APIProperty: pinchZoomOptions
48
     * {Object} Options passed to the PinchZoom control.
49
     */
50
    pinchZoomOptions: null,
51

    
52
    /**
53
     * APIProperty: documentDrag
54
     * {Boolean} Allow panning of the map by dragging outside map viewport.
55
     *     Default is false.
56
     */
57
    documentDrag: false,
58

    
59
    /** 
60
     * Property: zoomBox
61
     * {<OpenLayers.Control.ZoomBox>}
62
     */
63
    zoomBox: null,
64

    
65
    /**
66
     * APIProperty: zoomBoxEnabled
67
     * {Boolean} Whether the user can draw a box to zoom
68
     */
69
    zoomBoxEnabled: true, 
70

    
71
    /**
72
     * APIProperty: zoomWheelEnabled
73
     * {Boolean} Whether the mousewheel should zoom the map
74
     */
75
    zoomWheelEnabled: true,
76
    
77
    /**
78
     * Property: mouseWheelOptions
79
     * {Object} Options passed to the MouseWheel control (only useful if
80
     *     <zoomWheelEnabled> is set to true). Default is no options for maps
81
     *     with fractionalZoom set to true, otherwise
82
     *     {cumulative: false, interval: 50, maxDelta: 6} 
83
     */
84
    mouseWheelOptions: null,
85

    
86
    /**
87
     * APIProperty: handleRightClicks
88
     * {Boolean} Whether or not to handle right clicks. Default is false.
89
     */
90
    handleRightClicks: false,
91

    
92
    /**
93
     * APIProperty: zoomBoxKeyMask
94
     * {Integer} <OpenLayers.Handler> key code of the key, which has to be
95
     *    pressed, while drawing the zoom box with the mouse on the screen. 
96
     *    You should probably set handleRightClicks to true if you use this
97
     *    with MOD_CTRL, to disable the context menu for machines which use
98
     *    CTRL-Click as a right click.
99
     * Default: <OpenLayers.Handler.MOD_SHIFT>
100
     */
101
    zoomBoxKeyMask: OpenLayers.Handler.MOD_SHIFT,
102
    
103
    /**
104
     * APIProperty: autoActivate
105
     * {Boolean} Activate the control when it is added to a map.  Default is
106
     *     true.
107
     */
108
    autoActivate: true,
109

    
110
    /**
111
     * Constructor: OpenLayers.Control.Navigation
112
     * Create a new navigation control
113
     * 
114
     * Parameters:
115
     * options - {Object} An optional object whose properties will be set on
116
     *                    the control
117
     */
118
    initialize: function(options) {
119
        this.handlers = {};
120
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
121
    },
122

    
123
    /**
124
     * Method: destroy
125
     * The destroy method is used to perform any clean up before the control
126
     * is dereferenced.  Typically this is where event listeners are removed
127
     * to prevent memory leaks.
128
     */
129
    destroy: function() {
130
        this.deactivate();
131

    
132
        if (this.dragPan) {
133
            this.dragPan.destroy();
134
        }
135
        this.dragPan = null;
136

    
137
        if (this.zoomBox) {
138
            this.zoomBox.destroy();
139
        }
140
        this.zoomBox = null;
141

    
142
        if (this.pinchZoom) {
143
            this.pinchZoom.destroy();
144
        }
145
        this.pinchZoom = null;
146

    
147
        OpenLayers.Control.prototype.destroy.apply(this,arguments);
148
    },
149
    
150
    /**
151
     * Method: activate
152
     */
153
    activate: function() {
154
        this.dragPan.activate();
155
        if (this.zoomWheelEnabled) {
156
            this.handlers.wheel.activate();
157
        }    
158
        this.handlers.click.activate();
159
        if (this.zoomBoxEnabled) {
160
            this.zoomBox.activate();
161
        }
162
        if (this.pinchZoom) {
163
            this.pinchZoom.activate();
164
        }
165
        return OpenLayers.Control.prototype.activate.apply(this,arguments);
166
    },
167

    
168
    /**
169
     * Method: deactivate
170
     */
171
    deactivate: function() {
172
        if (this.pinchZoom) {
173
            this.pinchZoom.deactivate();
174
        }
175
        this.zoomBox.deactivate();
176
        this.dragPan.deactivate();
177
        this.handlers.click.deactivate();
178
        this.handlers.wheel.deactivate();
179
        return OpenLayers.Control.prototype.deactivate.apply(this,arguments);
180
    },
181
    
182
    /**
183
     * Method: draw
184
     */
185
    draw: function() {
186
        // disable right mouse context menu for support of right click events
187
        if (this.handleRightClicks) {
188
            this.map.viewPortDiv.oncontextmenu = OpenLayers.Function.False;
189
        }
190

    
191
        var clickCallbacks = { 
192
            'click': this.defaultClick,
193
            'dblclick': this.defaultDblClick, 
194
            'dblrightclick': this.defaultDblRightClick 
195
        };
196
        var clickOptions = {
197
            'double': true, 
198
            'stopDouble': true
199
        };
200
        this.handlers.click = new OpenLayers.Handler.Click(
201
            this, clickCallbacks, clickOptions
202
        );
203
        this.dragPan = new OpenLayers.Control.DragPan(
204
            OpenLayers.Util.extend({
205
                map: this.map,
206
                documentDrag: this.documentDrag
207
            }, this.dragPanOptions)
208
        );
209
        this.zoomBox = new OpenLayers.Control.ZoomBox(
210
                    {map: this.map, keyMask: this.zoomBoxKeyMask});
211
        this.dragPan.draw();
212
        this.zoomBox.draw();
213
        var wheelOptions = this.map.fractionalZoom ? {} : {
214
            cumulative: false,
215
            interval: 50,
216
            maxDelta: 6
217
        };
218
        this.handlers.wheel = new OpenLayers.Handler.MouseWheel(
219
            this, {up : this.wheelUp, down: this.wheelDown},
220
            OpenLayers.Util.extend(wheelOptions, this.mouseWheelOptions)
221
        );
222
        if (OpenLayers.Control.PinchZoom) {
223
            this.pinchZoom = new OpenLayers.Control.PinchZoom(
224
                OpenLayers.Util.extend(
225
                    {map: this.map}, this.pinchZoomOptions));
226
        }
227
    },
228

    
229
    /**
230
     * Method: defaultClick
231
     *
232
     * Parameters:
233
     * evt - {Event}
234
     */
235
    defaultClick: function (evt) {
236
        if (evt.lastTouches && evt.lastTouches.length == 2) {
237
            this.map.zoomOut();
238
        }
239
    },
240

    
241
    /**
242
     * Method: defaultDblClick 
243
     * 
244
     * Parameters:
245
     * evt - {Event} 
246
     */
247
    defaultDblClick: function (evt) {
248
        this.map.zoomTo(this.map.zoom + 1, evt.xy);
249
    },
250

    
251
    /**
252
     * Method: defaultDblRightClick 
253
     * 
254
     * Parameters:
255
     * evt - {Event} 
256
     */
257
    defaultDblRightClick: function (evt) {
258
        this.map.zoomTo(this.map.zoom - 1, evt.xy);
259
    },
260
    
261
    /**
262
     * Method: wheelChange  
263
     *
264
     * Parameters:
265
     * evt - {Event}
266
     * deltaZ - {Integer}
267
     */
268
    wheelChange: function(evt, deltaZ) {
269
        if (!this.map.fractionalZoom) {
270
            deltaZ =  Math.round(deltaZ);
271
        }
272
        var currentZoom = this.map.getZoom(),
273
            newZoom = currentZoom + deltaZ;
274
        newZoom = Math.max(newZoom, 0);
275
        newZoom = Math.min(newZoom, this.map.getNumZoomLevels());
276
        if (newZoom === currentZoom) {
277
            return;
278
        }
279
        this.map.zoomTo(newZoom, evt.xy);
280
    },
281

    
282
    /** 
283
     * Method: wheelUp
284
     * User spun scroll wheel up
285
     * 
286
     * Parameters:
287
     * evt - {Event}
288
     * delta - {Integer}
289
     */
290
    wheelUp: function(evt, delta) {
291
        this.wheelChange(evt, delta || 1);
292
    },
293

    
294
    /** 
295
     * Method: wheelDown
296
     * User spun scroll wheel down
297
     * 
298
     * Parameters:
299
     * evt - {Event}
300
     * delta - {Integer}
301
     */
302
    wheelDown: function(evt, delta) {
303
        this.wheelChange(evt, delta || -1);
304
    },
305
    
306
    /**
307
     * Method: disableZoomBox
308
     */
309
    disableZoomBox : function() {
310
        this.zoomBoxEnabled = false;
311
        this.zoomBox.deactivate();       
312
    },
313
    
314
    /**
315
     * Method: enableZoomBox
316
     */
317
    enableZoomBox : function() {
318
        this.zoomBoxEnabled = true;
319
        if (this.active) {
320
            this.zoomBox.activate();
321
        }    
322
    },
323
    
324
    /**
325
     * Method: disableZoomWheel
326
     */
327
    
328
    disableZoomWheel : function() {
329
        this.zoomWheelEnabled = false;
330
        this.handlers.wheel.deactivate();       
331
    },
332
    
333
    /**
334
     * Method: enableZoomWheel
335
     */
336
    
337
    enableZoomWheel : function() {
338
        this.zoomWheelEnabled = true;
339
        if (this.active) {
340
            this.handlers.wheel.activate();
341
        }    
342
    },
343

    
344
    CLASS_NAME: "OpenLayers.Control.Navigation"
345
});
(19-19/45)