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

commits at geoext.org commits at geoext.org
Fri Jun 5 22:23:38 CEST 2009


Author: ahocevar
Date: 2009-06-05 22:23:38 +0200 (Fri, 05 Jun 2009)
New Revision: 1000

Modified:
   core/trunk/geoext/examples/popup.js
   core/trunk/geoext/lib/GeoExt/widgets/Popup.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html
Log:
make popup behave like an Ext.Window, i.e. show it using popup.show() 
instead of adding it as item to the map panel. r=tschaub (closes #80) 
(closes #8)


Modified: core/trunk/geoext/examples/popup.js
===================================================================
--- core/trunk/geoext/examples/popup.js	2009-06-05 20:22:28 UTC (rev 999)
+++ core/trunk/geoext/examples/popup.js	2009-06-05 20:23:38 UTC (rev 1000)
@@ -36,7 +36,7 @@
                 }
             }
         });
-        mapPanel.add(popup);
+        popup.show();
     }
 
     // create popup on "featureselected"

Modified: core/trunk/geoext/lib/GeoExt/widgets/Popup.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/Popup.js	2009-06-05 20:22:28 UTC (rev 999)
+++ core/trunk/geoext/lib/GeoExt/widgets/Popup.js	2009-06-05 20:23:38 UTC (rev 1000)
@@ -44,6 +44,14 @@
      */
     anchored: true,
 
+    /** api: config[map]
+     *  ``OpenLayers.Map`` or :class:`GeoExt.MapPanel`
+     *  The map this popup will be anchored to (only required if ``anchored``
+     *  is set to true and the map cannot be derived from the ``feature``'s
+     *  layer.
+     */
+    map: null,
+
     /** api: config[panIn]
      *  ``Boolean`` The popup should pan the map so that the popup is
      *  fully in view when it is rendered.  Default is ``true``.
@@ -108,10 +116,19 @@
     /** private: method[initComponent]
      *  Initializes the popup.
      */
-    initComponent: function() {        
+    initComponent: function() {
+        if(this.map instanceof GeoExt.MapPanel) {
+            this.map = this.map.map;
+        }
+        if(!this.map && this.feature && this.feature.layer) {
+            this.map = this.feature.layer.map;
+        }
         if (!this.feature && this.lonlat) {
             this.feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(this.lonlat.lon, this.lonlat.lat));
         }
+        if(this.anchored) {
+            this.addAnchorEvents();
+        }
 
         this.baseCls = this.popupCls + " " + this.baseCls;
 
@@ -146,38 +163,19 @@
         GeoExt.Popup.superclass.initTools.call(this);
     },
 
-    /** private: method[addToMapPanel]
-     *  :param mapPanel: :class:`MapPanel` The panel to which this popup should
-     *      be added.
-     *  
-     *  Adds this popup to a :class:`MapPanel`.  Assumes that the
-     *  MapPanel's map is already initialized and that the
-     *  Popup's feature is on the map.  This method is called by the MapPanel's
-     *  add method.
+    /** private: method[show]
+     *  Override.
      */
