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