[Commits] r2654 - in core/trunk/geoext: lib/GeoExt/widgets tests/lib/GeoExt/widgets

commits at geoext.org commits at geoext.org
Thu Apr 28 14:01:40 CEST 2011


Author: ahocevar
Date: 2011-04-28 14:01:40 +0200 (Thu, 28 Apr 2011)
New Revision: 2654

Modified:
   core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html
Log:
Making MapPanel initialization more robust, and adding new forceInitialExtent option to set the initial center/extent even if the MapPanel's map has no layers yet when initially rendered. r=bartvde (closes #419)

Modified: core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js	2011-04-28 11:58:50 UTC (rev 2653)
+++ core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js	2011-04-28 12:01:40 UTC (rev 2654)
@@ -72,7 +72,6 @@
      *  :class:`GeoExt.data.LayerRecord` objects.
      */
     layers: null,
-
     
     /** api: config[center]
      *  ``OpenLayers.LonLat or Array(Number)``  A location for the map center.  If
@@ -85,6 +84,21 @@
      */
     zoom: null,
 
+    /** api: config[extent]
+     *  ``OpenLayers.Bounds or Array(Number)``  An initial extent for the map (used
+     *  if center and zoom are not provided.  If an array, the first four items
+     *  should be minx, miny, maxx, maxy.
+     */
+    extent: null,
+    
+    /** api: config[forceInitialExtent]
+     *  ``Boolean`` If set to true, the map's initial extent (provided through
+     *  ``extent``, ``center`` or ``zoom``, or the map's maxExtent) will be set
+     *  even if the map or mapPanel has no layers configured when it is first
+     *  rendered. Default is false.
+     */
+    forceInitialExtent: false,
+
     /** api: config[prettyStateKeys]
      *  ``Boolean`` Set this to true if you want pretty strings in the MapPanel's
      *  state keys. More specifically, layer.name instead of layer.id will be used
@@ -94,13 +108,6 @@
      */
     prettyStateKeys: false,
 
-    /** api: config[extent]
-     *  ``OpenLayers.Bounds or Array(Number)``  An initial extent for the map (used
-     *  if center and zoom are not provided.  If an array, the first four items
-     *  should be minx, miny, maxx, maxy.
-     */
-    extent: null,
-
     /** private: property[stateEvents]
      *  ``Array(String)`` Array of state events
      */
@@ -284,11 +291,13 @@
 
         // record location and zoom level
         var center = this.map.getCenter();
-        state = {
+        // map may not be centered yet, because it may still have zero
+        // dimensions or no layers
+        state = center ? {
             x: center.lon,
             y: center.lat,
             zoom: this.map.getZoom()
-        };
+        } : {};
 
         // record layer visibility and opacity
         var i, l, layer, layerId, layers = this.map.layers;
@@ -322,18 +331,28 @@
 
         this.layers.bind(map);
 
-        if(map.layers.length > 0) {
-            if(this.center || this.zoom != null) {
-                // both do not have to be defined
-                map.setCenter(this.center, this.zoom);
-            } else if(this.extent) {
-                map.zoomToExtent(this.extent);
-            } else {
-                map.zoomToMaxExtent();
-            }
+        if (map.layers.length > 0) {
+            this.setInitialExtent();
+        } else if (this.forceInitialExtent == true) {
+            this.layers.on("add", this.setInitialExtent, this, {single: true});
         }
     },
     
+    /** private: method[setInitialExtent]
+     *  Sets the initial extent of this panel's map
+     */
+    setInitialExtent: function() {
+        var map = this.map;
+        if(this.center || this.zoom != null) {
+            // both do not have to be defined
+            map.setCenter(this.center, this.zoom);
+        } else if(this.extent) {
+            map.zoomToExtent(this.extent);
+        } else {
+            map.zoomToMaxExtent();
+        }
+    },
+    
     /** private: method[afterRender]
      *  Private method called after the panel has been rendered.
      */
@@ -344,14 +363,26 @@
         } else {
             this.ownerCt.on("move", this.updateMapSize, this);
             this.ownerCt.on({
-                "afterlayout": {
-                    fn: this.renderMap,
-                    scope: this,
-                    single: true
-                }
+                "afterlayout": this.afterLayout,
+                scope: this
             });
         }
     },
+    
+    /** private: method[afterLayout]
+     *  Private method called after owner container has been laid out until
+     *  this panel has dimensions greater than zero.
+     */
+    afterLayout: function() {
+        var width = this.getInnerWidth() -
+                                this.body.getBorderWidth("lr");
+        var height = this.getInnerHeight() -
+                                this.body.getBorderWidth("tb");
+        if (width > 0 && height > 0) {
+            this.ownerCt.un("afterlayout", this.afterLayout, this);
+            this.renderMap();
+        }
+    },
 
     /** private: method[onResize]
      *  Private method called after the panel has been resized.

Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html	2011-04-28 11:58:50 UTC (rev 2653)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html	2011-04-28 12:01:40 UTC (rev 2654)
@@ -7,6 +7,9 @@
     <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
 
     <script type="text/javascript">
+    
+        // the hidden span element that contains the test iframe
+        var hiddenSpan = window.parent.Test.AnotherWay._g_test_iframe.frameElement.parentNode;
        
         function createMap() {
             var map = new OpenLayers.Map();
@@ -101,7 +104,7 @@
 
         function test_extent(t) {
             
-            t.plan(3);
+            t.plan(6);
             var map, panel, log = {};
             
             map = createMap();
@@ -151,8 +154,40 @@
             t.eq(log.extent.toArray(), [1, 2, 3, 4], "map extent set with Bounds");
             // since we created the map, we destroy it
             map.destroy();
-            panel.destroy();            
+            panel.destroy();
             
+            map = createMap();
+            var container = new Ext.Panel({
+                renderTo: document.body,
+                layout: "fit",
+                width: 0,
+                height: 0,
+                items: [new GeoExt.MapPanel({
+                    map: map,
+                    extent: new OpenLayers.Bounds(1, 2, 3, 4)
+                })]
+            });
+            t.eq(map.getExtent(), null, "no map extent set for a zero dimension map");
+            // make the hidden span element visible, because setSize does not
+            // work in iframes that are inside a hidden container in FF and IE.
+            // The span will be hidden again in test_layout.
+            hiddenSpan.style.display = "";
+            container.setSize(100, 100);
+            container.doLayout();
+            t.ok(map.getExtent(), "map extent set when layout is finished");
+            // since we created the map, we destroy it
+            map.destroy();
+            panel.destroy();
+            
+            panel = new GeoExt.MapPanel({
+                renderTo: "mappanel",
+                forceInitialExtent: true,
+                height: 400,
+                width: 600,
+                extent: new OpenLayers.Bounds(1, 2, 3, 4)
+            });
+            panel.map.addLayer(new OpenLayers.Layer());
+            t.ok(panel.map.getExtent(), "map has an extent after layer is added");
         }
         
         function test_center(t) {
@@ -261,6 +296,8 @@
                     t.ok(layout, 1,
                          "the OpenLayers map is rendered once the container " +
                          "has its final dimensions");
+                    // hide the span again, which we made visible in test_extent
+                    hiddenSpan.style.display = "none";
                 },
                 allOverlays: true
             });



More information about the Commits mailing list