~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Control/MouseDefaults.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *
 
12
 * This class is DEPRECATED in 2.4 and will be removed by 3.0.
 
13
 *     If you need this functionality, use Control.Navigation instead!!!
 
14
 *
 
15
 * Inherits from:
 
16
 *  - <OpenLayers.Control>
 
17
 */
 
18
OpenLayers.Control.MouseDefaults = OpenLayers.Class(OpenLayers.Control, {
 
19
 
 
20
    /** WARNING WARNING WARNING!!!
 
21
        This class is DEPRECATED in 2.4 and will be removed by 3.0.
 
22
        If you need this functionality, use Control.Navigation instead!!! */
 
23
 
 
24
    /** 
 
25
     * Property: performedDrag
 
26
     * {Boolean}
 
27
     */
 
28
    performedDrag: false,
 
29
 
 
30
    /** 
 
31
     * Property: wheelObserver 
 
32
     * {Function}
 
33
     */
 
34
    wheelObserver: null,
 
35
 
 
36
    /** 
 
37
     * Constructor: OpenLayers.Control.MouseDefaults
 
38
     */
 
39
    initialize: function() {
 
40
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
 
41
    },
 
42
 
 
43
    /**
 
44
     * APIMethod: destroy
 
45
     */    
 
46
    destroy: function() {
 
47
        
 
48
        if (this.handler) {
 
49
            this.handler.destroy();
 
50
        }
 
51
        this.handler = null;
 
52
 
 
53
        this.map.events.un({
 
54
            "click": this.defaultClick,
 
55
            "dblclick": this.defaultDblClick,
 
56
            "mousedown": this.defaultMouseDown,
 
57
            "mouseup": this.defaultMouseUp,
 
58
            "mousemove": this.defaultMouseMove,
 
59
            "mouseout": this.defaultMouseOut,
 
60
            scope: this
 
61
        });
 
62
 
 
63
        //unregister mousewheel events specifically on the window and document
 
64
        OpenLayers.Event.stopObserving(window, "DOMMouseScroll", 
 
65
                                        this.wheelObserver);
 
66
        OpenLayers.Event.stopObserving(window, "mousewheel", 
 
67
                                        this.wheelObserver);
 
68
        OpenLayers.Event.stopObserving(document, "mousewheel", 
 
69
                                        this.wheelObserver);
 
70
        this.wheelObserver = null;
 
71
                      
 
72
        OpenLayers.Control.prototype.destroy.apply(this, arguments);        
 
73
    },
 
74
 
 
75
    /**
 
76
     * Method: draw
 
77
     */
 
78
    draw: function() {
 
79
        this.map.events.on({
 
80
            "click": this.defaultClick,
 
81
            "dblclick": this.defaultDblClick,
 
82
            "mousedown": this.defaultMouseDown,
 
83
            "mouseup": this.defaultMouseUp,
 
84
            "mousemove": this.defaultMouseMove,
 
85
            "mouseout": this.defaultMouseOut,
 
86
            scope: this
 
87
        });
 
88
 
 
89
        this.registerWheelEvents();
 
90
 
 
91
    },
 
92
 
 
93
    /**
 
94
     * Method: registerWheelEvents
 
95
     */
 
96
    registerWheelEvents: function() {
 
97
 
 
98
        this.wheelObserver = OpenLayers.Function.bindAsEventListener(
 
99
            this.onWheelEvent, this
 
100
        );
 
101
        
 
102
        //register mousewheel events specifically on the window and document
 
103
        OpenLayers.Event.observe(window, "DOMMouseScroll", this.wheelObserver);
 
104
        OpenLayers.Event.observe(window, "mousewheel", this.wheelObserver);
 
105
        OpenLayers.Event.observe(document, "mousewheel", this.wheelObserver);
 
106
    },
 
107
 
 
108
    /**
 
109
     * Method: defaultClick
 
110
     * 
 
111
     * Parameters:
 
112
     * evt - {Event} 
 
113
     *
 
114
     * Returns:
 
115
     * {Boolean}
 
116
     */
 
117
    defaultClick: function (evt) {
 
118
        if (!OpenLayers.Event.isLeftClick(evt)) {
 
119
            return;
 
120
        }
 
121
        var notAfterDrag = !this.performedDrag;
 
122
        this.performedDrag = false;
 
123
        return notAfterDrag;
 
124
    },
 
125
 
 
126
    /**
 
127
     * Method: defaultDblClick
 
128
     * 
 
129
     * Parameters:
 
130
     * evt - {Event} 
 
131
     */
 
132
    defaultDblClick: function (evt) {
 
133
        var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
 
134
        this.map.setCenter(newCenter, this.map.zoom + 1);
 
135
        OpenLayers.Event.stop(evt);
 
136
        return false;
 
137
    },
 
138
 
 
139
    /**
 
140
     * Method: defaultMouseDown
 
141
     * 
 
142
     * Parameters:
 
143
     * evt - {Event} 
 
144
     */
 
145
    defaultMouseDown: function (evt) {
 
146
        if (!OpenLayers.Event.isLeftClick(evt)) {
 
147
            return;
 
148
        }
 
149
        this.mouseDragStart = evt.xy.clone();
 
150
        this.performedDrag  = false;
 
151
        if (evt.shiftKey) {
 
152
            this.map.div.style.cursor = "crosshair";
 
153
            this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
 
154
                                                     this.mouseDragStart,
 
155
                                                     null,
 
156
                                                     null,
 
157
                                                     "absolute",
 
158
                                                     "2px solid red");
 
159
            this.zoomBox.style.backgroundColor = "white";
 
160
            this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
 
161
            this.zoomBox.style.opacity = "0.50";
 
162
            this.zoomBox.style.fontSize = "1px";
 
163
            this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
 
164
            this.map.viewPortDiv.appendChild(this.zoomBox);
 
165
        }
 
166
        document.onselectstart=function() { return false; };
 
167
        OpenLayers.Event.stop(evt);
 
168
    },
 
169
 
 
170
    /**
 
171
     * Method: defaultMouseMove
 
172
     *
 
173
     * Parameters:
 
174
     * evt - {Event} 
 
175
     */
 
176
    defaultMouseMove: function (evt) {
 
177
        // record the mouse position, used in onWheelEvent
 
178
        this.mousePosition = evt.xy.clone();
 
179
 
 
180
        if (this.mouseDragStart != null) {
 
181
            if (this.zoomBox) {
 
182
                var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
 
183
                var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
 
184
                this.zoomBox.style.width = Math.max(1, deltaX) + "px";
 
185
                this.zoomBox.style.height = Math.max(1, deltaY) + "px";
 
186
                if (evt.xy.x < this.mouseDragStart.x) {
 
187
                    this.zoomBox.style.left = evt.xy.x+"px";
 
188
                }
 
189
                if (evt.xy.y < this.mouseDragStart.y) {
 
190
                    this.zoomBox.style.top = evt.xy.y+"px";
 
191
                }
 
192
            } else {
 
193
                var deltaX = this.mouseDragStart.x - evt.xy.x;
 
194
                var deltaY = this.mouseDragStart.y - evt.xy.y;
 
195
                var size = this.map.getSize();
 
196
                var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
 
197
                                                 size.h / 2 + deltaY);
 
198
                var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
 
199
                this.map.setCenter(newCenter, null, true);
 
200
                this.mouseDragStart = evt.xy.clone();
 
201
                this.map.div.style.cursor = "move";
 
202
            }
 
203
            this.performedDrag = true;
 
204
        }
 
205
    },
 
