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

commits at geoext.org commits at geoext.org
Wed Dec 23 18:25:59 CET 2009


Author: ahocevar
Date: 2009-12-23 18:25:59 +0100 (Wed, 23 Dec 2009)
New Revision: 1607

Modified:
   core/trunk/geoext/examples/legendpanel.js
Log:
made buttons more fun to play with

Modified: core/trunk/geoext/examples/legendpanel.js
===================================================================
--- core/trunk/geoext/examples/legendpanel.js	2009-12-23 16:26:05 UTC (rev 1606)
+++ core/trunk/geoext/examples/legendpanel.js	2009-12-23 17:25:59 UTC (rev 1607)
@@ -38,32 +38,34 @@
     ]);
     map.addControl(new OpenLayers.Control.LayerSwitcher());
 
-    var addLayer = function() {
-        var wmslayer = new OpenLayers.Layer.WMS("Bodies of Water",
-            "http://demo.opengeo.org/geoserver/wms?",
-            {layers: 'topp:tasmania_water_bodies', format: 'image/png', transparent: true},
-            {singleTile: true});
-        mapPanel.map.addLayer(wmslayer);
+    var addRemoveLayer = function() {
+        if(mapPanel.map.layers.indexOf(water) == -1) {
+            mapPanel.map.addLayer(water);
+        } else {
+            mapPanel.map.removeLayer(water);
+        }
     };
 
-    var removeLayer = function() {
-        mapPanel.map.removeLayer(mapPanel.map.layers[1]);
-    };
-
     var moveLayer = function(idx) {
-        mapPanel.map.setLayerIndex(mapPanel.map.layers[0], idx);
+        var layer = layerRec0.get("layer");
+        var idx = mapPanel.map.layers.indexOf(layer) == 0 ?
+            mapPanel.map.layers.length : 0;
+        mapPanel.map.setLayerIndex(layerRec0.get("layer"), idx);
     };
 
     var toggleVisibility = function() {
-        mapPanel.map.layers[1].setVisibility(!mapPanel.map.layers[1].getVisibility());
+        var layer = layerRec0.get("layer");
+        layer.setVisibility(!layer.getVisibility());
     };
 
     var updateHideInLegend = function() {
-        mapPanel.layers.getAt(1).set("hideInLegend", true);
+        layerRec0.set("hideInLegend", !layerRec0.get("hideInLegend"));
     };
 
     var updateLegendUrl = function() {
-        mapPanel.layers.getAt(0).set("legendURL", "http://www.geoext.org/trac/geoext/chrome/site/img/GeoExt.png");
+        var url = layerRec0.get("legendURL");
+        layerRec0.set("legendURL", otherUrl);
+        otherUrl = url;
     };
 
     var mapPanel = new GeoExt.MapPanel({
@@ -74,7 +76,21 @@
         center: new OpenLayers.LonLat(146.4, -41.6),
         zoom: 7
     });
+    
+    // give the record of the 1st layer a legendURL, which will cause
+    // UrlLegend instead of WMSLegend to be used
+    var layerRec0 = mapPanel.layers.getAt(0);
+    layerRec0.set("legendURL", "http://demo.opengeo.org/geoserver/wms?FORMAT=image%2Fgif&TRANSPARENT=true&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&EXCEPTIONS=application%2Fvnd.ogc.se_xml&LAYER=topp%3Atasmania_state_boundaries");
 
+    // stores another legendURL for the legendurl button action
+    var otherUrl = "http://www.geoext.org/trac/geoext/chrome/site/img/GeoExt.png";
+
+    // create another layer for the add/remove button action
+    var water = new OpenLayers.Layer.WMS("Bodies of Water",
+        "http://demo.opengeo.org/geoserver/wms?",
+        {layers: 'topp:tasmania_water_bodies', format: 'image/png', transparent: true},
+        {singleTile: true});
+
     legendPanel = new GeoExt.LegendPanel({
         defaults: {
             labelCls: 'mylabel',
@@ -94,12 +110,10 @@
         width: 800,
         tbar: new Ext.Toolbar({
             items: [
-                {text: 'add', handler: addLayer},
-                {text: 'remove', handler: removeLayer},
-                {text: 'movetotop', handler: function() { moveLayer(10); } },
-                {text: 'moveup', handler: function() { moveLayer(1); } },
+                {text: 'add/remove', handler: addRemoveLayer},
+                {text: 'movetop/bottom', handler: moveLayer },
                 {text: 'togglevis', handler: toggleVisibility},
-                {text: 'hide', handler: updateHideInLegend},
+                {text: 'hide/show', handler: updateHideInLegend},
                 {text: 'legendurl', handler: updateLegendUrl}
             ]
         }),



More information about the Commits mailing list