Files
openlayers/lib/OpenLayers/Control/Permalink.js

104 lines
3.0 KiB
JavaScript

/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
* text of the license. */
/**
* @class
*
* @requires OpenLayers/Control.js
*/
OpenLayers.Control.Permalink = OpenLayers.Class.create();
OpenLayers.Control.Permalink.prototype =
OpenLayers.Util.extend( new OpenLayers.Control(), {
/** @type DOMElement */
element: null,
/** @type String */
base: '',
/**
* @constructor
*
* @param {DOMElement} element
* @param {String} base
*/
initialize: function(element, base) {
OpenLayers.Control.prototype.initialize.apply(this, arguments);
this.element = element;
if (base) {
this.base = base;
}
},
/** Set the map property for the control.
*
* @param {OpenLayers.Map} 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());
}
},
/**
* @type DOMElement
*/
draw: function() {
OpenLayers.Control.prototype.draw.apply(this, arguments);
if (!this.element) {
this.element = document.createElement("a");
this.div.style.right = "3px";
this.div.style.bottom = "3px";
this.div.style.left = "";
this.div.style.top = "";
this.div.style.display = "block";
this.div.style.position = "absolute";
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);
return this.div;
},
/**
*
*/
updateLink: function() {
var center = this.map.getCenter();
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 + "?" + lat + "&" + lon + "&" + zoom +
"&" + layers;
this.element.href = href;
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Control.Permalink"
});