Files
openlayers/lib/OpenLayers/Control/Permalink.js
crschmidt f505542fbe "The Permalink control passes an element as the first parameter, however, the
initialize.apply is called with the arguments object, which causes an error if
an element is passed into the control." Also applied to Scale. Now fixed
with review from e-dog. (Closes #978) Fixes the only problem I found with 
migrating the Boston freemap to 2.5.


git-svn-id: http://svn.openlayers.org/trunk/openlayers@4333 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
2007-09-16 15:59:11 +00:00

148 lines
4.2 KiB
JavaScript

/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
* See http://svn.openlayers.org/trunk/openlayers/repository-license.txt
* for the full text of the license. */
/**
* @requires OpenLayers/Control.js
*
* Class: OpenLayers.Control.Permalink
*
* Inherits from:
* - <OpenLayers.Control>
*/
OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, {
/**
* Property: element
* {DOMElement}
*/
element: null,
/**
* APIProperty: base
* {String}
*/
base: '',
/**
* Constructor: OpenLayers.Control.Permalink
*
* Parameters:
* element - {DOMElement}
* base - {String}
* options - {Object} options to the control.
*/
initialize: function(element, base, options) {
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.element = OpenLayers.Util.getElement(element);
this.base = base || document.location.href;
},
/**
* APIMethod: destroy
*/
destroy: function() {
if (this.element.parentNode == this.div) {
this.div.removeChild(this.element);
}
this.element = null;
this.map.events.unregister('moveend', this, this.updateLink);
OpenLayers.Control.prototype.destroy.apply(this, arguments);
},
/**
* Method: setMap
* Set the map property for the control.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
setMap: function(map) {
OpenLayers.Control.prototype.setMap.apply(this, arguments);
//make sure we have an arg parser attached
for(var i=0; i< this.map.controls.length; i++) {
var control = this.map.controls[i];
if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") {
break;
}
}
if (i == this.map.controls.length) {
this.map.addControl(new OpenLayers.Control.ArgParser());
}
},
/**
* Method: draw
*
* Returns:
* {DOMElement}
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
if (!this.element) {
this.div.className = this.displayClass;
this.element = document.createElement("a");
this.element.style.fontSize="smaller";
this.element.innerHTML = "Permalink";
this.element.href="";
this.div.appendChild(this.element);
}
this.map.events.register('moveend', this, this.updateLink);
this.map.events.register('changelayer', this, this.updateLink);
this.map.events.register('changebaselayer', this, this.updateLink);
return this.div;
},
/**
* Method: updateLink
*/
updateLink: function() {
var center = this.map.getCenter();
// Map not initialized yet. Break out of this function.
if (!center) {
return;
}
var zoom = "zoom=" + this.map.getZoom();
var lat = "lat=" + Math.round(center.lat*100000)/100000;
var lon = "lon=" + Math.round(center.lon*100000)/100000;
var layers = "layers=";
for(var i=0; i< this.map.layers.length; i++) {
var layer = this.map.layers[i];
if (layer.isBaseLayer) {
layers += (layer == this.map.baseLayer) ? "B" : "0";
} else {
layers += (layer.getVisibility()) ? "T" : "F";
}
}
var href = this.base;
var paramsString = lat + "&" + lon + "&" + zoom + "&" + layers;
var lastServerChar = href.charAt(href.length - 1);
if ((lastServerChar == "&") || (lastServerChar == "?")) {
href += paramsString;
} else {
if (href.indexOf('?') == -1) {
//serverPath has no ? -- add one
href += '?' + paramsString;
} else {
//serverPath contains ?, so must already have paramsString at the end
href += '&' + paramsString;
}
}
this.element.href = href;
},
CLASS_NAME: "OpenLayers.Control.Permalink"
});