Ticket #1489: 1489-r7869-A0.patch
| File 1489-r7869-A0.patch, 5.9 kB (added by ahocevar, 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 argParser = OpenLayers.Class(OpenLayers.Control.ArgParser, { 116 CLASS_NAME: "CustomArgParser" 117 }); 118 119 control = new OpenLayers.Control.Permalink(null, "./edit.html", { 120 argParser: argParser, 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: argParser 21 * {Class} The ArgParser control to use with this control 22 */ 23 argParser: OpenLayers.Control.ArgParser, 18 24 19 25 /** 20 26 * Property: element … … 81 87 //make sure we have an arg parser attached 82 88 for(var i=0, len=this.map.controls.length; i<len; i++) { 83 89 var control = this.map.controls[i]; 84 if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") {90 if (control.CLASS_NAME == this.argParser.CLASS_NAME) { 85 91 86 92 // If a permalink is added to the map, and an ArgParser already 87 93 // exists, we override the displayProjection to be the one … … 94 100 } 95 101 } 96 102 if (i == this.map.controls.length) { 97 this.map.addControl(new OpenLayers.Control.ArgParser(103 this.map.addControl(new this.argParser( 98 104 { 'displayProjection': this.displayProjection })); 99 105 } 100 106 … … 110 116 OpenLayers.Control.prototype.draw.apply(this, arguments); 111 117 112 118 if (!this.element) { 119 this.div.className = this.displayClass; 113 120 this.element = document.createElement("a"); 114 121 this.element.innerHTML = OpenLayers.i18n("permalink"); 115 122 this.element.href=""; … … 121 128 'changebaselayer': this.updateLink, 122 129 scope: this 123 130 }); 131 132 // Make it so there is at least a link even though the map may not have 133 // moved yet. 134 this.updateLink(); 135 124 136 return this.div; 125 137 }, 126 138 … … 128 140 * Method: updateLink 129 141 */ 130 142 updateLink: function() { 131 var center = this.map.getCenter(); 143 var href = this.base; 144 if (href.indexOf('?') != -1) { 145 href = href.substring( 0, href.indexOf('?') ); 146 } 147 148 href += '?' + OpenLayers.Util.getParameterString(this.createParams()); 149 this.element.href = href; 150 }, 151 152 /** 153 * APIMethod: createParams 154 * Creates the parameters that need to be encoded into the permalink url. 155 * 156 * Parameters: 157 * center - {<OpenLayers.LonLat>} center to encode in the permalink. 158 * Defaults to the current map center. 159 * zoom - {Integer} zoom level to encode in the permalink. Defaults to the 160 * current map zoom level. 161 * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink. 162 * Defaults to the current map layers. 163 * 164 * Returns: 165 * {Object} Hash of parameters that will be url-encoded into the 166 * permalink. 167 */ 168 createParams: function(center, zoom, layers) { 169 center = center || this.map.getCenter(); 170 zoom = zoom || this.map.getZoom(); 171 layers = layers || this.map.layers; 172 173 var params = OpenLayers.Util.getParameters(this.base); 132 174 133 // Map not initialized yet. Break out of this function. 175 // If there's still no center, map is not initialized yet. 176 // Break out of this function, and simply return the params from the 177 // base link. 134 178 if (!center) { 135 return ;179 return params; 136 180 } 137 181 138 var params = OpenLayers.Util.getParameters(this.base);139 140 182 params.zoom = this.map.getZoom(); 141 183 var lat = center.lat; 142 184 var lon = center.lon; … … 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"
