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

commits at geoext.org commits at geoext.org
Tue May 26 19:11:07 CEST 2009


Author: bartvde
Date: 2009-05-26 19:11:07 +0200 (Tue, 26 May 2009)
New Revision: 867

Added:
   core/trunk/geoext/examples/legendpanel.html
   core/trunk/geoext/examples/legendpanel.js
   core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js
   core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
   core/trunk/geoext/lib/GeoExt/widgets/LegendWMS.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
Modified:
   core/trunk/geoext/lib/GeoExt.js
   core/trunk/geoext/tests/list-tests.html
Log:
add LegendPanel component with currently support for WMS layers r=tschaub thanks to dwins and tcoulter (closes #2)

Added: core/trunk/geoext/examples/legendpanel.html
===================================================================
--- core/trunk/geoext/examples/legendpanel.html	                        (rev 0)
+++ core/trunk/geoext/examples/legendpanel.html	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,26 @@
+<html>
+    <head>
+        <script type="text/javascript" src="http://dev.geoext.org/trunk/ext/adapter/ext/ext-base.js"></script>
+        <script type="text/javascript" src="http://dev.geoext.org/trunk/ext/ext-all.js"></script>
+        <link rel="stylesheet" type="text/css" href="http://extjs.cachefly.net/ext-2.2.1/resources/css/ext-all.css" />
+        <link rel="stylesheet" type="text/css" href="http://extjs.com/deploy/dev/examples/shared/examples.css"></link>
+        <script src="http://openlayers.org/api/2.8-rc2/OpenLayers.js"></script>
+        <script type="text/javascript" src="../lib/GeoExt.js"></script>
+
+        <script type="text/javascript" src="legendpanel.js"></script>
+
+        <style type="text/css">
+        .mylabel {
+            font-weight: bold;
+            color: red;
+        }
+        </style>
+    </head>
+    <body>
+        <h1>GeoExt.LegendPanel</h1>
+        <p>This example shows the how to create a LegendPanel that autopopulates with legends from a map
+        that has already been created.</p>
+        <p>The js is not minified so it is readable. See <a href="legendpanel.js">legendpanel.js</a>.</p>
+        <div id="view"></div>
+    </body>
+</html>
\ No newline at end of file

Added: core/trunk/geoext/examples/legendpanel.js
===================================================================
--- core/trunk/geoext/examples/legendpanel.js	                        (rev 0)
+++ core/trunk/geoext/examples/legendpanel.js	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,91 @@
+
+var mapPanel;
+
+Ext.onReady(function() {
+    var map = new OpenLayers.Map({allOverlays: true});
+    map.addLayers([
+        new OpenLayers.Layer.WMS(
+            "Tasmania",
+            "http://publicus.opengeo.org/geoserver/wms?",
+            {layers: 'topp:tasmania_state_boundaries', format: 'image/png', transparent: true},
+            {singleTile: true}),
+        new OpenLayers.Layer.WMS(
+            "Cities and Roads",
+            "http://publicus.opengeo.org/geoserver/wms?",
+            {layers: 'topp:tasmania_cities,topp:tasmania_roads', format: 'image/png', transparent: true},
+            {singleTile: true}),
+        new OpenLayers.Layer.Vector('Polygons', {styleMap: new OpenLayers.StyleMap({
+                "default": new OpenLayers.Style({
+                    pointRadius: 8,
+                    fillColor: "#00ffee",
+                    strokeColor: "#000000",
+                    strokeWidth: 2
+                }) }) })
+    ]);
+    map.addControl(new OpenLayers.Control.LayerSwitcher());
+
+    var addLayer = function() {
+        var wmslayer = new OpenLayers.Layer.WMS("Bodies of Water",
+            "http://publicus.opengeo.org/geoserver/wms?",
+            {layers: 'topp:tasmania_water_bodies', format: 'image/png', transparent: true},
+            {singleTile: true});
+        mapPanel.map.addLayer(wmslayer);
+    };
+
+    var removeLayer = function() {
+        mapPanel.map.removeLayer(mapPanel.map.layers[1]);
+    };
+
+    var moveLayer = function(idx) {
+        mapPanel.map.setLayerIndex(mapPanel.map.layers[0], idx);
+    };
+
+    var toggleVisibility = function() {
+        mapPanel.map.layers[1].setVisibility(!mapPanel.map.layers[1].getVisibility());
+    };
+
+    var updateHideInLegend = function() {
+        mapPanel.layers.getAt(1).set("hideInLegend", true);
+    };
+
+    var updateLegendUrl = function() {
+        mapPanel.layers.getAt(0).set("legendURL", "http://www.geoext.org/trac/geoext/chrome/site/img/GeoExt.png");
+    };
+
+    var mapPanel = new GeoExt.MapPanel({
+        region: 'center',
+        height: 400,
+        width: 600,
+        map: map,
+        center: new OpenLayers.LonLat(146.4, -41.6),
+        zoom: 7
+    });
+
+    var legendPanel = new GeoExt.LegendPanel({
+        labelCls: 'mylabel',
+        bodyStyle: 'padding:5px',
+        width: 350,
+        autoScroll: true,
+        region: 'west'
+    });
+
+    new Ext.Panel({
+        title: "GeoExt LegendPanel Demo",
+        layout: 'border',
+        renderTo: 'view',
+        height: 400,
+        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: 'togglevis', handler: toggleVisibility},
+                {text: 'hide', handler: updateHideInLegend},
+                {text: 'legendurl', handler: updateLegendUrl}
+            ]
+        }),
+        items: [legendPanel, mapPanel]
+    });
+});

