[OpenLayers-Commits] r4229 - in trunk/openlayers: lib/OpenLayers lib/OpenLayers/Control tests tests/Control

commits at openlayers.org commits at openlayers.org
Wed Sep 12 00:00:32 EDT 2007


Author: euzuro
Date: 2007-09-12 00:00:31 -0400 (Wed, 12 Sep 2007)
New Revision: 4229

Modified:
   trunk/openlayers/lib/OpenLayers/Control/LayerSwitcher.js
   trunk/openlayers/lib/OpenLayers/Layer.js
   trunk/openlayers/lib/OpenLayers/Map.js
   trunk/openlayers/tests/Control/test_LayerSwitcher.html
   trunk/openlayers/tests/test_Layer.html
Log:
making the layerswitcher a little smarter. Instead of fancy 'noEvent' parameters, we just keep track of the state at each redraw. When asked to redraw, we then check first to see if anything has changed before going ahead with the redraw. Also in this patch, we add a 'visibilitychanged' event to the layer's events object -- upon request by users. (Closes #878)

Modified: trunk/openlayers/lib/OpenLayers/Control/LayerSwitcher.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Control/LayerSwitcher.js	2007-09-12 03:26:35 UTC (rev 4228)
+++ trunk/openlayers/lib/OpenLayers/Control/LayerSwitcher.js	2007-09-12 04:00:31 UTC (rev 4229)
@@ -19,6 +19,14 @@
      */
     activeColor: "darkblue",
     
+    /**  
+     * Property: layerStates 
+     * {Array(Object)} Basically a copy of the "state" of the map's layers 
+     *     the last time the control was drawn. We have this in order to avoid
+     *     unnecessarily redrawing the control.
+     */
+    layerStates: null,
+    
 
   // DOM Elements
   
