From 0b58117c06e45e3c5a7bc73f6791e0b8156d821d Mon Sep 17 00:00:00 2001 From: euzuro Date: Wed, 30 Jul 2008 00:36:26 +0000 Subject: [PATCH] zoomToMaxExtent() and getMaxExtent() should be smarter if a 'restrictedExtent' property is set on the map. (Closes #1134) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7596 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Map.js | 35 ++++++++++++++++++---- tests/Map.html | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index 4fa5c8e246..fbd0bce7b7 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -1695,13 +1695,25 @@ OpenLayers.Map = OpenLayers.Class({ /** * APIMethod: getMaxExtent + * + * Parameters: + * options - {Object} * + * Allowed Options: + * restricted - {Boolean} If true, returns restricted extent (if it is + * available.) + * * Returns: - * {} + * {} The maxExtent property as set on the current + * baselayer, unless the 'restricted' option is set, in which case + * the 'restrictedExtent' option from the map is returned (if it + * is set). */ - getMaxExtent: function () { + getMaxExtent: function (options) { var maxExtent = null; - if (this.baseLayer != null) { + if(options && options.restricted && this.restrictedExtent){ + maxExtent = this.restrictedExtent; + } else if (this.baseLayer != null) { maxExtent = this.baseLayer.maxExtent; } return maxExtent; @@ -1942,9 +1954,22 @@ OpenLayers.Map = OpenLayers.Class({ /** * APIMethod: zoomToMaxExtent * Zoom to the full extent and recenter. + * + * Parameters: + * options - + * + * Allowed Options: + * restricted - {Boolean} True to zoom to restricted extent if it is + * set. Defaults to true. */ - zoomToMaxExtent: function() { - this.zoomToExtent(this.getMaxExtent()); + zoomToMaxExtent: function(options) { + //restricted is true by default + var restricted = (options) ? options.restricted : true; + + var maxExtent = this.getMaxExtent({ + 'restricted': restricted + }); + this.zoomToExtent(maxExtent); }, /** diff --git a/tests/Map.html b/tests/Map.html index 6acc365388..89c56fabf7 100644 --- a/tests/Map.html +++ b/tests/Map.html @@ -1028,6 +1028,73 @@ t.eq( map.controls, null, "map.controls is null after destroy" ); t.eq( map.viewPortDiv, null, "map's viewportDiv nullified"); } + + function test_Map_getMaxExtent(t){ + t.plan(5); + + var options = null; + var map = {}; + + //null options, no baseLayer + var maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]); + t.eq(maxExtent, null, "null options, no baseLayer returns null"); + + //null options.restricted, no baseLayer + maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]); + t.eq(maxExtent, null, "null options.restricted, no baseLayer returns null"); + + //true options.restricted, null map.restrictedExtent no baseLayer + maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]); + t.eq(maxExtent, null, "true options.restricted, null map.restrictedExtent no baseLayer returns null"); + + //true options.restricted, valid map.restrictedExtent no baseLayer + options = { + 'restricted': true + }; + map.restrictedExtent = {}; + maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]); + t.ok(maxExtent == map.restrictedExtent, "true options.restricted, valid map.restrictedExtent no baseLayer returns map.restrictedExtent"); + + //null options, valid baseLayer + options = null; + map.baseLayer = { + 'maxExtent': {} + }; + var maxExtent = OpenLayers.Map.prototype.getMaxExtent.apply(map, [options]); + t.ok(maxExtent == map.baseLayer.maxExtent, "null options, valid baseLayer returns map.baseLayer.maxExtent"); + } + + function test_Map_zoomToMaxExtent(t){ + t.plan(4) + + gMaxExtent = {}; + + var map = { + 'getMaxExtent': function(options) { + gRestricted = options.restricted; + return gMaxExtent; + }, + 'zoomToExtent': function(extent) { + t.ok(extent == gMaxExtent, "zoomToExtent() always called on return from map.getMaxExtent()"); + } + }; + + //options is null + var options = null; + gRestricted = null; + OpenLayers.Map.prototype.zoomToMaxExtent.apply(map, [options]); + t.eq(gRestricted, true, "default 'restricted' passed to map.getMaxExtent() is true"); + + //valid options + options = { + 'restricted': {} + }; + gRestricted = null; + OpenLayers.Map.prototype.zoomToMaxExtent.apply(map, [options]); + t.ok(gRestricted == options.restricted, "when valid options argument, 'options.restricted' passed to map.getMaxExtent()"); + } + +