Ticket #1489: permalink.2.patch
| File permalink.2.patch, 7.6 kB (added by euzuro, 4 months ago) |
|---|
-
tests/Control/Permalink.html
old new 108 108 OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B'; 109 109 t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base"); 110 110 } 111 112 function test_Control_Permalink_customized(t) { 113 t.plan(2); 114 115 var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, { 116 CLASS_NAME: "CustomArgParser" 117 }); 118 119 control = new OpenLayers.Control.Permalink(null, "./edit.html", { 120 argParserClass: argParserClass, 121 createParams: function(center, zoom, layers) { 122 var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments); 123 params.customParam = "foo"; 124 return params; 125 } 126 }); 127 map = new OpenLayers.Map('map'); 128 layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}); 129 map.addLayer(layer); 130 if (!map.getCenter()) map.zoomToMaxExtent(); 131 map.addControl(control); 132 map.pan(5, 0, {animate:false}); 133 134 t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly."); 135 t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly."); 136 } 111 137 </script> 112 138 </head> 113 139 <body> -
lib/OpenLayers/Control/Permalink.js
old new 15 15 * - <OpenLayers.Control> 16 16 */ 17 17 OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, { 18 19 /** 20 * APIProperty: argParserClass 21 * {Class} The ArgParser control class (not instance) to use with this 22 * control. 23 */ 24 argParserClass: OpenLayers.Control.ArgParser, 18 25 19 26 /** 20 27 * Property: element … … 81 88 //make sure we have an arg parser attached 82 89 for(var i=0, len=this.map.controls.length; i<len; i++) { 83 90 var control = this.map.controls[i]; 84 if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") {91 if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) { 85 92 86 93 // If a permalink is added to the map, and an ArgParser already 87 94 // exists, we override the displayProjection to be the one … … 94 101 } 95 102 } 96 103 if (i == this.map.controls.length) { 97 this.map.addControl(new OpenLayers.Control.ArgParser(104 this.map.addControl(new this.argParserClass( 98 105 { 'displayProjection': this.displayProjection })); 99 106 } 100 107 … … 110 117 OpenLayers.Control.prototype.draw.apply(this, arguments); 111 118 112 119 if (!this.element) { 120 this.div.className = this.displayClass; 113 121 this.element = document.createElement("a"); 114 122 this.element.innerHTML = OpenLayers.i18n("permalink"); 115 123 this.element.href=""; … … 121 129 'changebaselayer': this.updateLink, 122 130 scope: this 123 131 }); 132 133 // Make it so there is at least a link even though the map may not have 134 // moved yet. 135 this.updateLink(); 136 124 137 return this.div; 125 138 }, 126 139 … … 128 141 * Method: updateLink 129 142 */ 130 143 updateLink: function() { 131 var center = this.map.getCenter(); 132 133 // Map not initialized yet. Break out of this function. 134 if (!center) { 135 return; 144 var href = this.base; 145 if (href.indexOf('?') != -1) { 146 href = href.substring( 0, href.indexOf('?') ); 136 147 } 137 148 149 href += '?' + OpenLayers.Util.getParameterString(this.createParams()); 150 this.element.href = href; 151 }, 152 153 /** 154 * APIMethod: createParams 155 * Creates the parameters that need to be encoded into the permalink url. 156 * 157 * Parameters: 158 * center - {<OpenLayers.LonLat>} center to encode in the permalink. 159 * Defaults to the current map center. 160 * zoom - {Integer} zoom level to encode in the permalink. Defaults to the 161 * current map zoom level. 162 * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink. 163 * Defaults to the current map layers. 164 * 165 * Returns: 166 * {Object} Hash of parameters that will be url-encoded into the 167 * permalink. 168 */ 169 createParams: function(center, zoom, layers) { 170 center = center || this.map.getCenter(); 171 zoom = zoom || this.map.getZoom(); 172 layers = layers || this.map.layers; 173 138 174 var params = OpenLayers.Util.getParameters(this.base); 139 175 140 params.zoom = this.map.getZoom(); 141 var lat = center.lat; 142 var lon = center.lon; 143 144 if (this.displayProjection) { 145 var mapPosition = OpenLayers.Projection.transform( 146 { x: lon, y: lat }, 147 this.map.getProjectionObject(), 148 this.displayProjection ); 149 lon = mapPosition.x; 150 lat = mapPosition.y; 151 } 152 params.lat = Math.round(lat*100000)/100000; 153 params.lon = Math.round(lon*100000)/100000; 154 155 params.layers = ''; 156 for (var i=0, len=this.map.layers.length; i<len; i++) { 157 var layer = this.map.layers[i]; 158 159 if (layer.isBaseLayer) { 160 params.layers += (layer == this.map.baseLayer) ? "B" : "0"; 161 } else { 162 params.layers += (layer.getVisibility()) ? "T" : "F"; 176 // If there's still no center, map is not initialized yet. 177 // Break out of this function, and simply return the params from the 178 // base link. 179 if (center) { 180 181 params.zoom = this.map.getZoom(); 182 var lat = center.lat; 183 var lon = center.lon; 184 185 if (this.displayProjection) { 186 var mapPosition = OpenLayers.Projection.transform( 187 { x: lon, y: lat }, 188 this.map.getProjectionObject(), 189 this.displayProjection ); 190 lon = mapPosition.x; 191 lat = mapPosition.y; 192 } 193 params.lat = Math.round(lat*100000)/100000; 194 params.lon = Math.round(lon*100000)/100000; 195 196 params.layers = ''; 197 for (var i=0, len=this.map.layers.length; i<len; i++) { 198 var layer = this.map.layers[i]; 199 200 if (layer.isBaseLayer) { 201 params.layers += (layer == this.map.baseLayer) ? "B" : "0"; 202 } else { 203 params.layers += (layer.getVisibility()) ? "T" : "F"; 204 } 163 205 } 164 206 } 165 207 166 var href = this.base; 167 if (href.indexOf('?') != -1) { 168 href = href.substring( 0, href.indexOf('?') ); 169 } 170 171 href += '?' + OpenLayers.Util.getParameterString(params); 172 this.element.href = href; 208 return params; 173 209 }, 174 210 175 211 CLASS_NAME: "OpenLayers.Control.Permalink"
