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

commits at geoext.org commits at geoext.org
Mon Sep 13 15:14:39 CEST 2010


Author: ahocevar
Date: 2010-09-13 15:14:39 +0200 (Mon, 13 Sep 2010)
New Revision: 2343

Modified:
   core/trunk/geoext/examples/popup-more.js
   core/trunk/geoext/examples/popup.js
   core/trunk/geoext/lib/GeoExt/widgets/Popup.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html
Log:
Simplified the popup API: location can now be configured with features, geometries, lonlats and pixels. Sorry fredj that you had to make the exmaple changes - I had them, but forgot to include them in the original patch. r=fredj (closes #200)


Modified: core/trunk/geoext/examples/popup-more.js
===================================================================
--- core/trunk/geoext/examples/popup-more.js	2010-09-13 12:28:45 UTC (rev 2342)
+++ core/trunk/geoext/examples/popup-more.js	2010-09-13 13:14:39 UTC (rev 2343)
@@ -36,13 +36,6 @@
             });
         }
 
-        // This is awkward, but for now we need to create a feature to update
-        // the popup position.  TODO: fix this for 1.0
-        var feature = new OpenLayers.Feature.Vector(
-            new OpenLayers.Geometry.Point(loc.lon, loc.lat)
-        );  
-        
-        
         // add some content to the popup (this can be any Ext component)
         popup.add({
             xtype: "box",
@@ -52,7 +45,7 @@
         });
 
         // reset the popup's location
-        popup.feature = feature;
+        popup.location = loc;
         
         popup.doLayout();
 

