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

commits at geoext.org commits at geoext.org
Thu May 28 13:57:49 CEST 2009


Author: pgiraud
Date: 2009-05-28 13:57:49 +0200 (Thu, 28 May 2009)
New Revision: 877

Modified:
   core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html
Log:
if the panel is an item of a container, don't render the map till the container layout is complete, this prevents
issues with border layouts (Closes #60), thanks to ahocevar, tschaub and elemoine for their contribution on that
ticket



Modified: core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js	2009-05-28 03:49:48 UTC (rev 876)
+++ core/trunk/geoext/lib/GeoExt/widgets/MapPanel.js	2009-05-28 11:57:49 UTC (rev 877)
@@ -125,29 +125,38 @@
     },
     
     /** private: method[updateMapSize]
-     *  Tell the map that it needs to recaculate its size and position.
+     *  Tell the map that it needs to recalculate its size and position.
      */
     updateMapSize: function() {
         if(this.map) {
             this.map.updateSize();
         }
     },
-    
-    /** private: method[onRender]
-     *  Private method called after the panel has been
-     *  rendered.
+
+    /** private: method[renderMap]
+     *  Private method called after the panel has been rendered or after it
+     *  has been laid out by its parent's layout.
      */
-    onRender: function() {
-        GeoExt.MapPanel.superclass.onRender.apply(this, arguments);
-        this.map.render(this.body.dom);
-        if(this.map.layers.length > 0) {
+    renderMap: function() {
+        var map = this.map;
+
+        // hack: prevent map.updateSize (called from within map.render) from 
+        // zooming to the map extent. This hack is a workaround for 
+        // <http://trac.openlayers.org/ticket/2105> and must be
+        // removed once this ticket is closed.
+        var setCenter = map.setCenter;
+        map.setCenter = function() {};
+        map.render(this.body.dom);
+        map.setCenter = setCenter;
+
+        if(map.layers.length > 0) {
             if(this.center) {
                 // zoom does not have to be defined
-                this.map.setCenter(this.center, this.zoom);
-            }  else if(this.extent) {
-                this.map.zoomToExtent(this.extent);
+                map.setCenter(this.center, this.zoom);
+            } else if(this.extent) {
+                map.zoomToExtent(this.extent);
             } else {
-                this.map.zoomToMaxExtent();
+                map.zoomToMaxExtent();
             }
         }
     },
@@ -157,10 +166,19 @@
      */
     afterRender: function() {
         GeoExt.MapPanel.superclass.afterRender.apply(this, arguments);
-        if(this.ownerCt) {
+        if(!this.ownerCt) {
+            this.renderMap();
+        } else {
             this.ownerCt.on("move", this.updateMapSize, this);
+            this.ownerCt.on({
+                "afterlayout": {
+                    fn: this.renderMap,
+                    scope: this,
+                    single: true
+                }
+            });
         }
-    },    
+    },
 
     /** 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	2009-05-28 03:49:48 UTC (rev 876)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/MapPanel.html	2009-05-28 11:57:49 UTC (rev 877)
@@ -12,14 +12,24 @@
             var map = new OpenLayers.Map();
             var layer = new OpenLayers.Layer("test", {isBaseLayer: true});
             map.addLayer(layer);
+            // add a vector layer, which would fail onmapresize if we render
+            // the map before the panel has a layout.
+            map.addLayer(new OpenLayers.Layer.Vector("vector layer"));
             return map;
         }
 
         function test_mappanel(t) {
-            t.plan(3)
+            t.plan(4)
 
+            var moveToCnt;
+
             var map = createMap();
-            
+            map.moveTo = function() {
+                moveToCnt++;
+                OpenLayers.Map.prototype.moveTo.apply(this, arguments);
+            };
+
+            moveToCnt = 0;
             var mapPanel = new GeoExt.MapPanel({
                 // panel options
                 id: "map-panel",
@@ -32,6 +42,7 @@
                 center: new OpenLayers.LonLat(5, 45),
                 zoom: 4
             });
+            t.eq(moveToCnt, 1, "map.moveTo called exactly once");
             t.eq(mapPanel.map.getCenter().toString(), "lon=5,lat=45", "Map center set correctly");
             t.eq(mapPanel.map.getZoom(), 4, "Zoom set correctly");
             t.eq(GeoExt.MapPanel.guess().id, mapPanel.id, "MapPanel guessed correctly");
@@ -82,10 +93,12 @@
             map = createMap();
             map.zoomToExtent = function(extent) {
                 log.extent = extent;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 extent: [1, 2, 3, 4]
             });            
             t.eq(log.extent.toArray(), [1, 2, 3, 4], "map extent set with array");
@@ -97,10 +110,12 @@
             map = createMap();
             map.zoomToExtent = function(extent) {
                 log.extent = extent;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 extent: "1, 2, 3, 4"
             });
             t.eq(log.extent.toArray(), [1, 2, 3, 4], "map extent set with string");
@@ -111,10 +126,12 @@
             map = createMap();
             map.zoomToExtent = function(extent) {
                 log.extent = extent;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 extent: new OpenLayers.Bounds(1, 2, 3, 4)
             });
             t.eq(log.extent.toArray(), [1, 2, 3, 4], "map extent set with Bounds");
@@ -132,10 +149,12 @@
             map = createMap();
             map.setCenter = function(center) {
                 log.center = center;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 center: [1, 2]
             });            
             t.eq(log.center.toString(), "lon=1,lat=2", "map center set with array");
@@ -147,10 +166,12 @@
             map = createMap();
             map.setCenter = function(center) {
                 log.center = center;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 center: "1, 2"
             });            
             t.eq(log.center.toString(), "lon=1,lat=2", "map center set with string");
@@ -162,10 +183,12 @@
             map = createMap();
             map.setCenter = function(center) {
                 log.center = center;
-            }
+            };
             panel = new GeoExt.MapPanel({
                 renderTo: "mappanel",
                 map: map,
+                height: 400,
+                width: 600,
                 center: new OpenLayers.LonLat(1, 2)
             });            
             t.eq(log.center.toString(), "lon=1,lat=2", "map center set with LonLat");
@@ -213,7 +236,43 @@
             
         }
 
+        function test_layout(t) {
+            t.plan(1);
 
+            var map, panel, layout = 0;
+
+            map = new OpenLayers.Map({
+                render: function() {
+                    OpenLayers.Map.prototype.render.apply(this, arguments);
+                    t.ok(layout, 1,
+                         "the OpenLayers map is rendered once the container " +
+                         "has its final dimensions");
+                },
+                allOverlays: true
+            });
+            
+            panel = new Ext.Panel({
+                layout: "border",
+                renderTo: "mappanel",
+                listeners: {
+                    afterlayout: function() {
+                        layout++;
+                    }
+                },
+                items: [{
+                    region: "center",
+                    xtype: "gx_mappanel",
+                    map: map,
+                    layers: [
+                        new OpenLayers.Layer("")
+                    ]
+                }]
+            });
+
+            panel.destroy();
+        }
+
+
     </script>
   <body>
     <div id="mappanel"></div>



More information about the Commits mailing list