[Commits] r820 - in sandbox/bartvde/legend2/geoext: examples lib/GeoExt/widgets tests/lib/GeoExt/widgets

commits at geoext.org commits at geoext.org
Tue May 19 20:50:10 CEST 2009


Author: bartvde
Date: 2009-05-19 20:50:10 +0200 (Tue, 19 May 2009)
New Revision: 820

Modified:
   sandbox/bartvde/legend2/geoext/examples/legendpanel.js
   sandbox/bartvde/legend2/geoext/lib/GeoExt/widgets/LegendPanel.js
   sandbox/bartvde/legend2/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
Log:
update for legend panel wrt how it reorders itself when layer order changes

Modified: sandbox/bartvde/legend2/geoext/examples/legendpanel.js
===================================================================
--- sandbox/bartvde/legend2/geoext/examples/legendpanel.js	2009-05-19 17:12:01 UTC (rev 819)
+++ sandbox/bartvde/legend2/geoext/examples/legendpanel.js	2009-05-19 18:50:10 UTC (rev 820)
@@ -22,6 +22,7 @@
                     strokeWidth: 2
                 }) }) })
     ]);
+    map.addControl(new OpenLayers.Control.LayerSwitcher());
 
     addLayer = function() {
         var wmslayer = new OpenLayers.Layer.WMS("Bodies of Water",
@@ -61,7 +62,6 @@
 
     legendPanel = new GeoExt.LegendPanel({
         labelCls: 'mylabel',
-        ascending: false,
         bodyStyle: 'padding:5px',
         width: 350,
         tbar: new Ext.Toolbar({items: [

Modified: sandbox/bartvde/legend2/geoext/lib/GeoExt/widgets/LegendPanel.js
===================================================================
--- sandbox/bartvde/legend2/geoext/lib/GeoExt/widgets/LegendPanel.js	2009-05-19 17:12:01 UTC (rev 819)
+++ sandbox/bartvde/legend2/geoext/lib/GeoExt/widgets/LegendPanel.js	2009-05-19 18:50:10 UTC (rev 820)
@@ -20,14 +20,6 @@
  */
 GeoExt.LegendPanel = Ext.extend(Ext.Panel, {
 
-    /** api: config[ascending]
-     *  ``Boolean``
-     *  If true the layers in the legend will show the bottom OpenLayers 
-     *  layer on top and the top OpenLayers layer on the bottom. 
-     *  If false, this is inverted. Default value is true.
-     */
-    ascending: true,
-
     /** api: config[dynamic]
      *  ``Boolean``
      *  If false the LegendPanel will not listen to the add, remove and change 
@@ -78,11 +70,12 @@
         if(!this.layerStore) {
             this.layerStore = GeoExt.MapPanel.guess().layers;
         }
-        this.layerStore.each(this.addLegend, this);
+        this.layerStore.each(function(record) {
+                this.addLegend(record);
+            }, this);
         if (this.dynamic) {
             this.layerStore.on({
                 "add": this.onStoreAdd,
-                "move": this.onStoreMove,
                 "remove": this.onStoreRemove,
                 "update": this.onStoreUpdate,
                 scope: this
@@ -91,6 +84,33 @@
         this.doLayout();
     },
 
+    /** private: method[recordIndexToPanelIndex]
+     *  Private method to get the panel index for a layer represented by a
+     *  record.
+     *
+     *  :param index ``Integer`` The index of the record in the store.
+     *
+     *  :return: ``Integer`` The index of the sub panel in this panel.
+     */
+    recordIndexToPanelIndex: function(index) {
+        var store = this.layerStore;
+        var count = store.getCount();
+        var panelIndex = -1;
+        for(var i=count-1; i>=0; --i) {
+            var layer = store.getAt(i).get("layer");
+            var legendGenerator = GeoExt.legend[layer.CLASS_NAME.replace(
+                'OpenLayers.Layer.', '')];
+            if(layer.displayInLayerSwitcher && legendGenerator &&
+                (store.getAt(i).get("hideInLegend") !== true)) {
+                    ++panelIndex;
+                    if(index === i) {
+                        break;
+                    }
+            }
+        };
+        return panelIndex;
+    },
+
     /** private: method[onStoreUpdate]
      *  Update a layer within the legend panel. Gets called when the store
      *  fires the update event. This usually means the visibility of the layer
@@ -117,41 +137,6 @@
         }
     },
 
-    /** private: method[onStoreMove]
-     *  Relocate a layer within the legend panel. Gets called when the store
-     *  fires the move event.
-     *
-     *  :param store: ``Ext.data.Store`` The store in which the record was
-     *      moved.
-     *  :param record: ``Ext.data.Record`` The record object corresponding
-     *      to the moved layer.
-     *  :param from: ``Integer`` The previous index of the moved record.
-     *  :param to: ``Integer`` The new index of the moved record.
-     */
-    onStoreMove: function(store, record, from, to) {
-        this.moveLegend(record, to);
-    },
-
-    /** private: method[moveLegend]
-     *  Relocate a layer within the legend panel. Removes the existing panel
-     *  and then inserts it at the right index.
-     *
-     *  :param record: ``Ext.data.Record`` The record object(s) corresponding
-     *      to the moved layer.
-     *  :param index: ``Integer`` The new index of the moved record.
-     */
-    moveLegend: function(record, index) {
-        var legend = this.getComponent(record.get('layer').id);
-        this.remove(legend);
-        var newLegend = this.createLegendSubpanel(record);
-        if (this.ascending) {
-            this.insert(index, newLegend); 
-        } else {
-            this.insert((this.items.length-index), newLegend);
-        }
-        this.doLayout(); 
-    },
-
     /** private: method[onStoreAdd]
      *  Private method called when a layer is added to the store.
      *
@@ -162,8 +147,9 @@
      *  :param index: ``Integer`` The index of the inserted record.
      */
     onStoreAdd: function(store, records, index) {
+        var panelIndex = this.recordIndexToPanelIndex(index);
         for (var i=0, len=records.length; i<len; i++) {
-            this.addLegend(records[i]);
+            this.addLegend(records[i], panelIndex);
         }
         this.doLayout();
     },
@@ -189,7 +175,7 @@
     removeLegend: function(record) {
         var legend = this.getComponent(record.get('layer').id);
         if (legend) {
-            legend.hide();
+            this.remove(legend, true);
             this.doLayout();
         }
     },
@@ -205,18 +191,20 @@
     createLegendSubpanel: function(record) {
         var layer = record.get('layer');
         var mainPanel = this.createMainPanel(record);
-        // the default legend can be overridden by specifying a
-        // legendURL property
-        if (record.get('legendURL')) {
-            var legend = new GeoExt.legend.Image({url:
-                record.get('legendURL')});
-            mainPanel.add(legend);
-        } else {
-            var legendGenerator = GeoExt.legend[layer.CLASS_NAME.replace(
-                'OpenLayers.Layer.', '')];
-            if (legendGenerator) {
-                var legend = new legendGenerator({layer: layer});
+        if (mainPanel !== null) {
+            // the default legend can be overridden by specifying a
+            // legendURL property
+            if (record.get('legendURL')) {
+                var legend = new GeoExt.legend.Image({url:
+                    record.get('legendURL')});
                 mainPanel.add(legend);
+            } else {
+                var legendGenerator = GeoExt.legend[layer.CLASS_NAME.replace(
+                    'OpenLayers.Layer.', '')];
+                if (legendGenerator) {
+                    var legend = new legendGenerator({layer: layer});
+                    mainPanel.add(legend);
+                }
             }
         }
         return mainPanel;
@@ -227,26 +215,15 @@
      *
      *  :param record: ``Ext.data.Record`` The record object from the layer 
      *      store.
+     *  :param index: ``Integer`` The position at which to add the legend.
      */
-    addLegend: function(record) {
+    addLegend: function(record, index) {
+        index = index || 0;
         var layer = record.get('layer');
-        // a layer can be excluded from the legend by setting the hideInLegend
-        // property to true
-        var hideInLegend = record.get('hideInLegend');
         var legendSubpanel = this.createLegendSubpanel(record);
         if (legendSubpanel !== null) {
-            // if there is only a label, it does not make much sense to show it
-            if (legendSubpanel.items.items.length == 1) {
-                legendSubpanel.hide();
-            } else {
-                legendSubpanel.setVisible((layer.getVisibility() && 
-                    layer.displayInLayerSwitcher && !hideInLegend));
-            }
-            if (this.ascending) {
-                this.add(legendSubpanel);
-            } else {
-                this.insert(0, legendSubpanel);
-            }
+           legendSubpanel.setVisible(layer.getVisibility());
+           this.insert(index, legendSubpanel);
         }
     },
 
@@ -260,21 +237,27 @@
      */
     createMainPanel: function(record) {
         var layer = record.get('layer');
-        var panelConfig = {
-            id: layer.id,
-            border: false,
-            bodyBorder: false,
-            bodyStyle: this.bodyStyle,
-            items: [
-                new Ext.form.Label({
-                    text: (this.showTitle && !record.get('hideTitle')) ? 
-                        layer.name : '',
-                    cls: 'x-form-item x-form-item-label' +
-                        (this.labelCls ? ' ' + this.labelCls : '')
-                })
-            ]
-        };
-        var panel = new Ext.Panel(panelConfig);
+        var panel = null;
+        var legendGenerator = GeoExt.legend[layer.CLASS_NAME.replace(
+            'OpenLayers.Layer.', '')];
+        if (layer.displayInLayerSwitcher && !record.get('hideInLegend') && 
+          legendGenerator) {
+            var panelConfig = {
+                id: layer.id,
+                border: false,
+                bodyBorder: false,
+                bodyStyle: this.bodyStyle,
+                items: [
+                    new Ext.form.Label({
+                        text: (this.showTitle && !record.get('hideTitle')) ? 
+                            layer.name : '',
+                        cls: 'x-form-item x-form-item-label' +
+                            (this.labelCls ? ' ' + this.labelCls : '')
+                    })
+                ]
+            };
+            panel = new Ext.Panel(panelConfig);
+        }
         return panel;
     },
 

Modified: sandbox/bartvde/legend2/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
===================================================================
--- sandbox/bartvde/legend2/geoext/tests/lib/GeoExt/widgets/LegendPanel.html	2009-05-19 17:12:01 UTC (rev 819)
+++ sandbox/bartvde/legend2/geoext/tests/lib/GeoExt/widgets/LegendPanel.html	2009-05-19 18:50:10 UTC (rev 820)
@@ -98,7 +98,7 @@
             mapPanel.map.addLayer(layer);
 
             t.eq(lp.items.length, 1, "If dynamic is false, do not add or remove layers from legend");
-            
+
             lp.destroy();
             mapPanel.destroy();
         }
@@ -114,7 +114,7 @@
             var url = item.items.items[1].items.items[0].url;
             var expectedUrl = "/ows?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fgif&LAYER=a";
             t.eq(url, expectedUrl, "GetLegendGraphic url is generated correctly");
-            
+
             lp.destroy();
             mapPanel.destroy();
         }
@@ -138,10 +138,10 @@
             layer = new OpenLayers.Layer.WMS("test3", '/ows', {layers: 'c'}, {visibility: false});
             mapPanel.map.addLayer(layer);
 
-            t.eq(lp.items.length, 3, "A non visible WMS layer will be added");
+            t.eq(lp.items.length, 3, "A non visible WMS layer will be added but will be invisible");
 
             mapPanel.map.removeLayer(mapPanel.map.layers[0]);
-            t.eq(lp.items.length, 3, "Removing the WMS layer only hides the legend from the panel");
+            t.eq(lp.items.length, 2, "Removing the WMS layer really removes the legend from the panel");
 
             lp.destroy();
             mapPanel.destroy();



More information about the Commits mailing list