@@ -86,6 +94,7 @@
      */
     initialize: function(options) {
         OpenLayers.Control.prototype.initialize.apply(this, arguments);
+        this.layerStates = [];
     },
 
     /**
@@ -171,6 +180,32 @@
     },
 
 
+    /**
+     * Method: checkRedraw
+     * Checks if the layer state has changed since the last redraw() call.
+     * 
+     * Returns:
+     * {Boolean} The layer state changed since the last redraw() call. 
+     */
+    checkRedraw: function() {
+        var redraw = false;
+        if ( !this.layerStates.length ||
+             (this.map.layers.length != this.layerStates.length) ) {
+            redraw = true;
+        } else {
+            for (var i=0; i < this.layerStates.length; i++) {
+                var layerState = this.layerStates[i];
+                var layer = this.map.layers[i];
+                if ( (layerState.name != layer.name) || 
+                     (layerState.visibility != layer.visibility) ) {
+                    redraw = true;
+                    break;
+                }    
+            }
+        }    
+        return redraw;
+    },
+    
     /** 
      * Method: redraw
      * Goes through and takes the current state of the Map and rebuilds the
@@ -181,6 +216,11 @@
      * {DOMElement} A reference to the DIV DOMElement containing the control
      */  
     redraw: function() {
+        //if the state hasn't changed since last redraw, no need 
+        // to do anything. Just return the existing div.
+        if (!this.checkRedraw()) { 
+            return this.div; 
+        } 
 
         //clear out previous layers 
         this.clearLayersArray("base");
@@ -189,6 +229,19 @@
         var containsOverlays = false;
         var containsBaseLayers = false;
         
+        // Save state -- for checking layer if the map state changed.
+        // We save this before redrawing, because in the process of redrawing
+        // we will trigger more visibility changes, and we want to not redraw
+        // and enter an infinite loop.
+        this.layerStates = new Array(this.map.layers.length);
+        for (var i = 0; i < this.map.layers.length; i++) {
+            var layer = this.map.layers[i];
+            this.layerStates[i] = {
+                'name': layer.name, 
+                'visibility': layer.visibility
+            };
+        }    
+
         var layers = this.map.layers.slice();
         if (!this.ascending) { layers.reverse(); }
         for( var i = 0; i < layers.length; i++) {
@@ -286,8 +339,7 @@
         if (!this.inputElem.disabled) {
             if (this.inputElem.type == "radio") {
                 this.inputElem.checked = true;
-                this.layer.map.setBaseLayer(this.layer, true);
-                this.layer.map.events.triggerEvent("changebaselayer");
+                this.layer.map.setBaseLayer(this.layer);
             } else {
                 this.inputElem.checked = !this.inputElem.checked;
                 this.layerSwitcher.updateMap();
@@ -329,7 +381,7 @@
         // set the correct visibilities for the overlays
         for(var i=0; i < this.dataLayers.length; i++) {
             var layerEntry = this.dataLayers[i];   
-            layerEntry.layer.setVisibility(layerEntry.inputElem.checked, true);
+            layerEntry.layer.setVisibility(layerEntry.inputElem.checked);
         }
 
     },

Modified: trunk/openlayers/lib/OpenLayers/Layer.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Layer.js	2007-09-12 03:26:35 UTC (rev 4228)
+++ trunk/openlayers/lib/OpenLayers/Layer.js	2007-09-12 04:00:31 UTC (rev 4229)
@@ -32,7 +32,7 @@
      * Constant: EVENT_TYPES
      * {Array(String)} Supported application event types
      */
-    EVENT_TYPES: [ "loadstart", "loadend", "loadcancel"],
+    EVENT_TYPES: [ "loadstart", "loadend", "loadcancel", "visibilitychanged"],
         
     /**
      * APIProperty: events``
@@ -497,17 +497,16 @@
      * 
      * Parameters:
      * visible - {Boolean} Whether or not to display the layer (if in range)
-     * noEvent - {Boolean}
      */
-    setVisibility: function(visibility, noEvent) {
+    setVisibility: function(visibility) {
         if (visibility != this.visibility) {
             this.visibility = visibility;
             this.display(visibility);
             this.redraw();
-            if ((this.map != null) && 
-                ((noEvent == null) || (noEvent == false))) {
+            if (this.map != null) {
                 this.map.events.triggerEvent("changelayer");
             }
+            this.events.triggerEvent("visibilitychanged");
         }
     },
 

Modified: trunk/openlayers/lib/OpenLayers/Map.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Map.js	2007-09-12 03:26:35 UTC (rev 4228)
+++ trunk/openlayers/lib/OpenLayers/Map.js	2007-09-12 04:00:31 UTC (rev 4229)
@@ -660,9 +660,8 @@
      * 
      * Parameters:
      * newBaseLayer - {<OpenLayers.Layer>}
-     * noEvent - {Boolean}
      */
-    setBaseLayer: function(newBaseLayer, noEvent) {
+    setBaseLayer: function(newBaseLayer) {
         var oldExtent = null;
         if(this.baseLayer) {
             oldExtent = this.baseLayer.getExtent();
@@ -675,7 +674,7 @@
 
                 // make the old base layer invisible 
                 if (this.baseLayer != null) {
-                    this.baseLayer.setVisibility(false, noEvent);
+                    this.baseLayer.setVisibility(false);
                 }
 
                 // set new baselayer and make it visible
@@ -701,9 +700,7 @@
                     }
                 }
 
-                if ((noEvent == null) || (noEvent == false)) {
-                    this.events.triggerEvent("changebaselayer");
-                }
+                this.events.triggerEvent("changebaselayer");
             }        
         }
     },

Modified: trunk/openlayers/tests/Control/test_LayerSwitcher.html
===================================================================
--- trunk/openlayers/tests/Control/test_LayerSwitcher.html	2007-09-12 03:26:35 UTC (rev 4228)
+++ trunk/openlayers/tests/Control/test_LayerSwitcher.html	2007-09-12 04:00:31 UTC (rev 4229)
@@ -65,7 +65,7 @@
 
     function test_04_Control_LayerSwitcher_redraw (t) {
 
-        t.plan( 8 );
+        t.plan( 12 );
 
         map = new OpenLayers.Map('map');
         var layer = new OpenLayers.Layer.WMS("WMS", 
@@ -92,6 +92,16 @@
         t.eq(markersInput.name, markers.name, "wms correctly named");
         t.eq(markersInput.value, markers.name, "wms correctly valued");
 
+        t.eq(false, control.checkRedraw(), "check redraw is false");
+        
+        control = new OpenLayers.Control.LayerSwitcher();
+        control.redraw = function() { 
+            t.ok(true, "redraw called when setting vis");
+        }
+        map.addControl(control); 
+        markers.setVisibility(false); 
+        t.eq(control.checkRedraw(), true, "check redraw is true after changing layer and not letting redraw happen.");
+
     }
     function test_05_Control_LayerSwitcher_ascendingw (t) {
 

Modified: trunk/openlayers/tests/test_Layer.html
===================================================================
--- trunk/openlayers/tests/test_Layer.html	2007-09-12 03:26:35 UTC (rev 4228)
+++ trunk/openlayers/tests/test_Layer.html	2007-09-12 04:00:31 UTC (rev 4229)
@@ -118,7 +118,7 @@
 
     function test_05_Layer_visibility(t) {
 
-        t.plan(5)
+        t.plan(7);
 
         var layer = new OpenLayers.Layer('Test Layer');
     
@@ -140,6 +140,10 @@
         layermoved = false; 
         layer.moveTo = function() { layermoved = true; }
         
+        layer.events.register('visibilitychanged', t, function() {
+            this.ok(true, "Visibility changed calls layer event.");
+        });    
+        
         layer.setVisibility(false);
         t.eq(layermoved, false, "Layer didn't move when calling setvis false");
         



More information about the Commits mailing list