From 6f73f7059fcfd5e6ae08c2f435d067259bda4eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Mon, 23 Aug 2010 12:28:57 +0000 Subject: [PATCH] make Layer.addOptions call initResolutions if necessary, r=bartvde (closes #2360) git-svn-id: http://svn.openlayers.org/trunk/openlayers@10680 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Layer.js | 42 +++++++++++++++++++---- tests/Layer.html | 74 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 105 insertions(+), 11 deletions(-) diff --git a/lib/OpenLayers/Layer.js b/lib/OpenLayers/Layer.js index 5b459c726e..988ee58284 100644 --- a/lib/OpenLayers/Layer.js +++ b/lib/OpenLayers/Layer.js @@ -312,7 +312,7 @@ OpenLayers.Layer = OpenLayers.Class({ * transitionEffect values. */ SUPPORTED_TRANSITIONS: ['resize'], - + /** * Property: metadata * {Object} This object can be used to store additional information on a @@ -456,18 +456,46 @@ OpenLayers.Layer = OpenLayers.Class({ * newOptions - {Object} */ addOptions: function (newOptions) { - + if (this.options == null) { this.options = {}; } - + // update our copy for clone OpenLayers.Util.extend(this.options, newOptions); // add new options to this OpenLayers.Util.extend(this, newOptions); + + // make sure this.projection references a projection object + if(typeof this.projection == "string") { + this.projection = new OpenLayers.Projection(this.projection); + } + + // get the units from the projection, if we have a projection + // and it it has units + if(this.projection && this.projection.getUnits()) { + this.units = this.projection.getUnits(); + } + + // re-initialize resolutions if necessary, i.e. if any of the + // properties of the "properties" array defined below is set + // in the new options + if(this.map) { + var properties = this.RESOLUTION_PROPERTIES.concat( + ["projection", "units", "minExtent", "maxExtent"] + ); + for(var o in newOptions) { + if(newOptions.hasOwnProperty(o) && + OpenLayers.Util.indexOf(properties, o) >= 0) { + + this.initResolutions(); + break; + } + } + } }, - + /** * APIMethod: onMapResize * This function can be implemented by subclasses @@ -542,12 +570,12 @@ OpenLayers.Layer = OpenLayers.Class({ // been set this.maxExtent = this.maxExtent || this.map.maxExtent; this.minExtent = this.minExtent || this.map.minExtent; + this.projection = this.projection || this.map.projection; - - if (this.projection && typeof this.projection == "string") { + if (typeof this.projection == "string") { this.projection = new OpenLayers.Projection(this.projection); } - + // Check the projection to see if we can get units -- if not, refer // to properties. this.units = this.projection.getUnits() || diff --git a/tests/Layer.html b/tests/Layer.html index d7af3240cf..9938ac7e2b 100644 --- a/tests/Layer.html +++ b/tests/Layer.html @@ -82,26 +82,92 @@ function test_Layer_addOptions (t) { - t.plan( 4 ); + t.plan( 19 ); + var map = new OpenLayers.Map("map"); var options = { chicken: 151, foo: "bar" }; var layer = new OpenLayers.Layer('Test Layer', options); + map.addLayer(layer); layer.addOptions({bark:55, chicken: 171}); - t.eq(layer.bark, 55, "addOptions() assigns new option correctly to Layer"); t.eq(layer.options.bark, 55, "addOptions() adds new option correctly to backup"); t.eq(layer.chicken, 171, "addOptions() overwrites option correctly to Layer"); t.eq(layer.options.chicken, 171, "addOptions() overwrites option correctly to backup"); + var log; + layer.initResolutions = function() { + log++; + }; + log = 0; + layer.addOptions({bark: 56}); + t.eq(log, 0, "addOptions doesn't call initResolutions when not given a resolution option"); + + log = 0; + layer.addOptions({scales: [1, 2]}); + t.eq(log, 1, "addOptions calls initResolutions when given scales"); + + log = 0; + layer.addOptions({resolutions: [1, 2]}); + t.eq(log, 1, "addOptions calls initResolutions when given resolutions"); + + log = 0; + layer.addOptions({minScale: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given minScale"); + + log = 0; + layer.addOptions({maxScale: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given maxScale"); + + log = 0; + layer.addOptions({minResolution: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given minResolution"); + + log = 0; + layer.addOptions({maxResolution: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given maxResolution"); + + log = 0; + layer.addOptions({numZoomLevels: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given numZoomLevels"); + + log = 0; + layer.addOptions({maxZoomLevel: 4}); + t.eq(log, 1, "addOptions calls initResolutions when given maxZoomLevel"); + + log = 0; + layer.addOptions({projection: new OpenLayers.Projection("EPSG:900913")}); + t.eq(log, 1, "addOptions calls initResolutions when given projection"); + + log = 0; + layer.addOptions({units: "m"}); + t.eq(log, 1, "addOptions calls initResolutions when given units"); + + log = 0; + layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)}); + t.eq(log, 1, "addOptions calls initResolutions when given minExtent"); + + log = 0; + layer.addOptions({maxExtent: new OpenLayers.Bounds(0, 0, 0, 0)}); + t.eq(log, 1, "addOptions calls initResolutions when given maxExtent"); + + layer.projection = null; + layer.addOptions({projection: "EPSG:900913"}); + t.ok(layer.projection instanceof OpenLayers.Projection, + "addOptions creates a Projection object when given a projection string"); + + map.removeLayer(layer); + log = 0; + layer.addOptions({minExtent: new OpenLayers.Bounds(0, 0, 0, 0)}); + t.eq(log, 0, "addOptions doesn't call initResolutions when layer is not in map"); } function test_Layer_StandardOptionsAccessors (t) { t.plan( 4 ); - var projection = "chicken"; + var projection = "EPSG:4326"; var maxExtent = new OpenLayers.Bounds(50,50,100,100); var maxResolution = 1.5726; var numZoomLevels = 11; @@ -114,7 +180,7 @@ var layer = new OpenLayers.Layer('Test Layer', options); - t.eq(layer.projection, projection, "projection set correctly"); + t.eq(layer.projection.getCode(), projection, "projection set correctly"); t.ok(layer.maxExtent.equals(maxExtent), "maxExtent set correctly"); t.eq(layer.maxResolution, maxResolution, "maxResolution set correctly"); t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly");