From 3b9ce4ca2dbacb6ff7473d8cea935bce826db771 Mon Sep 17 00:00:00 2001 From: Bart van den Eijnden Date: Fri, 17 Aug 2012 10:13:04 +0200 Subject: [PATCH] introduce new property on the Map called autoUpdateSize which lets applications take control of window resizing / device orientation changes if they want to --- lib/OpenLayers/Map.js | 51 ++++++++++++++++++++++++++----------------- tests/Map.html | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 20 deletions(-) diff --git a/lib/OpenLayers/Map.js b/lib/OpenLayers/Map.js index ab2cc1a928..3dd31f2632 100644 --- a/lib/OpenLayers/Map.js +++ b/lib/OpenLayers/Map.js @@ -376,6 +376,13 @@ OpenLayers.Map = OpenLayers.Class({ * Default is to fall through. */ fallThrough: true, + + /** + * APIProperty: autoUpdateSize + * {Boolean} Should OpenLayers automatically update the size of the map + * when the resize event is fired. Default is true. + */ + autoUpdateSize: true, /** * Property: panTween @@ -578,20 +585,22 @@ OpenLayers.Map = OpenLayers.Class({ if(this.eventListeners instanceof Object) { this.events.on(this.eventListeners); } - - // Because Mozilla does not support the "resize" event for elements - // other than "window", we need to put a hack here. - if (parseFloat(navigator.appVersion.split("MSIE")[1]) < 9) { - // If IE < 9, register the resize on the div - this.events.register("resize", this, this.updateSize); - } else { - // Else updateSize on catching the window's resize - // Note that this is ok, as updateSize() does nothing if the - // map's size has not actually changed. - this.updateSizeDestroy = OpenLayers.Function.bind(this.updateSize, - this); - OpenLayers.Event.observe(window, 'resize', - this.updateSizeDestroy); + + if (this.autoUpdateSize === true) { + // Because Mozilla does not support the "resize" event for elements + // other than "window", we need to put a hack here. + if (parseFloat(navigator.appVersion.split("MSIE")[1]) < 9) { + // If IE < 9, register the resize on the div + this.events.register("resize", this, this.updateSize); + } else { + // Else updateSize on catching the window's resize + // Note that this is ok, as updateSize() does nothing if the + // map's size has not actually changed. + this.updateSizeDestroy = OpenLayers.Function.bind(this.updateSize, + this); + OpenLayers.Event.observe(window, 'resize', + this.updateSizeDestroy); + } } // only append link stylesheet if the theme property is set @@ -744,12 +753,14 @@ OpenLayers.Map = OpenLayers.Class({ OpenLayers.Event.stopObserving(window, 'unload', this.unloadDestroy); this.unloadDestroy = null; - if (this.updateSizeDestroy) { - OpenLayers.Event.stopObserving(window, 'resize', - this.updateSizeDestroy); - } else { - this.events.unregister("resize", this, this.updateSize); - } + if (this.autoUpdateSize === true) { + if (this.updateSizeDestroy) { + OpenLayers.Event.stopObserving(window, 'resize', + this.updateSizeDestroy); + } else { + this.events.unregister("resize", this, this.updateSize); + } + } this.paddingForPopups = null; diff --git a/tests/Map.html b/tests/Map.html index 6323c249db..73fcbbbc87 100644 --- a/tests/Map.html +++ b/tests/Map.html @@ -2090,6 +2090,48 @@ t.ok(center.equals(new OpenLayers.LonLat(-13.25, 56)), "Center is correct and not equal to maxExtent's center"); } + function test_autoUpdateSize(t) { + t.plan(1); + OpenLayers.Event.unloadCache(); + var resizeListener = false; + var register = OpenLayers.Events.prototype.register; + OpenLayers.Events.prototype.register = function(name, el , func) { + if (name === 'resize') { + resizeListener = true; + } + }; + var map = new OpenLayers.Map({ + events: { + register: function(name, el, func) { + if (name === 'resize') { + resizeListener = true; + } + } + }, + autoUpdateSize: false, + div: 'map', + layers: [ + new OpenLayers.Layer('name', { + isBaseLayer: true, + wrapDateLine: true + }) + ] + }); + map.setCenter(new OpenLayers.LonLat(-1.3, 50.8), 4); + for (var key in OpenLayers.Event.observers) { + var obj = OpenLayers.Event.observers[key]; + for (var i=0, ii=obj.length; i