206
 
 
207
    /**
 
208
     * Method: defaultMouseUp
 
209
     * 
 
210
     * Parameters:
 
211
     * evt - {<OpenLayers.Event>} 
 
212
     */
 
213
    defaultMouseUp: function (evt) {
 
214
        if (!OpenLayers.Event.isLeftClick(evt)) {
 
215
            return;
 
216
        }
 
217
        if (this.zoomBox) {
 
218
            this.zoomBoxEnd(evt);    
 
219
        } else {
 
220
            if (this.performedDrag) {
 
221
                this.map.setCenter(this.map.center);
 
222
            }
 
223
        }
 
224
        document.onselectstart=null;
 
225
        this.mouseDragStart = null;
 
226
        this.map.div.style.cursor = "";
 
227
    },
 
228
 
 
229
    /**
 
230
     * Method: defaultMouseOut
 
231
     * 
 
232
     * Parameters:
 
233
     * evt - {Event} 
 
234
     */
 
235
    defaultMouseOut: function (evt) {
 
236
        if (this.mouseDragStart != null && 
 
237
            OpenLayers.Util.mouseLeft(evt, this.map.div)) {
 
238
            if (this.zoomBox) {
 
239
                this.removeZoomBox();
 
240
            }
 
241
            this.mouseDragStart = null;
 
242
        }
 
243
    },
 
244
 
 
245
 
 
246
    /** 
 
247
     * Method: defaultWheelUp
 
248
     * User spun scroll wheel up
 
249
     * 
 
250
     */
 
