break permalink into two controls: permalink and argparser. make the arg parser a default control that is added to the map. furthermore, make it such that on addition to the map, the permalink will hunt through the maps controls to make sure there is an argparser control loaded, and if not it will load one itself. now we add a new parameter to the permalink suite: 'layer'. the application will now save the layer visibility information. finally, update tests so nothing breaks.
git-svn-id: http://svn.openlayers.org/trunk/openlayers@1634 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
113
lib/OpenLayers/Control/ArgParser.js
Normal file
113
lib/OpenLayers/Control/ArgParser.js
Normal file
@@ -0,0 +1,113 @@
|
||||
/* 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.ArgParser = OpenLayers.Class.create();
|
||||
OpenLayers.Control.ArgParser.prototype =
|
||||
OpenLayers.Util.extend( new OpenLayers.Control(), {
|
||||
|
||||
/** @type OpenLayers.LonLat */
|
||||
center: null,
|
||||
|
||||
/** @type int */
|
||||
zoom: null,
|
||||
|
||||
/** @type Array */
|
||||
layers: null,
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*
|
||||
* @param {DOMElement} element
|
||||
* @param {String} base
|
||||
*/
|
||||
initialize: function(element, base) {
|
||||
OpenLayers.Control.prototype.initialize.apply(this, arguments);
|
||||
},
|
||||
|
||||
/** Set the map property for the control.
|
||||
*
|
||||
* @param {OpenLayers.Map} map
|
||||
*/
|
||||
setMap: function(map) {
|
||||
OpenLayers.Control.prototype.setMap.apply(this, arguments);
|
||||
|
||||
//make sure we dont already have an arg parser attached
|
||||
for(var i=0; i< this.map.controls.length; i++) {
|
||||
var control = this.map.controls[i];
|
||||
if ( (control != this) &&
|
||||
(control.CLASS_NAME == "OpenLayers.Control.ArgParser") ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == this.map.controls.length) {
|
||||
|
||||
var args = OpenLayers.Util.getArgs();
|
||||
if (args.lat && args.lon) {
|
||||
this.center = new OpenLayers.LonLat(parseFloat(args.lon),
|
||||
parseFloat(args.lat));
|
||||
if (args.zoom) {
|
||||
this.zoom = parseInt(args.zoom);
|
||||
}
|
||||
|
||||
// when we add a new baselayer to see when we can set the center
|
||||
this.map.events.register('changebaselayer', this,
|
||||
this.setCenter);
|
||||
this.setCenter();
|
||||
}
|
||||
|
||||
if (args.layers) {
|
||||
this.layers = args.layers;
|
||||
|
||||
// when we add a new layer, set its visibility
|
||||
this.map.events.register('addlayer', this,
|
||||
this.configureLayers);
|
||||
this.configureLayers();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/** As soon as a baseLayer has been loaded, we center and zoom
|
||||
* ...and remove the handler.
|
||||
*/
|
||||
setCenter: function() {
|
||||
|
||||
if (this.map.baseLayer) {
|
||||
//dont need to listen for this one anymore
|
||||
this.map.events.unregister('changebaselayer', this,
|
||||
this.setCenter);
|
||||
|
||||
this.map.setCenter(this.center, this.zoom);
|
||||
}
|
||||
},
|
||||
|
||||
/** As soon as all the layers are loaded, cycle through them and
|
||||
* hide or show them.
|
||||
*/
|
||||
configureLayers: function() {
|
||||
|
||||
if (this.layers.length == this.map.layers.length) {
|
||||
this.map.events.unregister('addlayer', this, this.configureLayers);
|
||||
|
||||
for(var i=0; i < this.layers.length; i++) {
|
||||
|
||||
var layer = this.map.layers[i];
|
||||
var c = this.layers.charAt(i);
|
||||
|
||||
if (c == "B") {
|
||||
this.map.setBaseLayer(layer);
|
||||
} else if ( (c == "T") || (c == "F") ) {
|
||||
layer.setVisibility(c == "T");
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/** @final @type String */
|
||||
CLASS_NAME: "OpenLayers.Control.ArgParser"
|
||||
});
|
||||
Reference in New Issue
Block a user