Added: core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendImage.js	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,80 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ * 
+ * pending approval */
+
+/** api: (define)
+ *  module = GeoExt
+ *  class = LegendImage
+ */
+
+Ext.namespace('GeoExt');
+
+/** api: constructor
+ *  .. class:: LegendImage(config)
+ *
+ *  Show a legend image in a BoxComponent and make sure load errors are dealt
+ *  with.
+ */
+GeoExt.LegendImage = Ext.extend(Ext.BoxComponent, {
+
+    /** api: config[url]
+     *  ``String``  The url of the image to load
+     */
+    url: null,
+
+    /** api: config[imgCls]
+     *  ``String``  Optional css class to apply to img tag
+     */
+    imgCls: null,
+
+    /** private: method[initComponent]
+     *  Initializes the legend image component. 
+     */
+    initComponent: function() {
+        GeoExt.LegendImage.superclass.initComponent.call(this);
+        this.autoEl = {tag: 'img',
+            'class': (this.imgCls ? this.imgCls : ''), src: this.url};
+    },
+
+    /** api: method[setUrl]
+     *  :param url: ``String`` The new url of the image.
+     *  
+     *  Sets the url of the image.
+     */
+    setUrl: function(url) {
+        var el = this.getEl();
+        if (el) {
+            el.dom.src = url;
+        }
+    },
+
+    /** private: method[onRender]
+     *  Private method called when the legend image component is being
+     *  rendered.
+     */
+    onRender: function(ct, position) {
+        GeoExt.LegendImage.superclass.onRender.call(this, ct, position);
+        this.getEl().on('error', this.onImageLoadError, this);
+    },
+
+    /** private: method[onDestroy]
+     *  Private method called during the destroy sequence.
+     */
+    onDestroy: function() {
+        this.getEl().un('error', this.onImageLoadError, this);
+        GeoExt.LegendImage.superclass.onDestroy.apply(this, arguments);
+    },
+    
+    /** private: method[onImageLoadError]
+     *  Private method called if the legend image fails loading.
+     */
+    onImageLoadError: function() {
+        this.getEl().dom.src = Ext.BLANK_IMAGE_URL;
+    }
+
+});
+
+Ext.reg('gx_legendimage', GeoExt.LegendImage);

