]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Control/Permalink.js
fixes notices
[syp.git] / openlayers / lib / OpenLayers / Control / Permalink.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 /**
7  * @requires OpenLayers/Control.js
8  * @requires OpenLayers/Control/ArgParser.js
9  */
10
11 /**
12  * Class: OpenLayers.Control.Permalink
13  * The Permalink control is hyperlink that will return the user to the 
14  * current map view. By default it is drawn in the lower right corner of the
15  * map. The href is updated as the map is zoomed, panned and whilst layers
16  * are switched.
17  * `
18  * Inherits from:
19  *  - <OpenLayers.Control>
20  */
21 OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
22     
23     /**
24      * APIProperty: argParserClass
25      * {Class} The ArgParser control class (not instance) to use with this
26      *     control.
27      */
28     argParserClass: OpenLayers.Control.ArgParser,
29
30     /** 
31      * Property: element 
32      * {DOMElement}
33      */
34     element: null,
35     
36     /** 
37      * APIProperty: base
38      * {String}
39      */
40     base: '',
41
42     /** 
43      * APIProperty: displayProjection
44      * {<OpenLayers.Projection>} Requires proj4js support.  Projection used
45      *     when creating the coordinates in the link. This will reproject the
46      *     map coordinates into display coordinates. If you are using this
47      *     functionality, the permalink which is last added to the map will
48      *     determine the coordinate type which is read from the URL, which
49      *     means you should not add permalinks with different
50      *     displayProjections to the same map. 
51      */
52     displayProjection: null, 
53
54     /**
55      * Constructor: OpenLayers.Control.Permalink
56      *
57      * Parameters: 
58      * element - {DOMElement} 
59      * base - {String} 
60      * options - {Object} options to the control. 
61      */
62     initialize: function(element, base, options) {
63         OpenLayers.Control.prototype.initialize.apply(this, [options]);
64         this.element = OpenLayers.Util.getElement(element);        
65         this.base = base || document.location.href;
66     },
67
68     /**
69      * APIMethod: destroy
70      */
71     destroy: function()  {
72         if (this.element.parentNode == this.div) {
73             this.div.removeChild(this.element);
74         }
75         this.element = null;
76
77         this.map.events.unregister('moveend', this, this.updateLink);
78
79         OpenLayers.Control.prototype.destroy.apply(this, arguments); 
80     },
81
82     /**
83      * Method: setMap
84      * Set the map property for the control. 
85      * 
86      * Parameters:
87      * map - {<OpenLayers.Map>} 
88      */
89     setMap: function(map) {
90         OpenLayers.Control.prototype.setMap.apply(this, arguments);
91
92         //make sure we have an arg parser attached
93         for(var i=0, len=this.map.controls.length; i<len; i++) {
94             var control = this.map.controls[i];
95             if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) {
96                 
97                 // If a permalink is added to the map, and an ArgParser already
98                 // exists, we override the displayProjection to be the one
99                 // on the permalink. 
100                 if (control.displayProjection != this.displayProjection) {
101                     this.displayProjection = control.displayProjection;
102                 }    
103                 
104                 break;
105             }
106         }
107         if (i == this.map.controls.length) {
108             this.map.addControl(new this.argParserClass(
109                 { 'displayProjection': this.displayProjection }));       
110         }
111
112     },
113
114     /**
115      * Method: draw
116      *
117      * Returns:
118      * {DOMElement}
119      */    
120     draw: function() {
121         OpenLayers.Control.prototype.draw.apply(this, arguments);
122           
123         if (!this.element) {
124             this.div.className = this.displayClass;
125             this.element = document.createElement("a");
126             this.element.innerHTML = OpenLayers.i18n("permalink");
127             this.element.href="";
128             this.div.appendChild(this.element);
129         }
130         this.map.events.on({
131             'moveend': this.updateLink,
132             'changelayer': this.updateLink,
133             'changebaselayer': this.updateLink,
134             scope: this
135         });
136         
137         // Make it so there is at least a link even though the map may not have
138         // moved yet.
139         this.updateLink();
140         
141         return this.div;
142     },
143    
144     /**
145      * Method: updateLink 
146      */
147     updateLink: function() {
148         var href = this.base;
149         if (href.indexOf('?') != -1) {
150             href = href.substring( 0, href.indexOf('?') );
151         }
152
153         href += '?' + OpenLayers.Util.getParameterString(this.createParams());
154         this.element.href = href;
155     }, 
156     
157     /**
158      * APIMethod: createParams
159      * Creates the parameters that need to be encoded into the permalink url.
160      * 
161      * Parameters:
162      * center - {<OpenLayers.LonLat>} center to encode in the permalink.
163      *     Defaults to the current map center.
164      * zoom - {Integer} zoom level to encode in the permalink. Defaults to the
165      *     current map zoom level.
166      * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink.
167      *     Defaults to the current map layers.
168      * 
169      * Returns:
170      * {Object} Hash of parameters that will be url-encoded into the
171      * permalink.
172      */
173     createParams: function(center, zoom, layers) {
174         center = center || this.map.getCenter();
175           
176         var params = OpenLayers.Util.getParameters(this.base);
177         
178         // If there's still no center, map is not initialized yet. 
179         // Break out of this function, and simply return the params from the
180         // base link.
181         if (center) { 
182
183             //zoom
184             params.zoom = zoom || this.map.getZoom(); 
185
186             //lon,lat
187             var lat = center.lat;
188             var lon = center.lon;
189             
190             if (this.displayProjection) {
191                 var mapPosition = OpenLayers.Projection.transform(
192                   { x: lon, y: lat }, 
193                   this.map.getProjectionObject(), 
194                   this.displayProjection );
195                 lon = mapPosition.x;  
196                 lat = mapPosition.y;  
197             }       
198             params.lat = Math.round(lat*100000)/100000;
199             params.lon = Math.round(lon*100000)/100000;
200     
201             //layers        
202             layers = layers || this.map.layers;  
203             params.layers = '';
204             for (var i=0, len=layers.length; i<len; i++) {
205                 var layer = layers[i];
206     
207                 if (layer.isBaseLayer) {
208                     params.layers += (layer == this.map.baseLayer) ? "B" : "0";
209                 } else {
210                     params.layers += (layer.getVisibility()) ? "T" : "F";           
211                 }
212             }
213         }
214
215         return params;
216     }, 
217
218     CLASS_NAME: "OpenLayers.Control.Permalink"
219 });