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

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Jul 5 03:48:29 EDT 2011


Author: marcjansen
Date: 2011-07-05 00:48:28 -0700 (Tue, 05 Jul 2011)
New Revision: 12154

Modified:
   trunk/openlayers/lib/OpenLayers/Map.js
   trunk/openlayers/tests/Map.html
Log:
Give OpenLayers.Map a preremovelayer-event. Thanks to mosesonline for the patch. p=mosesonline, r=me (fixes #3306)

Modified: trunk/openlayers/lib/OpenLayers/Map.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Map.js	2011-07-04 20:48:12 UTC (rev 12153)
+++ trunk/openlayers/lib/OpenLayers/Map.js	2011-07-05 07:48:28 UTC (rev 12154)
@@ -58,9 +58,14 @@
      * Supported map event types:
      *  - *preaddlayer* triggered before a layer has been added.  The event
      *      object will include a *layer* property that references the layer  
-     *      to be added.
+     *      to be added. When a listener returns "false" the adding will be 
+     *      aborted.
      *  - *addlayer* triggered after a layer has been added.  The event object
      *      will include a *layer* property that references the added layer.
+     *  - *preremovelayer* triggered before a layer has been removed. The event
+     *      object will include a *layer* property that references the layer  
+     *      to be removed. When a listener returns "false" the removal will be 
+     *      aborted.
      *  - *removelayer* triggered after a layer has been removed.  The event
      *      object will include a *layer* property that references the removed
      *      layer.
@@ -81,7 +86,8 @@
      *  - *changebaselayer* triggered after the base layer changes
      */
     EVENT_TYPES: [ 
-        "preaddlayer", "addlayer", "removelayer", "changelayer", "movestart",
+        "preaddlayer", "addlayer","preremovelayer", "removelayer", 
+        "changelayer", "movestart",
         "move", "moveend", "zoomend", "popupopen", "popupclose",
         "addmarker", "removemarker", "clearmarkers", "mouseover",
         "mouseout", "mousemove", "dragstart", "drag", "dragend",
@@ -957,13 +963,13 @@
                 return false;
             }
         }
+        if (this.events.triggerEvent("preaddlayer", {layer: layer}) === false) {
+            return;
+        }
         if(this.allOverlays) {
             layer.isBaseLayer = false;
         }
 
-        if (this.events.triggerEvent("preaddlayer", {layer: layer}) === false) {
-            return;
-        }
         
         layer.div.className = "olLayerDiv";
         layer.div.style.overflow = "";
@@ -1034,6 +1040,9 @@
      * setNewBaseLayer - {Boolean} Default is true
      */
     removeLayer: function(layer, setNewBaseLayer) {
+        if (this.events.triggerEvent("preremovelayer", {layer: layer}) === false) {
+            return;
+        }
         if (setNewBaseLayer == null) {
             setNewBaseLayer = true;
         }

Modified: trunk/openlayers/tests/Map.html
===================================================================
--- trunk/openlayers/tests/Map.html	2011-07-04 20:48:12 UTC (rev 12153)
+++ trunk/openlayers/tests/Map.html	2011-07-05 07:48:28 UTC (rev 12154)
@@ -802,7 +802,40 @@
 
         map.destroy();
     }
-
+    
+	function test_Map_removeLayer_preremovelayer(t) {
+        t.plan(4);
+        map = new OpenLayers.Map('map');
+		
+        map.addLayer(new OpenLayers.Layer());
+        map.removeLayer(map.layers[0]);
+        
+        // one test: standard behaviour without listener
+        t.eq(map.layers.length, 0, "without registered preremovelayer-listener layers can be removed as usual");
+        
+        var callCnt = 0;
+        
+        map.events.register('preremovelayer', this, function(evt) {
+            callCnt++;
+            return !(evt.layer.name === 'donotremove');
+        });
+        var layer1 = new OpenLayers.Layer('donotremove');
+        var layer2 = new OpenLayers.Layer('doremove');
+		
+        map.addLayers([layer1,layer2]);
+		
+        // two tests: remove action can be canceled
+        map.removeLayer(layer1);
+        t.eq(map.layers.length, 2, "layer is not removed since preremovelayer returns false");
+        map.removeLayer(layer2);
+        t.eq(map.layers.length, 1, "layer is removed since preremovelayer returns true");
+        
+        // one test: listener was called twice
+        t.eq(callCnt, 2, "preremovelayer-listener was called exactly twice");
+        
+        map.destroy();
+	}
+    
     function test_Map_setBaseLayer_after_pan (t) {
         t.plan(1);
 



More information about the Commits mailing list