Added: core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,281 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ *
+ * pending approval */
+
+/** api: (define)
+ *  module = GeoExt
+ *  class = LegendPanel
+ */
+
+Ext.namespace('GeoExt');
+
+/** api: constructor
+ *  .. class:: LegendPanel(config)
+ *
+ *  A panel showing legends of all layers in a layer store.
+ *  Depending on the layer type, a legend renderer will be chosen.
+ */
+GeoExt.LegendPanel = Ext.extend(Ext.Panel, {
+
+    /** api: config[dynamic]
+     *  ``Boolean``
+     *  If false the LegendPanel will not listen to the add, remove and change 
+     *  events of the LayerStore. So it will load with the initial state of
+     *  the LayerStore and not change anymore. 
+     */
+    dynamic: true,
+    
+    /** api: config[showTitle]
+     *  ``Boolean``
+     *  Whether or not to show the title of a layer. This can be a global
+     *  setting for the whole panel, or it can be overridden on the LayerStore 
+     *  record using the hideInLegend property.
+     */
+    showTitle: true,
+
+    /** api: config[labelCls]
+     *  ``String``
+     *  Optional css class to use for the layer title labels.
+     */
+    labelCls: null,
+
+    /** api: config[bodyStyle]
+     *  ``String``
+     *  Optional style to apply to the body of the legend panels.
+     */
+    bodyStyle: '',
+
+    /** api: config[layerStore]
+     *  ``GeoExt.data.LayerStore``
+     *  The layer store containing layers to be displayed in the legend 
+     *  container. If not provided it will be taken from the MapPanel.
+     */
+    layerStore: null,
+
+    /** private: method[initComponent]
+     *  Initializes the legend panel.
+     */
+    initComponent: function() {
+        GeoExt.LegendPanel.superclass.initComponent.call(this);
+    },
+    
+    /** private: method[onRender]
+     *  Private method called when the legend panel is being rendered.
+     */
+    onRender: function() {
+        GeoExt.LegendPanel.superclass.onRender.apply(this, arguments);
+        if(!this.layerStore) {
+            this.layerStore = GeoExt.MapPanel.guess().layers;
+        }
+        this.layerStore.each(function(record) {
+                this.addLegend(record);
+            }, this);
+        if (this.dynamic) {
+            this.layerStore.on({
+                "add": this.onStoreAdd,
+                "remove": this.onStoreRemove,
+                "update": this.onStoreUpdate,
+                scope: this
+            });
+        }
+        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.split(".").pop()
+            ];
+            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
+     *  has changed.
+     *
+     *  :param store: ``Ext.data.Store`` The store in which the record was
+     *      changed.
+     *  :param record: ``Ext.data.Record`` The record object corresponding
+     *      to the updated layer.
+     *  :param operation: ``String`` The type of operation.
+     */
+    onStoreUpdate: function(store, record, operation) {
+        var layer = record.get('layer');
+        var legend = this.getComponent(layer.id);
+        if (legend) {
+            legend.setVisible(layer.getVisibility() && 
+                layer.displayInLayerSwitcher && !record.get('hideInLegend'));
+            if (record.get('legendURL')) {
+                var items = legend.findByType('gx_legendimage');
+                for (var i=0, len=items.length; i<len; i++) {
+                    items[i].setUrl(record.get('legendURL'));
+                }
+            }
+        }
+    },
+
+    /** private: method[onStoreAdd]
+     *  Private method called when a layer is added to the store.
+     *
+     *  :param store: ``Ext.data.Store`` The store to which the record(s) was 
+     *      added.
+     *  :param record: ``Ext.data.Record`` The record object(s) corresponding
+     *      to the added layers.
+     *  :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], panelIndex);
+        }
+        this.doLayout();
+    },
+
+    /** private: method[onStoreRemove]
+     *  Private method called when a layer is removed from the store.
+     *
+     *  :param store: ``Ext.data.Store`` The store from which the record(s) was
+     *      removed.
+     *  :param record: ``Ext.data.Record`` The record object(s) corresponding
+     *      to the removed layers.
+     *  :param index: ``Integer`` The index of the removed record.
+     */
+    onStoreRemove: function(store, record, index) {
+        this.removeLegend(record);
+    },
+
+    /** private: method[removeLegend]
+     *  Remove the legend of a layer.
+     *  :param record: ``Ext.data.Record`` The record object from the layer 
+     *      store to remove.
+     */
+    removeLegend: function(record) {
+        var legend = this.getComponent(record.get('layer').id);
+        if (legend) {
+            this.remove(legend, true);
+            this.doLayout();
+        }
+    },
+
+    /** private: method[createLegendSubpanel]
+     *  Create a legend sub panel for the layer.
+     *
+     *  :param record: ``Ext.data.Record`` The record object from the layer
+     *      store.
+     *
+     *  :return: ``Ext.Panel`` The created panel per layer
+     */
+    createLegendSubpanel: function(record) {
+        var layer = record.get('layer');
+        var mainPanel = this.createMainPanel(record);
+        if (mainPanel !== null) {
+            // the default legend can be overridden by specifying a
+            // legendURL property
+            var legend;
+            if (record.get('legendURL')) {
+                legend = new GeoExt.LegendImage({url: record.get('legendURL')});
+                mainPanel.add(legend);
+            } else {
+                var legendGenerator = GeoExt[
+                    "Legend" + layer.CLASS_NAME.split(".").pop()
+                ];
+                if (legendGenerator) {
+                    legend = new legendGenerator({layer: layer});
+                    mainPanel.add(legend);
+                }
+            }
+        }
+        return mainPanel;
+    },
+
+    /** private: method[addLegend]
+     *  Add a legend for the layer.
+     *
+     *  :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, index) {
+        index = index || 0;
+        var layer = record.get('layer');
+        var legendSubpanel = this.createLegendSubpanel(record);
+        if (legendSubpanel !== null) {
+           legendSubpanel.setVisible(layer.getVisibility());
+           this.insert(index, legendSubpanel);
+        }
+    },
+
+    /** private: method[createMainPanel]
+     *  Creates the main panel with a title for the layer.
+     *
+     *  :param record: ``Ext.data.Record`` The record object from the layer
+     *      store.
+     *
+     *  :return: ``Ext.Panel`` The created main panel with a label.
+     */
+    createMainPanel: function(record) {
+        var layer = record.get('layer');
+        var panel = null;
+        var legendGenerator = GeoExt[
+            "Legend" + layer.CLASS_NAME.split(".").pop()
+        ];
+        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;
+    },
+
+    /** private: method[onDestroy]
+     *  Private method called during the destroy sequence.
+     */
+    onDestroy: function() {
+        if(this.layerStore) {
+            this.layerStore.un("add", this.onStoreAdd, this);
+            this.layerStore.un("remove", this.onStoreRemove, this);
+            this.layerStore.un("update", this.onStoreUpdate, this);
+        }
+        GeoExt.LegendPanel.superclass.onDestroy.apply(this, arguments);
+    }
+    
+});
+
+Ext.reg('gx_legendpanel', GeoExt.LegendPanel);
\ No newline at end of file

