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

commits at geoext.org commits at geoext.org
Fri Sep 10 17:03:02 CEST 2010


Author: fredj
Date: 2010-09-10 17:03:02 +0200 (Fri, 10 Sep 2010)
New Revision: 2334

Modified:
   core/trunk/geoext/lib/GeoExt/widgets/LayerOpacitySlider.js
   core/trunk/geoext/tests/lib/GeoExt/widgets/LayerOpacitySlider.html
Log:
LayerOpacitySlider listen to opacity change and update the slider accordingly. r=pgiraud (closes #144)

Modified: core/trunk/geoext/lib/GeoExt/widgets/LayerOpacitySlider.js
===================================================================
--- core/trunk/geoext/lib/GeoExt/widgets/LayerOpacitySlider.js	2010-09-10 14:59:30 UTC (rev 2333)
+++ core/trunk/geoext/lib/GeoExt/widgets/LayerOpacitySlider.js	2010-09-10 15:03:02 UTC (rev 2334)
@@ -134,6 +134,7 @@
     constructor: function(config) {
         if (config.layer) {
             this.layer = this.getLayer(config.layer);
+            this.bind();
             this.complementaryLayer = this.getLayer(config.complementaryLayer);
             config.value = (config.value !== undefined) ? 
                 config.value : this.getOpacityValue(this.layer);
@@ -143,14 +144,47 @@
         GeoExt.LayerOpacitySlider.superclass.constructor.call(this, config);
     },
 
+    /** private: method[bind]
+     */
+    bind: function() {
+        if (this.layer && this.layer.map) {
+            this.layer.map.events.on({
+                changelayer: this.update,
+                scope: this
+            });
+        }
+    },
+
+    /** private: method[unbind]
+     */
+    unbind: function() {
+        if (this.layer && this.layer.map) {
+            this.layer.map.events.un({
+                changelayer: this.update,
+                scope: this
+            });
+        }
+    },
+
+    /** private: method[update]
+     *  Registered as a listener for opacity change.  Updates the value of the slider.
+     */
+    update: function(evt) {
+        if (evt.property === "opacity" && evt.layer == this.layer) {
+            this.setValue(this.getOpacityValue(this.layer));
+        }
+    },
+
     /** api: method[setLayer]
      *  :param layer: ``OpenLayers.Layer`` or :class:`GeoExt.data.LayerRecord`
      *
      *  Bind a new layer to the opacity slider.
      */
     setLayer: function(layer) {
+        this.unbind();
         this.layer = this.getLayer(layer);
         this.setValue(this.getOpacityValue(layer));
+        this.bind();
     },
 
     /** private: method[getOpacityValue]
@@ -219,6 +253,7 @@
                 buffer: this.changeVisibilityDelay
             });
         }
+        this.on("beforedestroy", this.unbind, this);
     },
 
     /** private: method[changeLayerOpacity]
@@ -301,6 +336,7 @@
             click: this.stopMouseEvents,
             scope: this
         });
+        this.unbind();
     },
 
     /** private: method[stopMouseEvents]

Modified: core/trunk/geoext/tests/lib/GeoExt/widgets/LayerOpacitySlider.html
===================================================================
--- core/trunk/geoext/tests/lib/GeoExt/widgets/LayerOpacitySlider.html	2010-09-10 14:59:30 UTC (rev 2333)
+++ core/trunk/geoext/tests/lib/GeoExt/widgets/LayerOpacitySlider.html	2010-09-10 15:03:02 UTC (rev 2334)
@@ -301,6 +301,29 @@
             t.eq(slider.getValue(), 60, "Opacity of second layer is used");
             slider.destroy();
         }
+
+        function test_changelayerEvent(t) {
+            t.plan(2);
+            var map = new OpenLayers.Map();
+            var layer1 = new OpenLayers.Layer("1");
+            var layer2 = new OpenLayers.Layer("2");
+            
+            map.addLayers([layer1, layer2]);
+            
+            slider = new GeoExt.LayerOpacitySlider({
+                layer: layer1,
+                renderTo: document.body,
+                value: 100
+            });
+            slider.setLayer(layer1);
+            layer1.setOpacity(0.9);
+            t.eq(slider.getValue(), 90, "value set from changelayer event");
+            layer2.setOpacity(0.1);
+            t.eq(slider.getValue(), 90, "listen to the right layer");
+
+            map.destroy();
+            slider.destroy();
+        }
     </script>
   <body>
   </body>



More information about the Commits mailing list