]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Control/PanZoom.js
fix #2046
[rails.git] / public / lib / OpenLayers / Control / PanZoom.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
6 /**
7  * @class
8  * 
9  * default zoom/pan controls
10  */
11 OpenLayers.Control.PanZoom = Class.create();
12 OpenLayers.Control.PanZoom.X = 4;
13 OpenLayers.Control.PanZoom.Y = 4;
14 OpenLayers.Control.PanZoom.prototype = 
15   Object.extend( new OpenLayers.Control(), {
16
17     /** @type int */
18     slideFactor: 50,
19
20     /** @type Array of Button Divs */
21     buttons: null,
22
23     /** @type OpenLayers.Pixel */
24     position: null,
25
26     /**
27      * @constructor
28      */
29     initialize: function() {
30         OpenLayers.Control.prototype.initialize.apply(this, arguments);
31         this.position = new OpenLayers.Pixel(OpenLayers.Control.PanZoom.X,
32                                              OpenLayers.Control.PanZoom.Y);
33     },
34
35     /**
36     * @param {OpenLayers.Pixel} px
37     * 
38     * @returns A reference to the container div for the PanZoom control
39     * @type DOMElement
40     */
41     draw: function(px) {
42         // initialize our internal div
43         OpenLayers.Control.prototype.draw.apply(this, arguments);
44         px = this.position;
45
46         // place the controls
47         this.buttons = new Array();
48
49         var sz = new OpenLayers.Size(18,18);
50         var centered = new OpenLayers.Pixel(px.x+sz.w/2, px.y);
51
52         this._addButton("panup", "north-mini.png", centered, sz);
53         px.y = centered.y+sz.h;
54         this._addButton("panleft", "west-mini.png", px, sz);
55         this._addButton("panright", "east-mini.png", px.add(sz.w, 0), sz);
56         this._addButton("pandown", "south-mini.png", 
57                         centered.add(0, sz.h*2), sz);
58         this._addButton("zoomin", "zoom-plus-mini.png", 
59                         centered.add(0, sz.h*3+5), sz);
60         this._addButton("zoomworld", "zoom-world-mini.png", 
61                         centered.add(0, sz.h*4+5), sz);
62         this._addButton("zoomout", "zoom-minus-mini.png", 
63                         centered.add(0, sz.h*5+5), sz);
64         return this.div;
65     },
66     
67     /**
68      * @param {String} id
69      * @param {String} img
70      * @param {OpenLayers.Pixel} xy
71      * @param {OpenLayers.Size} sz
72      * 
73      * @returns A Div (an alphaImageDiv, to be precise) that contains the 
74      *          image of the button, and has all the proper event handlers
75      *          set.
76      * @type DOMElement
77      */
78     _addButton:function(id, img, xy, sz) {
79         var imgLocation = OpenLayers.Util.getImagesLocation() + img;
80         // var btn = new ol.AlphaImage("_"+id, imgLocation, xy, sz);
81         var btn = OpenLayers.Util.createAlphaImageDiv(
82                                     "OpenLayers_Control_PanZoom_" + id, 
83                                     xy, sz, imgLocation, "absolute");
84
85         //we want to add the outer div
86         this.div.appendChild(btn);
87
88         btn.onmousedown = this.buttonDown.bindAsEventListener(btn);
89         btn.ondblclick  = this.doubleClick.bindAsEventListener(btn);
90         btn.onclick  = this.doubleClick.bindAsEventListener(btn);
91         btn.action = id;
92         btn.map = this.map;
93         btn.slideFactor = this.slideFactor;
94
95         //we want to remember/reference the outer div
96         this.buttons.push(btn);
97         return btn;
98     },
99     
100     /**
101      * @param {event} evt
102      * 
103      * @type Boolean
104      */
105     doubleClick: function (evt) {
106         Event.stop(evt);
107         return false;
108     },
109     
110     /**
111      * @param {event} evt
112      */
113     buttonDown: function (evt) {
114         if (!Event.isLeftClick(evt)) return;
115
116         var slide = this.map.getResolution() * this.slideFactor;
117         var center = this.map.getCenter();
118
119         var newCenter = center.copyOf();
120
121         switch (this.action) {
122             case "panup": 
123                 newCenter = newCenter.add( 0, slide);
124                 break;
125             case "pandown": 
126                 newCenter = newCenter.add( 0, -slide);
127                 break;
128             case "panleft": 
129                 newCenter = newCenter.add( -slide, 0);
130                 break;
131             case "panright": 
132                 newCenter = newCenter.add( slide, 0);
133                 break;
134             case "zoomin": 
135                 this.map.zoomIn(); 
136                 break;
137             case "zoomout": 
138                 this.map.zoomOut(); 
139                 break;
140             case "zoomworld": 
141                 this.map.zoomToFullExtent(); 
142                 break;
143         }
144         
145         if (!newCenter.equals(center)) {
146             this.map.setCenter(newCenter);
147         }
148         
149         Event.stop(evt);
150     },
151
152     /**
153      * 
154      */
155     destroy: function() {
156         OpenLayers.Control.prototype.destroy.apply(this, arguments);
157         for(i=0; i<this.buttons.length; i++) {
158             this.buttons[i].map = null;
159         }
160     },
161     
162     /** @final @type String */
163     CLASS_NAME: "OpenLayers.Control.PanZoom"
164 });