[Commits] r1327 - in sandbox/camptocamp/geobretagne: lib/GeoExt/widgets tests/lib/GeoExt/widgets

commits at geoext.org commits at geoext.org
Wed Aug 12 15:45:33 CEST 2009


Author: elemoine
Date: 2009-08-12 15:45:33 +0200 (Wed, 12 Aug 2009)
New Revision: 1327

Modified:
   sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendImage.js
   sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendPanel.js
   sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendWMS.js
   sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendPanel.html
   sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendWMS.html
Log:
applying patch-147-r1326-A0.diff


Modified: sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendImage.js
===================================================================
--- sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendImage.js	2009-08-11 14:51:54 UTC (rev 1326)
+++ sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendImage.js	2009-08-12 13:45:33 UTC (rev 1327)
@@ -53,12 +53,12 @@
         };
     },
 
-    /** api: method[setUrl]
-     *  :param url: ``String`` The new url of the image.
+    /** api: method[updateLegend]
+     *  :param url: ``String`` The new URL.
      *  
      *  Sets the url of the image.
      */
-    setUrl: function(url) {
+    updateLegend: function(url) {
         this.url = url;
         var el = this.getEl();
         if (el) {
@@ -75,7 +75,7 @@
     onRender: function(ct, position) {
         GeoExt.LegendImage.superclass.onRender.call(this, ct, position);
         if(this.url) {
-            this.setUrl(this.url);
+            this.updateLegend(this.url);
         }
     },
 

Modified: sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendPanel.js
===================================================================
--- sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendPanel.js	2009-08-11 14:51:54 UTC (rev 1326)
+++ sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendPanel.js	2009-08-12 13:45:33 UTC (rev 1327)
@@ -143,12 +143,11 @@
         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'));
-                }
-            }
+            var url = record.get("legendURL") != null ?
+                      record.get("legendURL") : undefined;
+            // the actual legend panel is the second item in
+            // the main panel
+            legend.items.get(1).updateLegend(url);
         }
     },
 

Modified: sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendWMS.js
===================================================================
--- sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendWMS.js	2009-08-11 14:51:54 UTC (rev 1326)
+++ sandbox/camptocamp/geobretagne/lib/GeoExt/widgets/LegendWMS.js	2009-08-12 13:45:33 UTC (rev 1327)
@@ -71,57 +71,107 @@
         if(!this.layer) {
             this.layer = this.record.get("layer");
         }
-        this.createLegend();
+        this.updateLegend();
     },
 
     /** 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.
+     *  :param layerName: ``String`` A sublayer.
+     *  :param layerNames: ``Array(String)`` The array of sublayers,
+     *      read from this.layer if not provided.
+     *  :return: ``String`` The legend URL.
      *
-     *  Get the url for the SLD WMS GetLegendGraphic request.
+     *  Get the legend URL of a sublayer.
      */