251
    defaultWheelUp: function(evt) {
 
252
        if (this.map.getZoom() <= this.map.getNumZoomLevels()) {
 
253
            this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
 
254
                               this.map.getZoom() + 1);
 
255
        }
 
256
    },
 
257
 
 
258
    /**
 
259
     * Method: defaultWheelDown
 
260
     * User spun scroll wheel down
 
261
     */
 
262
    defaultWheelDown: function(evt) {
 
263
        if (this.map.getZoom() > 0) {
 
264
            this.map.setCenter(this.map.getLonLatFromPixel(evt.xy),
 
265
                               this.map.getZoom() - 1);
 
266
        }
 
267
    },
 
268
 
 
269
    /**
 
270
     * Method: zoomBoxEnd
 
271
     * Zoombox function. 
 
272
     */
 
273
    zoomBoxEnd: function(evt) {
 
274
        if (this.mouseDragStart != null) {
 
275
            if (Math.abs(this.mouseDragStart.x - evt.xy.x) > 5 ||    
 
276
                Math.abs(this.mouseDragStart.y - evt.xy.y) > 5) {   
 
277
                var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart ); 
 
278
                var end = this.map.getLonLatFromViewPortPx( evt.xy );
 
279
                var top = Math.max(start.lat, end.lat);
 
280
                var bottom = Math.min(start.lat, end.lat);
 
281
                var left = Math.min(start.lon, end.lon);
 
282
                var right = Math.max(start.lon, end.lon);
 
283
                var bounds = new OpenLayers.Bounds(left, bottom, right, top);
 
284
                this.map.zoomToExtent(bounds);
 
285
            } else {
 
286
                var end = this.map.getLonLatFromViewPortPx( evt.xy );
 
287
                this.map.setCenter(new OpenLayers.LonLat(
 
288
                  (end.lon),
 
289
                  (end.lat)
 
290
                 ), this.map.getZoom() + 1);
 
291
            }    
 
292
            this.removeZoomBox();
 
293
       }
 
294
    },
 
295
 
 
296
    /**
 
297
     * Method: removeZoomBox
 
298
     * Remove the zoombox from the screen and nullify our reference to it.
 
299
     */
 
300
    removeZoomBox: function() {
 
301
        this.map.viewPortDiv.removeChild(this.zoomBox);
 
302
        this.zoomBox = null;
 
303
    },
 
304
 
 
305
 
 
306
/**
 
307
 *  Mouse ScrollWheel code thanks to http://adomas.org/javascript-mouse-wheel/
 
308
 */
 
309
 
 
310
 
 
311
    /**
 
312
     * Method: onWheelEvent
 
313
     * Catch the wheel event and handle it xbrowserly
 
314
     *
 
315
     * Parameters: 
 
316
     * e - {Event} 
 
317
     */
 
318
    onWheelEvent: function(e){
 
319
    
 
320
        // first determine whether or not the wheeling was inside the map
 
321
        var inMap = false;
 
322
        var elem = OpenLayers.Event.element(e);
 
323
        while(elem != null) {
 
324
            if (this.map && elem == this.map.div) {
 
325
                inMap = true;
 
326
                break;
 
327
            }
 
328
            elem = elem.parentNode;
 
329
        }
 
330
        
 
331
        if (inMap) {
 
332
            
 
333
            var delta = 0;
 
334
            if (!e) {
 
335
                e = window.event;
 
336
            }
 
337
            if (e.wheelDelta) {
 
338
                delta = e.wheelDelta/120; 
 
339
                if (window.opera && window.opera.version() < 9.2) {
 
340
                    delta = -delta;
 
341
                }
 
342
            } else if (e.detail) {
 
343
                delta = -e.detail / 3;
 
344
            }
 
345
            if (delta) {
 
346
                // add the mouse position to the event because mozilla has a bug
 
347
                // with clientX and clientY (see https://bugzilla.mozilla.org/show_bug.cgi?id=352179)
 
348
                // getLonLatFromViewPortPx(e) returns wrong values
 
349
                e.xy = this.mousePosition;
 
350
 
 
351
                if (delta < 0) {
 
352
                   this.defaultWheelDown(e);
 
353
                } else {
 
354
                   this.defaultWheelUp(e);
 
355
                }
 
356
            }
 
357
            
 
358
            //only wheel the map, not the window
 
359
            OpenLayers.Event.stop(e);
 
360
        }
 
361
    },
 
362
 
 
363
    CLASS_NAME: "OpenLayers.Control.MouseDefaults"
 
364
});