Modified: core/trunk/geoext/examples/popup.js
===================================================================
--- core/trunk/geoext/examples/popup.js	2010-09-13 12:28:45 UTC (rev 2342)
+++ core/trunk/geoext/examples/popup.js	2010-09-13 13:14:39 UTC (rev 2343)
@@ -32,7 +32,7 @@
     function createPopup(feature) {
         popup = new GeoExt.Popup({
             title: 'My Popup',
-            feature: feature,
+            location: feature,
             width:200,
             html: bogusMarkup,
             maximizable: true,

Modified: core/trunk/geoext/lib/GeoExt/widgets/Popup.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/Popup.js	2010-09-13 12:28:45 UTC (rev 2342)
+++ core/trunk/geoext/lib/GeoExt/widgets/Popup.js	2010-09-13 13:14:39 UTC (rev 2343)
@@ -24,7 +24,7 @@
  *     
  *      var popup = new GeoExt.Popup({
  *          title: "My Popup",
- *          feature: feature,
+ *          location: feature,
  *          width: 200,
  *          html: "<div>Popup content</div>",
  *          collapsible: true
@@ -35,15 +35,15 @@
  *  .. class:: Popup(config)
  *   
  *      Popups are a specialized Window that supports anchoring
- *      to a particular feature in a MapPanel.  When a popup
- *      is anchored to a feature, that means that the popup
- *      will visibly point to the feature on the map, and move
+ *      to a particular location in a MapPanel.  When a popup
+ *      is anchored to a location, that means that the popup
+ *      will visibly point to the location on the map, and move
  *      accordingly when the map is panned or zoomed.
  */
 GeoExt.Popup = Ext.extend(Ext.Window, {
 
     /** api: config[anchored]
-     *  ``Boolean``  The popup begins anchored to its feature.  Default is
+     *  ``Boolean``  The popup begins anchored to its location.  Default is
      *  ``true``.
      */
     anchored: true,
@@ -51,7 +51,7 @@
     /** 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
+     *  is set to true and the map cannot be derived from the ``location``'s
      *  layer.
      */
     map: null,
@@ -64,21 +64,20 @@
 
     /** api: config[unpinnable]
      *  ``Boolean`` The popup should have a "unpin" tool that unanchors it from
-     *  its feature.  Default is ``true``.
+     *  its location.  Default is ``true``.
      */
     unpinnable: true,
 
-    /** api: config[feature]
-     *  ``OpenLayers.Feature`` A location for this popup's anchor.  One of
-     *  ``feature`` or ``lonlat`` must be provided.
+    /** api: config[location]
+     *  ``OpenLayers.Feature.Vector`` or ``OpenLayers.LonLat`` or
+     *  ``OpenLayers.Pixel`` or ``OpenLayers.Geometry`` A location for this 
+     *  popup's anchor.
      */
-    feature: null,
-
-    /** api: config[lonlat]
-     *  ``OpenLayers.LonLat`` A location for this popup's anchor.  One of
-     *  ``feature`` or ``lonlat`` must be provided.
+    
+    /** private: property[location]
+     *  ``OpenLayers.LonLat``
      */
-    lonlat: null,
+    location: null,
 
     /**
      * Some Ext.Window defaults need to be overriden here
@@ -124,12 +123,22 @@
         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.map && this.location instanceof OpenLayers.Feature.Vector &&
+                                                        this.location.layer) {
+            this.map = this.location.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.location instanceof OpenLayers.Feature.Vector) {
+            this.location = this.location.geometry;
         }
+        if (this.location instanceof OpenLayers.Geometry) {
+            if (typeof this.location.getCentroid == "function") {
+                this.location = this.location.getCentroid();
+            }
+            this.location = this.location.getBounds().getCenterLonLat();
+        } else if (this.location instanceof OpenLayers.Pixel) {
+            this.location = this.map.getLonLatFromViewPortPx(this.location);
+        }
+
         if(this.anchored) {
             this.addAnchorEvents();
         }
@@ -210,20 +219,18 @@
     },
 
     /** private: method[position]
-     *  Positions the popup relative to its feature
+     *  Positions the popup relative to its location
      */
     position: function() {
-        var centerLonLat = this.feature.geometry.getBounds().getCenterLonLat();
-
         if(this._mapMove === true) {
-            var visible = this.map.getExtent().containsLonLat(centerLonLat);
+            var visible = this.map.getExtent().containsLonLat(this.location);
             if(visible !== this.isVisible()) {
                 this.setVisible(visible);
             }
         }
 
         if(this.isVisible()) {
-            var centerPx = this.map.getViewPortPxFromLonLat(centerLonLat);
+            var centerPx = this.map.getViewPortPxFromLonLat(this.location);
             var mapBox = Ext.fly(this.map.div).getBox(); 
     
             //This works for positioning with the anchor on the bottom.
@@ -239,7 +246,7 @@
     },
 
     /** private: method[unanchorPopup]
-     *  Unanchors a popup from its feature.  This removes the popup from its
+     *  Unanchors a popup from its location.  This removes the popup from its
      *  MapPanel and adds it to the page body.
      */
     unanchorPopup: function() {
@@ -327,7 +334,7 @@
     /** private: method[removeAnchorEvents]
      */
     removeAnchorEvents: function() {
-        //stop position with feature
+        //stop position with location
         this.map.events.un({
             "move" : this.onMapMove,
             scope : this

Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html	2010-09-13 12:28:45 UTC (rev 2342)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/Popup.html	2010-09-13 13:14:39 UTC (rev 2343)
@@ -50,10 +50,11 @@
             context.mapPanel.destroy();
         }
 
-        function popup(feature) {
+        function popup(feature, map) {
             return new GeoExt.Popup({
                 title: 'My Popup',
-                feature: feature,
+                location: feature,
+                map: map,
                 width: 200,
                 html: "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
                 collapsible: true
@@ -180,7 +181,37 @@
             tearDown(context);
 
         }
+        
+        function test_Popup_position(t) {
+            t.plan(5);
+            
+            var context = setupContext();
+            
+            var pos = context.feature.geometry.getBounds().getCenterLonLat()
+            var pop = popup(pos, context.map);
+            t.eq(pop.location.toString(), pos.toString(), "Popup with LonLat positioned correctly");
+            
+            var px = context.map.getViewPortPxFromLonLat(pos);
+            pop = popup(px, context.map);
+            t.eq(pop.location.toString(), context.map.getLonLatFromViewPortPx(px).toString(), "Popup with Pixel positioned correctly");
+            
+            // mock up a geometry without getCentroid method
+            context.feature.geometry.getCentroid = null;
+            pop = popup(context.feature);
+            t.eq(pop.location.toString(), pos.toString(), "Popup with Feature positioned correctly");
+            
+            var geom = new OpenLayers.Geometry.Point(1, 42);
+            pop = popup(geom, context.map);
+            t.eq(pop.location.toString(), geom.getCentroid().getBounds().getCenterLonLat().toString(), "Popup with Point positioned correctly");
 
+            var geom = OpenLayers.Geometry.fromWKT("POLYGON ((10 10, 10 20, 20 20, 20 15, 10 10))");
+            pop = popup(geom, context.map);
+            t.eq(pop.location.toString(), geom.getCentroid().getBounds().getCenterLonLat().toString(), "Popup with Polygon positioned correctly");
+
+
+            tearDown(context);
+        }
+
         function test_Popup_destroy(t){
 
             t.plan(1);



More information about the Commits mailing list