-    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
+    getLegendUrl: function(layerName, layerNames) {
+        var url;
+ 
+        // check if we have a legend URL in the record's
+        // "styles" data field
+        var styles = this.record && this.record.get("styles");
+        if(styles && styles.length > 0) {
+            layerNames = layerNames ||
+                         (this.layer.params.LAYERS instanceof Array) ?
+                            this.layer.params.LAYERS :
+                            this.layer.params.LAYERS.split(",");
+
+            var styleNames = this.layer.params.STYLES &&
+                             this.layer.params.STYLES.split(",");
+
+            var idx = layerNames.indexOf(layerName);
+            var styleName = styleNames && styleNames[idx];
+
+            if(styleName) {
+                Ext.each(styles, function(s) {
+                    url = (s.name == styleName && s.legend) && s.legend.href;
+                    return !url;
+                })
+            } else if(this.defaultStyleIsFirst === true &&
+                      !styleNames && !this.layer.params.SLD) {
+                url = styles[0].legend && styles[0].legend.href;
+            }
+        }
+
+        return url ||
+               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.
+    /** private: method[updateLegend]
+     *  :param url: ``String`` The legend URL, derived from the
+     *      layer record or layer params (WMS GetLegendGraphic)
+     *      if not provided.
+     *
+     *  Update the legend panel, adding, removing or updating
+     *  the per-sublayer box component.
      */
-    createLegend: function() {
-        var layers = (this.layer.params.LAYERS instanceof Array) ? 
-            this.layer.params.LAYERS : this.layer.params.LAYERS.split(",");
-        var styleNames = this.layer.params.STYLES &&
-            this.layer.params.STYLES.split(",");
-        var styles = this.record && this.record.get("styles");
-        var url, layerName, styleName;
-        for (var i = 0, len = layers.length; i < len; i++){
-            layerName = layers[i];
-            if(styles && styles.length > 0) {
-                styleName = styleNames && styleNames[i];
-                if(styleName) {
-                    Ext.each(styles, function(s) {
-                        url = (s.name == styleName && s.legend) && s.legend.href;
-                        return !url;
-                    })
-                } else if(this.defaultStyleIsFirst === true){
-                    url = styles[0].legend && styles[0].legend.href;
+    updateLegend: function(url) {
+        var layerNames, layerName, i, len;
+        
+        layerNames = (this.layer.params.LAYERS instanceof Array) ? 
+            this.layer.params.LAYERS :
+            this.layer.params.LAYERS.split(",");
+
+        if(this.items) {
+            var destroyList = [];
+            this.items.each(function(cmp) {
+                i = layerNames.indexOf(cmp.itemId);
+                if(i < 0) {
+                    destroyList.push(cmp);
+                } else {
+                    layerName = layerNames[i];
+                    var newUrl = url ||
+                                 this.getLegendUrl(layerName, layerNames);
+                    if(!OpenLayers.Util.isEquivalentUrl(newUrl, cmp.url)) {
+                        cmp.updateLegend(newUrl);
+                    }
                 }
+            }, this);
+            for(i = 0, len = destroyList.length; i<len; i++) {
+                var cmp = destroyList[i];
+                // cmp.destroy() deos not remove the cmp from
+                // its parent container!
+                this.remove(cmp);
+                cmp.destroy();
             }
-            var legend = new GeoExt.LegendImage({url:
-                url || this.getLegendUrl(layerName)});
-            this.add(legend);
         }
+
+        for(i = 0, len = layerNames.length; i<len; i++) {
+            layerName = layerNames[i];
+            if(!this.items || !this.getComponent(layerName)) {
+                this.add({
+                    xtype: "gx_legendimage",
+                    url: url || this.getLegendUrl(layerName, layerNames),
+                    itemId: layerName
+                });
+            }
+        }
     }
-
 });

Modified: sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendPanel.html
===================================================================
--- sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendPanel.html	2009-08-11 14:51:54 UTC (rev 1326)
+++ sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendPanel.html	2009-08-12 13:45:33 UTC (rev 1327)
@@ -188,6 +188,29 @@
             mapPanel.destroy();
         }
 
+        function test_changelayerparams(t) {
+            t.plan(2);
+
+            var mapPanel = loadMapPanel();
+            var lp = new GeoExt.LegendPanel({
+                renderTo: "legendpanel"
+            });
+            var layer = mapPanel.map.layers[0];
+            var cmp = lp.getComponent(layer.id);
+
+            layer.mergeNewParams({
+                layers: "a,b"
+            });
+            t.eq(cmp.items.get(1).items.getCount(), 2,
+                 "mergeNewParams caused addition of a legend image");
+
+            layer.mergeNewParams({
+                layers: "b"
+            });
+            t.eq(cmp.items.get(1).items.getCount(), 1,
+                 "mergeNewParams caused removal of a legend image");
+        }
+
     </script>
   <body>
     <div id="legendpanel"></div>

Modified: sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendWMS.html
===================================================================
--- sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendWMS.html	2009-08-11 14:51:54 UTC (rev 1326)
+++ sandbox/camptocamp/geobretagne/tests/lib/GeoExt/widgets/LegendWMS.html	2009-08-12 13:45:33 UTC (rev 1327)
@@ -69,7 +69,89 @@
             mapPanel.destroy();
         }
 
+        function test_updateLegend(t) {
+            t.plan(8);
 
+            // set up
+
+            var mapPanel = loadMapPanel();
+            var layerRecord = mapPanel.layers.getAt(0);
+
+            var url, expectedUrl, cmp;
+
+            var l = new GeoExt.LegendWMS({
+                renderTo: 'legendwms',
+                record: layerRecord
+            });
+            item = l.items.get(0);
+
+            // test
+
+            // #1
+            layerRecord.get("layer").mergeNewParams({
+                layers: "b"
+            });
+            l.updateLegend();
+            t.ok(!l.getComponent("a"),
+                 "updateLegend removes old components");
+            t.ok(l.getComponent("b"),
+                 "updateLegend adds new components");
+
+            // #2
+            layerRecord.get("layer").mergeNewParams({
+                layers: "b,c",
+                foo: "bar"
+            });
+            l.updateLegend();
+            t.ok(l.getComponent("b"),
+                 "updateLegend does not remove components to be updated");
+            expectedUrl = "/ows?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fgif&FOO=bar&LAYER=b";
+            t.eq(l.getComponent("b").url, expectedUrl,
+                 "updateLegend updates component URL");
+            expectedUrl = "/ows?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&STYLES=&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fgif&FOO=bar&LAYER=c";
+            t.eq(l.getComponent("c").url, expectedUrl,
+                 "updateLegend sets correct URL in new component");
+
+            // #3
+            layerRecord.get("layer").mergeNewParams({
+                layers: "c",
+                styles: "style1"
+            });
+            expectedUrl = "/ows?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&STYLES=style1&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fgif&FOO=bar&LAYER=c";
+            l.updateLegend();
+            t.eq(l.getComponent("c").url, expectedUrl,
+                 "updateLegend sets correct STYLES params in URL");
+
+            // #4
+            layerRecord.set("styles", [{
+                name: "style1",
+                legend: {
+                    href: "http://url-to-legend.org/"
+                }
+            }]);
+            l.updateLegend();
+            expectedUrl = "http://url-to-legend.org/";
+            t.eq(l.getComponent("c").url, expectedUrl,
+                 "updateLegend uses the legend href from the styles field");
+
+            // #5
+            layerRecord.get("layer").mergeNewParams({
+                layers: "c",
+                styles: null,
+                sld: "http://url-to-sld.org/"
+            });
+            l.updateLegend();
+            expectedUrl = "/ows?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetLegendGraphic&EXCEPTIONS=application%2Fvnd.ogc.se_xml&FORMAT=image%2Fgif&FOO=bar&SLD=http%3A%2F%2Furl-to-sld.org%2F&LAYER=c";
+            t.eq(l.getComponent("c").url, expectedUrl,
+                 "updateLegend does not use the legend href from the " +
+                 "styles field if SLD is set in the layer params");
+
+            // tear down
+
+            l.destroy()
+        }
+
+
     </script>
   <body>
     <div id="legendwms"></div>



More information about the Commits mailing list