-    addToMapPanel: function(mapPanel) {
-        this.mapPanel = mapPanel;
-        this.map = this.mapPanel.map;
-        
-        mapPanel.on({
-            "add": {
-                fn: function() {
-                    mapPanel.doLayout();
-                    this.position();
-                    if(this.anchored) {
-                        this.anchorPopup();
-                    }
-                    this.show();
-                    if(this.panIn) {
-                        this.panIntoView();
-                    }
-                },
-                single: true,
-                scope: this
+    show: function() {
+        GeoExt.Popup.superclass.show.apply(this, arguments);
+        if(this.anchored) {
+            this.position();
+            if(this.panIn && !this._mapmove) {
+                this.panIntoView();
             }
-        });
+        }
     },
-
+    
     /** api: method[setSize]
      *  :param w: ``Integer``
      *  :param h: ``Integer``
@@ -202,21 +200,32 @@
      */
     position: function() {
         var centerLonLat = this.feature.geometry.getBounds().getCenterLonLat();
-        var centerPx = this.map.getViewPortPxFromLonLat(centerLonLat);
 
-        //This works for positioning with the anchor on the bottom.
-        
-        //Will have to functionalize this out later and allow
-        //for other positions relative to the feature.
-        var anchorSelector = "div." + this.ancCls;
+        if(this._mapMove === true) {
+            var visible = this.map.getExtent().containsLonLat(centerLonLat);
+            if(visible !== this.isVisible()) {
+                this.setVisible(visible);
+            }
+        }
 
-        var dx = this.anc.down(anchorSelector).getLeft(true) +
-                            this.anc.down(anchorSelector).getWidth() / 2;
-        var dy = this.el.getHeight();
-
-        //Assuming for now that the map viewport takes up
-        //the entire area of the MapPanel
-        this.setPosition(centerPx.x - dx, centerPx.y - dy);
+        if(this.isVisible()) {
+            var centerPx = this.map.getViewPortPxFromLonLat(centerLonLat);
+            var mapBox = Ext.fly(this.map.div).getBox(); 
+    
+            //This works for positioning with the anchor on the bottom.
+            
+            //Will have to functionalize this out later and allow
+            //for other positions relative to the feature.
+            var anchorSelector = "div." + this.ancCls;
+    
+            var dx = this.anc.down(anchorSelector).getLeft(true) +
+                                this.anc.down(anchorSelector).getWidth() / 2;
+            var dy = this.el.getHeight();
+    
+            //Assuming for now that the map viewport takes up
+            //the entire area of the MapPanel
+            this.setPosition(centerPx.x + mapBox.x - dx, centerPx.y + mapBox.y - dy);
+        }
     },
 
     /** private: method[getAnchorElement]
@@ -228,30 +237,13 @@
         return anc;
     },
 
-    /** private: method[anchorPopup]
-     *  Anchors a popup to its feature by registering listeners that reposition
-     *  the popup when the map is moved.
-     */
-    anchorPopup: function() {
-        this.map.events.on({
-            "move" : this.position,
-            scope : this            
-        });
-
-        this.on({
-            "resize": this.position,
-            "collapse": this.position,
-            "expand": this.position,
-            scope: this
-        });
-    },
-
     /** private: method[unanchorPopup]
      *  Unanchors a popup from its feature.  This removes the popup from its
      *  MapPanel and adds it to the page body.
      */
     unanchorPopup: function() {
-
+        this.removeAnchorEvents();
+        
         //make the window draggable
         this.draggable = true;
         this.header.addClass("x-window-draggable");
@@ -263,70 +255,24 @@
 
         //hide unpin tool
         this.tools.unpin.hide();
-
-        //keep track of whether the popup has been collapsed
-        var collapsed = this.collapsed;
-
-        //Steps to move this window out to the body
-        //TODO: Make 'unpinned' container configurable
-        this.mapPanel.remove(this, false);
-
-        this.container = Ext.getBody();
-        
-        var xy = this.getPosition();
-        this.hide();
-        this.el.appendTo(Ext.getBody());
-        this.setPagePosition(xy[0],xy[1]);
-        this.show();
-
-        //recollapse if it was collapsed before
-        if(collapsed) {
-            this.collapse();
-        }
     },
 
-    /** private: method[removeFromMapPanel]
-     *  Utility method for unbinding events that call for popup repositioning.
-     *  Called from the panel during panel.remove(popup).
-     */
-    removeFromMapPanel: function() {
-        if(this.map && this.map.events) {
-            //stop position with feature
-            this.map.events.un({
-                "move" : this.position,
-                scope : this
-            });
-        }
-
-        this.un("resize", this.position, this);
-        this.un("collapse", this.position, this);
-        this.un("expand", this.position, this);
-
-        delete this.mapPanel;
-        delete this.map;
-    },
-
     /** private: method[panIntoView]
      *  Pans the MapPanel's map so that an anchored popup can come entirely
      *  into view, with padding specified as per normal OpenLayers.Map popup
      *  padding.
      */ 
     panIntoView: function() {
-        if(!this.anchored) {
-            /*
-             * If it's not anchored, panning the map won't put popup into view
-             */
-            return;
-        }
-        this.position();
-        
-        var centerLonLat = this.feature.geometry.getBounds().getCenterLonLat(); 
+        var centerLonLat = this.feature.geometry.getBounds().getCenterLonLat();
         var centerPx = this.map.getViewPortPxFromLonLat(centerLonLat);
+        var mapBox = Ext.fly(this.map.div).getBox(); 
 
         //assumed viewport takes up whole body element of map panel
         var popupPos =  this.getPosition(true);
+        popupPos[0] -= mapBox.x;
+        popupPos[1] -= mapBox.y;
        
-        var panelSize = [this.mapPanel.getInnerWidth(), this.mapPanel.getInnerHeight()]; // [X,Y]
+        var panelSize = [mapBox.width, mapBox.height]; // [X,Y]
 
         var popupSize = this.getSize();
 
@@ -354,15 +300,56 @@
 
         this.map.pan(dx, dy);
     },
+    
+    /** private: method[onMapMove]
+     */
+    onMapMove: function() {
+        this._mapMove = true;
+        this.position();
+        delete this._mapMove;
+    },
+    
+    /** private: method[addAnchorEvents]
+     */
+    addAnchorEvents: function() {
+        this.map.events.on({
+            "move" : this.onMapMove,
+            scope : this            
+        });
+        
+        this.on({
+            "resize": this.position,
+            "collapse": this.position,
+            "expand": this.position,
+            scope: this
+        });
+    },
+    
+    /** private: method[removeAnchorEvents]
+     */
+    removeAnchorEvents: function() {
+        //stop position with feature
+        this.map.events.un({
+            "move" : this.onMapMove,
+            scope : this
+        });
 
+        this.un("resize", this.position, this);
+        this.un("collapse", this.position, this);
+        this.un("expand", this.position, this);
+
+    },
+
     /** private: method[beforeDestroy]
      *  Cleanup events before destroying the popup.
      */
     beforeDestroy: function() {
-        this.removeFromMapPanel();
+        if(this.anchored) {
+            this.removeAnchorEvents();
+        }
         GeoExt.Popup.superclass.beforeDestroy.call(this);
     }
 });
 
