]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Control/MouseDefaults.js
Booleans should be true/false - coding style comments welcome
[rails.git] / public / lib / OpenLayers / Control / MouseDefaults.js
1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3  * text of the license. */
4 // @require: OpenLayers/Control.js
5 OpenLayers.Control.MouseDefaults = Class.create();
6 OpenLayers.Control.MouseDefaults.prototype = 
7   Object.extend( new OpenLayers.Control(), {
8
9     performedDrag: false,
10
11     initialize: function() {
12         OpenLayers.Control.prototype.initialize.apply(this, arguments);
13     },
14     
15     draw: function() {
16         this.map.events.register( "click", this, this.defaultClick );
17         this.map.events.register( "dblclick", this, this.defaultDblClick );
18         this.map.events.register( "mousedown", this, this.defaultMouseDown );
19         this.map.events.register( "mouseup", this, this.defaultMouseUp );
20         this.map.events.register( "mousemove", this, this.defaultMouseMove );
21         this.map.events.register( "mouseout", this, this.defaultMouseOut );
22     },
23
24     defaultClick: function (evt) {
25         if (!Event.isLeftClick(evt)) return;
26         var notAfterDrag = !this.performedDrag;
27         this.performedDrag = false;
28         return notAfterDrag;
29     },
30
31     /**
32     * @param {Event} evt
33     */
34     defaultDblClick: function (evt) {
35         var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
36         this.map.setCenter(newCenter, this.map.zoom + 1);
37     },
38
39     /**
40     * @param {Event} evt
41     */
42     defaultMouseDown: function (evt) {
43         if (!Event.isLeftClick(evt)) return;
44         this.mouseDragStart = evt.xy.copyOf();
45         this.performedDrag  = false;
46         if (evt.shiftKey) {
47             this.map.div.style.cursor = "crosshair";
48             this.zoomBox = OpenLayers.Util.createDiv('zoomBox',
49                                                      this.mouseDragStart,
50                                                      null,
51                                                      null,
52                                                      "absolute",
53                                                      "2px solid red");
54             this.zoomBox.style.backgroundColor = "white";
55             this.zoomBox.style.filter = "alpha(opacity=50)"; // IE
56             this.zoomBox.style.opacity = "0.50";
57             this.zoomBox.style.zIndex = this.map.Z_INDEX_BASE["Popup"] - 1;
58             this.map.viewPortDiv.appendChild(this.zoomBox);
59         }
60         document.onselectstart=function() { return false; }
61         Event.stop(evt);
62     },
63
64     /**
65     * @param {Event} evt
66     */
67     defaultMouseMove: function (evt) {
68         if (this.mouseDragStart != null) {
69             if (this.zoomBox) {
70                 var deltaX = Math.abs(this.mouseDragStart.x - evt.xy.x);
71                 var deltaY = Math.abs(this.mouseDragStart.y - evt.xy.y);
72                 this.zoomBox.style.width = deltaX+"px";
73                 this.zoomBox.style.height = deltaY+"px";
74                 if (evt.xy.x < this.mouseDragStart.x) {
75                     this.zoomBox.style.left = evt.xy.x+"px";
76                 }
77                 if (evt.xy.y < this.mouseDragStart.y) {
78                     this.zoomBox.style.top = evt.xy.y+"px";
79                 }
80             } else {
81                 var deltaX = this.mouseDragStart.x - evt.xy.x;
82                 var deltaY = this.mouseDragStart.y - evt.xy.y;
83                 var size = this.map.getSize();
84                 var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX,
85                                                  size.h / 2 + deltaY);
86                 var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
87                 this.map.setCenter(newCenter, null, true);
88                 this.mouseDragStart = evt.xy.copyOf();
89                 this.map.div.style.cursor = "move";
90             }
91             this.performedDrag = true;
92         }
93     },
94
95     /**
96     * @param {Event} evt
97     */
98     defaultMouseUp: function (evt) {
99         if (!Event.isLeftClick(evt)) return;
100         if (this.zoomBox) {
101             var start = this.map.getLonLatFromViewPortPx( this.mouseDragStart ); 
102             var end = this.map.getLonLatFromViewPortPx( evt.xy );
103             var top = Math.max(start.lat, end.lat);
104             var bottom = Math.min(start.lat, end.lat);
105             var left = Math.min(start.lon, end.lon);
106             var right = Math.max(start.lon, end.lon);
107             var bounds = new OpenLayers.Bounds(left, bottom, right, top);
108             var zoom = this.map.getZoomForExtent(bounds);
109             this.map.setCenter(new OpenLayers.LonLat(
110               (start.lon + end.lon) / 2,
111               (start.lat + end.lat) / 2
112              ), zoom);
113             this.map.viewPortDiv.removeChild(document.getElementById("zoomBox"));
114             this.zoomBox = null;
115         } else {
116             this.map.setCenter(this.map.center);
117         }
118         document.onselectstart=null;
119         this.mouseDragStart = null;
120         this.map.div.style.cursor = "default";
121     },
122
123     defaultMouseOut: function (evt) {
124         if (this.mouseDragStart != null
125             && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
126                 this.defaultMouseUp(evt);
127         }
128     }
129 });
130