From 5364ce73e5fe3e6745151efc8c6ece5270b7362f Mon Sep 17 00:00:00 2001 From: euzuro Date: Fri, 6 Oct 2006 03:28:53 +0000 Subject: [PATCH] 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 --- lib/OpenLayers.js | 1 + lib/OpenLayers/Control/ArgParser.js | 113 +++++++++++++++++++++++++++ lib/OpenLayers/Control/Permalink.js | 86 +++++--------------- lib/OpenLayers/Map.js | 4 +- tests/test_Control_MouseToolbar.html | 6 +- tests/test_Control_PanZoom.html | 6 +- tests/test_Control_Permalink.html | 2 +- 7 files changed, 144 insertions(+), 74 deletions(-) create mode 100644 lib/OpenLayers/Control/ArgParser.js diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 12d6977a52..fc3ff3d753 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -93,6 +93,7 @@ if (typeof(_OPENLAYERS_SFL_) == "undefined") { "OpenLayers/Control/KeyboardDefaults.js", "OpenLayers/Control/PanZoom.js", "OpenLayers/Control/PanZoomBar.js", + "OpenLayers/Control/ArgParser.js", "OpenLayers/Control/Permalink.js", "OpenLayers/Control/Scale.js", "OpenLayers/Control/LayerSwitcher.js" diff --git a/lib/OpenLayers/Control/ArgParser.js b/lib/OpenLayers/Control/ArgParser.js new file mode 100644 index 0000000000..c54b7afe80 --- /dev/null +++ b/lib/OpenLayers/Control/ArgParser.js @@ -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" +}); diff --git a/lib/OpenLayers/Control/Permalink.js b/lib/OpenLayers/Control/Permalink.js index 345290a78f..a77716a8a6 100644 --- a/lib/OpenLayers/Control/Permalink.js +++ b/lib/OpenLayers/Control/Permalink.js @@ -16,15 +16,6 @@ OpenLayers.Control.Permalink.prototype = /** @type String */ base: '', - - /** @type OpenLayers.LonLat */ - center: null, - - /** @type int */ - zoom: null, - - /** @type Array */ - layers: null, /** * @constructor @@ -40,33 +31,32 @@ OpenLayers.Control.Permalink.prototype = } }, + /** 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); - 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(); - } - if (!this.element) { this.element = document.createElement("a"); this.div.style.right = "3px"; @@ -108,42 +98,6 @@ OpenLayers.Control.Permalink.prototype = this.element.href = href; }, - /** 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.Permalink" }); diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 4c31590236..4dcf347f02 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -164,7 +164,9 @@ OpenLayers.Map.prototype = { if (this.controls == null) { if (OpenLayers.Control != null) { // running full or lite? this.controls = [ new OpenLayers.Control.MouseDefaults(), - new OpenLayers.Control.PanZoom()]; + new OpenLayers.Control.PanZoom(), + new OpenLayers.Control.ArgParser() + ]; } else { this.controls = []; } diff --git a/tests/test_Control_MouseToolbar.html b/tests/test_Control_MouseToolbar.html index e65f77da97..85fea77eba 100644 --- a/tests/test_Control_MouseToolbar.html +++ b/tests/test_Control_MouseToolbar.html @@ -17,9 +17,9 @@ t.ok( map instanceof OpenLayers.Map, "new OpenLayers.Map creates map" ); map.addControl(control); t.ok( control.map === map, "Control.map is set to the map object" ); - t.ok( map.controls[2] === control, "map.controls contains control" ); - t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Control div zIndexed properly" ); - t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Viewport div contains control div" ); + t.ok( map.controls[3] === control, "map.controls contains control" ); + t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Control div zIndexed properly" ); + t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Viewport div contains control div" ); t.eq( control.div.style.top, "6px", "Control div top located correctly by default"); } diff --git a/tests/test_Control_PanZoom.html b/tests/test_Control_PanZoom.html index e2c29c3bc7..205d818264 100644 --- a/tests/test_Control_PanZoom.html +++ b/tests/test_Control_PanZoom.html @@ -17,9 +17,9 @@ t.ok( map instanceof OpenLayers.Map, "new OpenLayers.Map creates map" ); map.addControl(control); t.ok( control.map === map, "Control.map is set to the map object" ); - t.ok( map.controls[2] === control, "map.controls contains control" ); - t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Control div zIndexed properly" ); - t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Viewport div contains control div" ); + t.ok( map.controls[3] === control, "map.controls contains control" ); + t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Control div zIndexed properly" ); + t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Viewport div contains control div" ); t.eq( control.div.style.top, "4px", "Control div top located correctly by default"); var control2 = new OpenLayers.Control.PanZoom(); diff --git a/tests/test_Control_Permalink.html b/tests/test_Control_Permalink.html index c83c8d1433..57bf90988d 100644 --- a/tests/test_Control_Permalink.html +++ b/tests/test_Control_Permalink.html @@ -46,7 +46,7 @@ t.ok( control instanceof OpenLayers.Control.Permalink, "new OpenLayers.Control returns object" ); map = new OpenLayers.Map($('map')); map.addControl(control); - t.eq(map.controls[2].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." ); + t.eq(map.controls[3].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." ); } // -->