[Commits] r1344 - in core/trunk/geoext: lib/GeoExt/widgets tests/lib/GeoExt/widgets
commits at geoext.org
commits at geoext.org
Tue Sep 1 07:52:37 CEST 2009
Author: bartvde
Date: 2009-09-01 07:52:36 +0200 (Tue, 01 Sep 2009)
New Revision: 1344
Modified:
core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
Log:
add filter option for legend panel, so that e.g. base layers can be hidden from the legend, r=ahocevar (closes #135)
Modified: core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js 2009-08-31 07:07:26 UTC (rev 1343)
+++ core/trunk/geoext/lib/GeoExt/widgets/LegendPanel.js 2009-09-01 05:52:36 UTC (rev 1344)
@@ -63,6 +63,22 @@
* the legend image.
*/
+ /** api: config[filter]
+ * ``Function``
+ * A function, called in the scope of the legend panel, with a layer record
+ * as argument. Is expected to return true for layers to be displayed, false
+ * otherwise. By default, all layers will be displayed.
+ *
+ * .. code-block:: javascript
+ *
+ * filter: function(record) {
+ * return record.get("layer").isBaseLayer;
+ * }
+ */
+ filter: function(record) {
+ return true;
+ },
+
/** private: method[initComponent]
* Initializes the legend panel.
*/
@@ -237,12 +253,14 @@
* :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);
+ if (this.filter(record) === true) {
+ index = index || 0;
+ var layer = record.get('layer');
+ var legendSubpanel = this.createLegendSubpanel(record);
+ if (legendSubpanel !== null) {
+ legendSubpanel.setVisible(layer.getVisibility());
+ this.insert(index, legendSubpanel);
+ }
}
},
Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html 2009-08-31 07:07:26 UTC (rev 1343)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/LegendPanel.html 2009-09-01 05:52:36 UTC (rev 1344)
@@ -188,6 +188,44 @@
mapPanel.destroy();
}
+ function test_filter(t) {
+ t.plan(2);
+
+ var map = new OpenLayers.Map();
+ var layer = new OpenLayers.Layer.WMS("test", '/ows', {layers: 'a'}, {isBaseLayer: true});
+ map.addLayer(layer);
+ layer = new OpenLayers.Layer.WMS("test2", '/ows', {layers: 'b'}, {isBaseLayer: false});
+ map.addLayer(layer);
+
+ var mapPanel = new GeoExt.MapPanel({
+ map: map,
+ id: "map-panel",
+ title: "GeoExt MapPanel",
+ renderTo: "mappanel",
+ height: 400,
+ width: 600
+ });
+
+ var lp = new GeoExt.LegendPanel({
+ filter: function(record) { return !record.get("layer").isBaseLayer; },
+ renderTo: 'legendpanel'});
+ lp.render();
+
+ t.eq(lp.items.length, 1, "Using the filter function only the non base layers will be shown in the legend");
+
+ lp.destroy();
+
+ var lp = new GeoExt.LegendPanel({
+ renderTo: 'legendpanel'});
+ lp.render();
+
+ t.eq(lp.items.length, 2, "With no filter both layers are drawn");
+
+ lp.destroy();
+ mapPanel.destroy();
+
+ }
+
</script>
<body>
<div id="legendpanel"></div>
More information about the Commits
mailing list