Added: core/trunk/geoext/lib/GeoExt/widgets/LegendWMS.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendWMS.js	                        (rev 0)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendWMS.js	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,87 @@
+/* Copyright (C) 2008-2009 The Open Source Geospatial Foundation
+ * Published under the BSD license.
+ * See http://geoext.org/svn/geoext/core/trunk/license.txt for the full text
+ * of the license.
+ *
+ * pending approval */
+
+/**
+ * @include GeoExt/widgets/legend/Image.js
+ */
+
+/** api: (define)
+ *  module = GeoExt
+ *  class = LegendWMS
+ */
+Ext.namespace('GeoExt');
+
+/** api: constructor
+ *  .. class:: LegendWMS(config)
+ *
+ *  Show a legend image for a WMS layer.
+ */
+GeoExt.LegendWMS = Ext.extend(Ext.Panel, {
+
+    /** api: config[imageFormat]
+     *  ``String``  
+     *  The image format to request the legend image in.
+     *  Defaults to image/png.
+     */
+    imageFormat: "image/gif",
+
+    /** api: config[layer]
+     *  ``OpenLayers.Layer.WMS``
+     *  The WMS layer to request the legend for.
+     */
+    layer: null,
+
+    /** api: config[bodyBorder]
+     *  ``Boolean``
+     *  Show a border around the legend image or not. Default is false.
+     */
+    bodyBorder: false,
+
+    /** private: method[initComponent]
+     *  Initializes the WMS legend. For group layers it will create multiple
+     *  image box components.
+     */
+    initComponent: function() {
+        GeoExt.LegendWMS.superclass.initComponent.call(this);
+        this.createLegend();
+    },
+
+    /** private: method[getLegendUrl]
+     *  :param layer: ``OpenLayers.Layer.WMS`` The OpenLayers WMS layer object
+     *  :param layerName: ``String`` The name of the layer 
+     *      (used in the LAYERS parameter)
+     *  :return: ``String`` The url of the SLD WMS GetLegendGraphic request.
+     *
+     *  Get the url for the SLD WMS GetLegendGraphic request.
+     */
+    getLegendUrl: function(layerName) {
+        return this.layer.getFullRequestString({
+            REQUEST: "GetLegendGraphic",
+            WIDTH: null,
+            HEIGHT: null,
+            EXCEPTIONS: "application/vnd.ogc.se_xml",
+            LAYER: layerName,
+            LAYERS: null,
+            SRS: null,
+            FORMAT: this.imageFormat
+        });
+    },
+
+    /** private: method[createLegend]
+     *  Add one BoxComponent per sublayer to this panel.
+     */
+    createLegend: function() {
+        var layers = this.layer.params.LAYERS.split(",");
+        for (var i = 0, len = layers.length; i < len; i++){
+            var layerName = layers[i];
+            var legend = new GeoExt.LegendImage({url:
+                this.getLegendUrl(layerName)});
+            this.add(legend);
+        }
+    }
+
+});
\ No newline at end of file

