[OpenLayers-Commits] r5085 - in trunk/openlayers: lib/OpenLayers/Handler tests/Handler

commits at openlayers.org commits at openlayers.org
Thu Nov 1 09:37:23 EDT 2007


Author: elemoine
Date: 2007-11-01 09:37:16 -0400 (Thu, 01 Nov 2007)
New Revision: 5085

Modified:
   trunk/openlayers/lib/OpenLayers/Handler/Point.js
   trunk/openlayers/lib/OpenLayers/Handler/RegularPolygon.js
   trunk/openlayers/tests/Handler/test_Point.html
   trunk/openlayers/tests/Handler/test_RegularPolygon.html
Log:
handlers have to check if layer exists before destroying it. Thanks pgiraud. (closes #1107)


Modified: trunk/openlayers/lib/OpenLayers/Handler/Point.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Handler/Point.js	2007-11-01 08:31:27 UTC (rev 5084)
+++ trunk/openlayers/lib/OpenLayers/Handler/Point.js	2007-11-01 13:37:16 UTC (rev 5085)
@@ -114,8 +114,14 @@
         if(this.drawing) {
             this.cancel();
         }
-        this.map.removeLayer(this.layer, false);
-        this.layer.destroy();
+        // If a layer's map property is set to null, it means that that layer
+        // isn't added to the map. Since we ourself added the layer to the map
+        // in activate(), we can assume that if this.layer.map is null it means
+        // that the layer has been destroyed (as a result of map.destroy() for
+        // example.
+        if (this.layer.map != null) {
+            this.layer.destroy(false);
+        }
         this.layer = null;
         return true;
     },

Modified: trunk/openlayers/lib/OpenLayers/Handler/RegularPolygon.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Handler/RegularPolygon.js	2007-11-01 08:31:27 UTC (rev 5084)
+++ trunk/openlayers/lib/OpenLayers/Handler/RegularPolygon.js	2007-11-01 13:37:16 UTC (rev 5085)
@@ -164,11 +164,19 @@
             if(this.dragging) {
                 this.cancel();
             }
-            this.map.removeLayer(this.layer, false);
-            this.layer.destroy();
-            if (this.feature) {
-                this.feature.destroy();
-            }    
+            // If a layer's map property is set to null, it means that that
+            // layer isn't added to the map. Since we ourself added the layer
+            // to the map in activate(), we can assume that if this.layer.map
+            // is null it means that the layer has been destroyed (as a result
+            // of map.destroy() for example.
+            if (this.layer.map != null) {
+                this.layer.destroy(false);
+                if (this.feature) {
+                    this.feature.destroy();
+                }
+            }
+            this.layer = null;
+            this.feature = null;
             deactivated = true;
         }
         return deactivated;

Modified: trunk/openlayers/tests/Handler/test_Point.html
===================================================================
--- trunk/openlayers/tests/Handler/test_Point.html	2007-11-01 08:31:27 UTC (rev 5084)
+++ trunk/openlayers/tests/Handler/test_Point.html	2007-11-01 13:37:16 UTC (rev 5085)
@@ -43,6 +43,21 @@
              "deactivate returns true if the handler was active already");
     }
 
+    function test_Handler_Point_deactivation(t) {
+        t.plan(1);
+        var map = new OpenLayers.Map('map');
+        var control = new OpenLayers.Control();
+        map.addControl(control);
+             
+        var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
+        handler.activate();
+        handler.layer.destroy();
+        handler.deactivate();
+        t.eq(handler.layer, null,
+             "deactivate doesn't throw an error if layer was" +
+             " previously destroyed");
+    }
+
     function test_Handler_Point_bounds(t) {
         t.plan(4);
         var map = new OpenLayers.Map('map');

Modified: trunk/openlayers/tests/Handler/test_RegularPolygon.html
===================================================================
--- trunk/openlayers/tests/Handler/test_RegularPolygon.html	2007-11-01 08:31:27 UTC (rev 5084)
+++ trunk/openlayers/tests/Handler/test_RegularPolygon.html	2007-11-01 13:37:16 UTC (rev 5085)
@@ -43,6 +43,21 @@
              "deactivate returns true if the handler was active already");
     }
 
+    function test_Handler_RegularPolygon_deactivation(t) {
+        t.plan(1);
+        var map = new OpenLayers.Map('map');
+        var control = new OpenLayers.Control();
+        map.addControl(control);
+             
+        var handler = new OpenLayers.Handler.RegularPolygon(control, {foo: 'bar'});
+        handler.activate();
+        handler.layer.destroy();
+        handler.deactivate();
+        t.eq(handler.layer, null,
+             "deactivate doesn't throw an error if layer was" +
+             " previously destroyed");
+    }
+
     function test_Handler_RegularPolygon_four_corners(t) {
         t.plan(7);
         var map = new OpenLayers.Map('map');



More information about the Commits mailing list