-/** api: xtype = gx_mappanel */
+/** api: xtype = gx_popup */
 Ext.reg('gx_popup', GeoExt.Popup); 

Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html	2009-06-05 20:22:28 UTC (rev 999)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html	2009-06-05 20:23:38 UTC (rev 1000)
@@ -29,9 +29,13 @@
             });
 
             var feature = new OpenLayers.Feature.Vector(
-                new OpenLayers.Geometry.Point(100,50),
+                new OpenLayers.Geometry.Point(5,45),
                 {name: "My Feature"}
             );
+            feature.layer = {
+                map: map,
+                removeFeatures: function() {}
+            };
 
             return {
                 feature: feature,
@@ -63,9 +67,9 @@
 
             var pop = popup(context.feature);
 
-            context.mapPanel.add(pop);
+            pop.show();
 
-            t.ok(context.mapPanel.el.child("div." + pop.popupCls),"Map panel contains popup");
+            t.ok(Ext.getBody().child("div." + pop.popupCls),"viewport contains popup");
             
             tearDown(context);
         }
@@ -77,7 +81,7 @@
 
             var pop = popup(context.feature);
 
-            context.mapPanel.add(pop);
+            pop.show();
 
             pop.on({
                 'move' : function(c,x,y){
@@ -102,13 +106,13 @@
 
 
         function test_unanchorPopup(t) {
-            t.plan(6);
+            t.plan(4);
 
             var context = setupContext();
 
             var pop = popup(context.feature, context.mapPanel);
 
-            context.mapPanel.add(pop);
+            pop.show();
         
             pop.collapse();
 
@@ -120,8 +124,6 @@
 
             t.ok(!pop.getAnchorElement(),"Anchor element removed");
             t.ok(!this.collapsed, "Preserved collapsed state");
-            t.ok(!context.mapPanel.el.child("div." + pop.popupCls),"Map panel does not contain popup");
-            t.ok(Ext.getBody().child("div." + pop.popupCls),"Document body contains popup element");
             t.eq(origPos[0],newPos[0],"Popup remains in same position (X)");
             t.eq(origPos[1],newPos[1],"Popup remains in same position (Y)");
 
@@ -151,7 +153,7 @@
 
             var context = setupContext();
             var pop = popup(context.feature);
-            context.mapPanel.add(pop);
+            pop.show();
             
             var called = false;
             pop.on({



More information about the Commits mailing list