Modified: core/trunk/geoext/lib/GeoExt.js
===================================================================
--- core/trunk/geoext/lib/GeoExt.js	2009-05-25 08:16:45 UTC (rev 866)
+++ core/trunk/geoext/lib/GeoExt.js	2009-05-26 17:11:07 UTC (rev 867)
@@ -77,7 +77,10 @@
             "GeoExt/widgets/tree/LayerNode.js",
             "GeoExt/widgets/tree/LayerContainer.js",
             "GeoExt/widgets/tree/BaseLayerContainer.js",
-            "GeoExt/widgets/tree/OverlayLayerContainer.js"
+            "GeoExt/widgets/tree/OverlayLayerContainer.js",
+            "GeoExt/widgets/LegendImage.js",
+            "GeoExt/widgets/LegendWMS.js",
+            "GeoExt/widgets/LegendPanel.js"
         );
 
         var agent = navigator.userAgent;

Added: core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html	                        (rev 0)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html	2009-05-26 17:11:07 UTC (rev 867)
@@ -0,0 +1,155 @@
+<!DOCTYPE html>
+<html debug="true">
+  <head>
+    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
+    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
+    <script type="text/javascript" src="../../../../../ext/ext-all.js"></script>
+    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>
+
+    <script type="text/javascript">
+
+        function createMap() {
+            var map = new OpenLayers.Map({allOverlays: true});
+            var layer = new OpenLayers.Layer.WMS("test", '/ows', {layers: 'a'});
+            map.addLayer(layer);
+            return map;
+        }
+
+        function loadMapPanel() {
+            var map = createMap();
+
+            mapPanel = new GeoExt.MapPanel({
+                // panel options
+                id: "map-panel",
+                title: "GeoExt MapPanel",
+                renderTo: "mappanel",
+                height: 400,
+                width: 600,
+                // map panel-specific options
+                map: map,
+                center: new OpenLayers.LonLat(5, 45),
+                zoom: 4
+            });
+
+            return mapPanel;
+        }
+
+        function test_legendurl(t) {
+            t.plan(1);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                renderTo: 'legendpanel'});
+            lp.render();
+
+            var newUrl = "http://www.geoext.org//trac/geoext/chrome/site/img/GeoExt.png";
+            mapPanel.layers.getAt(0).set("legendURL", newUrl);
+
+            var item = lp.getComponent(mapPanel.map.layers[0].id);
+            var url = item.items.items[1].items.items[0].getEl().dom.src;
+            t.eq(url, newUrl, "Update the image with the provided legendURL");
+
+            lp.destroy();
+            mapPanel.destroy();
+        }
+
+        function test_togglevisibility(t) {
+            t.plan(2);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                renderTo: 'legendpanel'});
+            lp.render();
+
+            mapPanel.map.layers[0].setVisibility(false);
+            var id = mapPanel.layers.getAt(0).get('layer').id;
+            t.eq(lp.getComponent(id).hidden, true, "Layer has been hidden in legend");
+
+            mapPanel.map.layers[0].setVisibility(true);
+            t.eq(lp.getComponent(id).hidden, false, "Layer has been made visible again in legend");
+
+            lp.destroy();
+            mapPanel.destroy();
+        }
+
+        function test_hide(t) {
+            t.plan(1);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                renderTo: 'legendpanel'});
+            lp.render();
+
+            mapPanel.layers.getAt(0).set("hideInLegend", true);
+            var id = mapPanel.layers.getAt(0).get('layer').id;
+            t.eq(lp.getComponent(id).hidden, true, "Layer has been hidden in legend");
+
+            lp.destroy();
+            mapPanel.destroy();
+        }
+
+        function test_dynamic(t) {
+            t.plan(1);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                dynamic: false,
+                renderTo: 'legendpanel'});
+            lp.render();
+
+            var layer;
+            layer = new OpenLayers.Layer.WMS("test2", '/ows', {layers: 'b', format: 'image/png', transparent: 'TRUE'});
+            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();
+        }
+
+        function test_wms(t) {
+            t.plan(1);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                renderTo: 'legendpanel'});
+            lp.render();
+
+            var item = lp.getComponent(mapPanel.map.layers[0].id);
+            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();
+        }
+
+        function test_addremove(t) {
+            t.plan(4);
+            var mapPanel = loadMapPanel();
+            var lp  = new GeoExt.LegendPanel({
+                renderTo: 'legendpanel'});
+            lp.render();
+            t.eq(lp.items.length, 1, "Same number of layers in legend panel and in map");
+
+            var item = lp.getComponent(mapPanel.map.layers[0].id);
+
+            var layer;
+            layer = new OpenLayers.Layer.WMS("test2", '/ows', {layers: 'b', format: 'image/png', transparent: 'TRUE'});
+            mapPanel.map.addLayer(layer);
+
+            t.eq(lp.items.length, 2, "New WMS layer has been added");
+
+            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 but will be invisible");
+
+            mapPanel.map.removeLayer(mapPanel.map.layers[0]);
+            t.eq(lp.items.length, 2, "Removing the WMS layer really removes the legend from the panel");
+
+            lp.destroy();
+            mapPanel.destroy();
+        }
+
+    </script>
+  <body>
+    <div id="legendpanel"></div>
+    <div id="mappanel"></div>
+  </body>
+</html>

Modified: core/trunk/geoext/tests/list-tests.html
===================================================================
--- core/trunk/geoext/tests/list-tests.html	2009-05-25 08:16:45 UTC (rev 866)
+++ core/trunk/geoext/tests/list-tests.html	2009-05-26 17:11:07 UTC (rev 867)
@@ -16,4 +16,5 @@
   <li>lib/GeoExt/widgets/form/FormPanel.html</li>
   <li>lib/GeoExt/widgets/tree/LayerNode.html</li>
   <li>lib/GeoExt/widgets/tree/LayerContainer.html</li>
+  <li>lib/GeoExt/widgets/LegendPanel.html</li>
 </ul>



More information about the Commits mailing list