made permalink control more configurable by adding an argParserClass property and separating the logic for generating the key-value pairs from the div and link generation. Original patch by tcoulter. r=euzuro,me (closes #1489)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7881 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -15,6 +15,13 @@
|
|||||||
* - <OpenLayers.Control>
|
* - <OpenLayers.Control>
|
||||||
*/
|
*/
|
||||||
OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIProperty: argParserClass
|
||||||
|
* {Class} The ArgParser control class (not instance) to use with this
|
||||||
|
* control.
|
||||||
|
*/
|
||||||
|
argParserClass: OpenLayers.Control.ArgParser,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property: element
|
* Property: element
|
||||||
@@ -81,7 +88,7 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
//make sure we have an arg parser attached
|
//make sure we have an arg parser attached
|
||||||
for(var i=0, len=this.map.controls.length; i<len; i++) {
|
for(var i=0, len=this.map.controls.length; i<len; i++) {
|
||||||
var control = this.map.controls[i];
|
var control = this.map.controls[i];
|
||||||
if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") {
|
if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) {
|
||||||
|
|
||||||
// If a permalink is added to the map, and an ArgParser already
|
// If a permalink is added to the map, and an ArgParser already
|
||||||
// exists, we override the displayProjection to be the one
|
// exists, we override the displayProjection to be the one
|
||||||
@@ -94,7 +101,7 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (i == this.map.controls.length) {
|
if (i == this.map.controls.length) {
|
||||||
this.map.addControl(new OpenLayers.Control.ArgParser(
|
this.map.addControl(new this.argParserClass(
|
||||||
{ 'displayProjection': this.displayProjection }));
|
{ 'displayProjection': this.displayProjection }));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +117,7 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
OpenLayers.Control.prototype.draw.apply(this, arguments);
|
OpenLayers.Control.prototype.draw.apply(this, arguments);
|
||||||
|
|
||||||
if (!this.element) {
|
if (!this.element) {
|
||||||
|
this.div.className = this.displayClass;
|
||||||
this.element = document.createElement("a");
|
this.element = document.createElement("a");
|
||||||
this.element.innerHTML = OpenLayers.i18n("permalink");
|
this.element.innerHTML = OpenLayers.i18n("permalink");
|
||||||
this.element.href="";
|
this.element.href="";
|
||||||
@@ -121,6 +129,11 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
'changebaselayer': this.updateLink,
|
'changebaselayer': this.updateLink,
|
||||||
scope: this
|
scope: this
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Make it so there is at least a link even though the map may not have
|
||||||
|
// moved yet.
|
||||||
|
this.updateLink();
|
||||||
|
|
||||||
return this.div;
|
return this.div;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -128,49 +141,72 @@ OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* Method: updateLink
|
* Method: updateLink
|
||||||
*/
|
*/
|
||||||
updateLink: function() {
|
updateLink: function() {
|
||||||
var center = this.map.getCenter();
|
|
||||||
|
|
||||||
// Map not initialized yet. Break out of this function.
|
|
||||||
if (!center) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var params = OpenLayers.Util.getParameters(this.base);
|
|
||||||
|
|
||||||
params.zoom = this.map.getZoom();
|
|
||||||
var lat = center.lat;
|
|
||||||
var lon = center.lon;
|
|
||||||
|
|
||||||
if (this.displayProjection) {
|
|
||||||
var mapPosition = OpenLayers.Projection.transform(
|
|
||||||
{ x: lon, y: lat },
|
|
||||||
this.map.getProjectionObject(),
|
|
||||||
this.displayProjection );
|
|
||||||
lon = mapPosition.x;
|
|
||||||
lat = mapPosition.y;
|
|
||||||
}
|
|
||||||
params.lat = Math.round(lat*100000)/100000;
|
|
||||||
params.lon = Math.round(lon*100000)/100000;
|
|
||||||
|
|
||||||
params.layers = '';
|
|
||||||
for (var i=0, len=this.map.layers.length; i<len; i++) {
|
|
||||||
var layer = this.map.layers[i];
|
|
||||||
|
|
||||||
if (layer.isBaseLayer) {
|
|
||||||
params.layers += (layer == this.map.baseLayer) ? "B" : "0";
|
|
||||||
} else {
|
|
||||||
params.layers += (layer.getVisibility()) ? "T" : "F";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var href = this.base;
|
var href = this.base;
|
||||||
if (href.indexOf('?') != -1) {
|
if (href.indexOf('?') != -1) {
|
||||||
href = href.substring( 0, href.indexOf('?') );
|
href = href.substring( 0, href.indexOf('?') );
|
||||||
}
|
}
|
||||||
|
|
||||||
href += '?' + OpenLayers.Util.getParameterString(params);
|
href += '?' + OpenLayers.Util.getParameterString(this.createParams());
|
||||||
this.element.href = href;
|
this.element.href = href;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIMethod: createParams
|
||||||
|
* Creates the parameters that need to be encoded into the permalink url.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* center - {<OpenLayers.LonLat>} center to encode in the permalink.
|
||||||
|
* Defaults to the current map center.
|
||||||
|
* zoom - {Integer} zoom level to encode in the permalink. Defaults to the
|
||||||
|
* current map zoom level.
|
||||||
|
* layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink.
|
||||||
|
* Defaults to the current map layers.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* {Object} Hash of parameters that will be url-encoded into the
|
||||||
|
* permalink.
|
||||||
|
*/
|
||||||
|
createParams: function(center, zoom, layers) {
|
||||||
|
center = center || this.map.getCenter();
|
||||||
|
zoom = zoom || this.map.getZoom();
|
||||||
|
layers = layers || this.map.layers;
|
||||||
|
|
||||||
|
var params = OpenLayers.Util.getParameters(this.base);
|
||||||
|
|
||||||
|
// If there's still no center, map is not initialized yet.
|
||||||
|
// Break out of this function, and simply return the params from the
|
||||||
|
// base link.
|
||||||
|
if (center) {
|
||||||
|
|
||||||
|
params.zoom = this.map.getZoom();
|
||||||
|
var lat = center.lat;
|
||||||
|
var lon = center.lon;
|
||||||
|
|
||||||
|
if (this.displayProjection) {
|
||||||
|
var mapPosition = OpenLayers.Projection.transform(
|
||||||
|
{ x: lon, y: lat },
|
||||||
|
this.map.getProjectionObject(),
|
||||||
|
this.displayProjection );
|
||||||
|
lon = mapPosition.x;
|
||||||
|
lat = mapPosition.y;
|
||||||
|
}
|
||||||
|
params.lat = Math.round(lat*100000)/100000;
|
||||||
|
params.lon = Math.round(lon*100000)/100000;
|
||||||
|
|
||||||
|
params.layers = '';
|
||||||
|
for (var i=0, len=this.map.layers.length; i<len; i++) {
|
||||||
|
var layer = this.map.layers[i];
|
||||||
|
|
||||||
|
if (layer.isBaseLayer) {
|
||||||
|
params.layers += (layer == this.map.baseLayer) ? "B" : "0";
|
||||||
|
} else {
|
||||||
|
params.layers += (layer.getVisibility()) ? "T" : "F";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
},
|
||||||
|
|
||||||
CLASS_NAME: "OpenLayers.Control.Permalink"
|
CLASS_NAME: "OpenLayers.Control.Permalink"
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -108,6 +108,32 @@
|
|||||||
OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
|
OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B';
|
||||||
t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base");
|
t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_Control_Permalink_customized(t) {
|
||||||
|
t.plan(2);
|
||||||
|
|
||||||
|
var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, {
|
||||||
|
CLASS_NAME: "CustomArgParser"
|
||||||
|
});
|
||||||
|
|
||||||
|
control = new OpenLayers.Control.Permalink(null, "./edit.html", {
|
||||||
|
argParserClass: argParserClass,
|
||||||
|
createParams: function(center, zoom, layers) {
|
||||||
|
var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments);
|
||||||
|
params.customParam = "foo";
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
map = new OpenLayers.Map('map');
|
||||||
|
layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
|
||||||
|
map.addLayer(layer);
|
||||||
|
if (!map.getCenter()) map.zoomToMaxExtent();
|
||||||
|
map.addControl(control);
|
||||||
|
map.pan(5, 0, {animate:false});
|
||||||
|
|
||||||
|
t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly.");
|
||||||
|
t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly.");
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
Reference in New Issue
Block a user