[Commits] r290 - core/trunk/geoext/examples

commits at geoext.org commits at geoext.org
Sat Mar 28 15:40:52 CET 2009


Author: elemoine
Date: 2009-03-28 15:40:51 +0100 (Sat, 28 Mar 2009)
New Revision: 290

Modified:
   core/trunk/geoext/examples/popup.js
Log:
(a) popup example refactoring, and (b) show how to unselect feature when popup is closed, no functional change


Modified: core/trunk/geoext/examples/popup.js
===================================================================
--- core/trunk/geoext/examples/popup.js	2009-03-27 18:14:42 UTC (rev 289)
+++ core/trunk/geoext/examples/popup.js	2009-03-28 14:40:51 UTC (rev 290)
@@ -1,77 +1,75 @@
-    var mapwin, mapPanel, pop;
-    Ext.onReady(function() {
+// make the references to the map panel and the popup 
+// global, this is useful for looking at their states
+// from the console
+var mapPanel, popup;
 
-        /* 
-         * Set up the vector layer.
-         *
-         * Will need a reference to the layer later when
-         * giving it to a SelectFeature control to trigger
-         * the popups.
-         *
-         */
-        var vector = new OpenLayers.Layer.Vector("Vector Layer",{
-            styleMap: new OpenLayers.StyleMap()
-        });
+Ext.onReady(function() {
 
-        x = -45;
-        y = 5;
+    // create a vector layer, add a feature into it
+    var vectorLayer = new OpenLayers.Layer.Vector("vector");
+    vectorLayer.addFeatures(
+        new OpenLayers.Feature.Vector(
+            new OpenLayers.Geometry.Point(-45, 5)
+        )
+    );
 
-        var point = new OpenLayers.Geometry.Point(x,y);
-        var feature = new OpenLayers.Feature.Vector(point, {
-            name : "MyFeature",
-            id : "42"
-        });
-        vector.addFeatures(feature);
+    // create select feature control
+    var selectCtrl = new OpenLayers.Control.SelectFeature(vectorLayer);
 
-        mapwin = new Ext.Window({
-            layout: "fit",
-            title: "Map",
-            closeAction: "hide",
-            width: 650,
-            height: 356,
-            x: 50,
-            y: 100,
-            items: {
-                xtype: "gx_mappanel",
-                region: "center",
-                layers: [
-                    new OpenLayers.Layer.WMS( 
-                        "OpenLayers WMS",
-                        "http://labs.metacarta.com/wms/vmap0",
-                        {layers: 'basic'} ),
-                    vector
-                ]
-            }
+    // define "createPopup" function
+    var bogusMarkup = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
+    function createPopup(feature) {
+        popup = new GeoExt.Popup({
+            title: 'My Popup',
+            feature: feature,
+            width:200,
+            html: bogusMarkup,
+            collapsible: true
         });
-
-        mapwin.show();
-
-        mapPanel = mapwin.items.get(0);
-
-        var select = new OpenLayers.Control.SelectFeature(vector, {
-            onSelect : function(feature){
-                openPopup(feature);
+        // unselect feature when the popup
+        // is closed
+        popup.on({
+            close: function() {
+                if(OpenLayers.Util.indexOf(vectorLayer.selectedFeatures,
+                                           this.feature) > -1) {
+                    selectCtrl.unselect(this.feature);
+                }
             }
         });
-        
-        mapPanel.map.addControl(select);
-        select.activate();
+        popup.addToMapPanel(mapPanel);
+    }
 
+    // create popup on "featureselected"
+    vectorLayer.events.on({
+        featureselected: function(e) {
+            createPopup(e.feature);
+        }
     });
 
+    // create Ext window including a map panel
+    var mapwin = new Ext.Window({
+        layout: "fit",
+        title: "Map",
+        closeAction: "hide",
+        width: 650,
+        height: 356,
+        x: 50,
+        y: 100,
+        items: {
+            xtype: "gx_mappanel",
+            region: "center",
+            layers: [
+                new OpenLayers.Layer.WMS( 
+                    "OpenLayers WMS",
+                    "http://labs.metacarta.com/wms/vmap0",
+                    {layers: 'basic'} ),
+                vectorLayer
+            ]
+        }
+    });
+    mapwin.show();
 
-    function openPopup(feature){
-      pop = new GeoExt.Popup({
-        title: 'My Popup',
-        feature: feature,
-        width:200,
-        html: bogusMarkup,
-        collapsible: true
-      });
-      
-      pop.addToMapPanel(mapPanel);
-    }
-
-    var bogusMarkup = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
-
-
+    mapPanel = mapwin.items.get(0);
+    mapPanel.map.addControl(selectCtrl);
+    selectCtrl.activate();
+});



More information about the Commits mailing list