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

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Sat Jan 22 07:04:40 EST 2011


Author: ahocevar
Date: 2011-01-22 04:04:40 -0800 (Sat, 22 Jan 2011)
New Revision: 11052

Modified:
   trunk/openlayers/lib/OpenLayers/Layer.js
   trunk/openlayers/lib/OpenLayers/Layer/Bing.js
   trunk/openlayers/lib/OpenLayers/Map.js
   trunk/openlayers/tests/Layer.html
   trunk/openlayers/tests/Map.html
Log:
give layer an "added" and "removed" event. p=mpriour,me r=me (closes #2983)


Modified: trunk/openlayers/lib/OpenLayers/Layer/Bing.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Layer/Bing.js	2011-01-21 13:33:59 UTC (rev 11051)
+++ trunk/openlayers/lib/OpenLayers/Layer/Bing.js	2011-01-22 12:04:40 UTC (rev 11052)
@@ -51,28 +51,6 @@
     metadataParams: null,
 
     /**
-     * Constant: EVENT_TYPES
-     * {Array(String)} Supported application event types.  Register a listener
-     *     for a particular event with the following syntax:
-     * (code)
-     * layer.events.register(type, obj, listener);
-     * (end)
-     *
-     * Listeners will be called with a reference to an event object.  The
-     *     properties of this event depends on exactly what happened.
-     *
-     * All event objects have at least the following properties:
-     * object - {Object} A reference to layer.events.object.
-     * element - {DOMElement} A reference to layer.events.element.
-     *
-     * Supported map event types (in addition to those from <OpenLayers.Layer>):
-     * added - Triggered after the layer is added to a map.  Listeners
-     *      will receive an object with a *map* property referencing the
-     *      map and a *layer* property referencing the layer.
-     */
-    EVENT_TYPES: ["added"],
-
-    /**
      * Constructor: OpenLayers.Layer.Bing
      * Create a new Bing layer.
      *
@@ -98,12 +76,6 @@
      * Any other documented layer properties can be provided in the config object.
      */
     initialize: function(options) {
-        // concatenate events specific to vector with those from the base
-        this.EVENT_TYPES =
-            OpenLayers.Layer.Bing.prototype.EVENT_TYPES.concat(
-            OpenLayers.Layer.prototype.EVENT_TYPES
-        );
-        
         options = OpenLayers.Util.applyDefaults({
             zoomOffset: 1,
             maxResolution: 78271.51695,
@@ -245,9 +217,6 @@
             this.updateAttribution();
         }
         this.map.events.register("moveend", this, this.updateAttribution);
-        // TODO: move this event to Layer
-        // http://trac.osgeo.org/openlayers/ticket/2983
-        this.events.triggerEvent("added", {map: this.map, layer: this});
     },
     
     /**

Modified: trunk/openlayers/lib/OpenLayers/Layer.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Layer.js	2011-01-21 13:33:59 UTC (rev 11051)
+++ trunk/openlayers/lib/OpenLayers/Layer.js	2011-01-22 12:04:40 UTC (rev 11052)
@@ -81,9 +81,15 @@
      * moveend - Triggered when layer is done moving, object passed as
      *     argument has a zoomChanged boolean property which tells that the
      *     zoom has changed.
+     * added - Triggered after the layer is added to a map.  Listeners will
+     *     receive an object with a *map* property referencing the map and a
+     *     *layer* property referencing the layer.
+     * removed - Triggered after the layer is removed from the map.  Listeners
+     *     will receive an object with a *map* property referencing the map and
+     *     a *layer* property referencing the layer.
      */
     EVENT_TYPES: ["loadstart", "loadend", "loadcancel", "visibilitychanged",
-                  "move", "moveend"],
+                  "move", "moveend", "added", "removed"],
 
     /**
      * Constant: RESOLUTION_PROPERTIES

Modified: trunk/openlayers/lib/OpenLayers/Map.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Map.js	2011-01-21 13:33:59 UTC (rev 11051)
+++ trunk/openlayers/lib/OpenLayers/Map.js	2011-01-22 12:04:40 UTC (rev 11052)
@@ -943,6 +943,7 @@
         }
 
         this.events.triggerEvent("addlayer", {layer: layer});
+		layer.events.triggerEvent("added", {map: this, layer: layer});
         layer.afterAdd();
     },
 
@@ -1017,6 +1018,7 @@
         this.resetLayersZIndex();
 
         this.events.triggerEvent("removelayer", {layer: layer});
+		layer.events.triggerEvent("removed", {map: this, layer: layer})
     },
 
     /**

Modified: trunk/openlayers/tests/Layer.html
===================================================================
--- trunk/openlayers/tests/Layer.html	2011-01-21 13:33:59 UTC (rev 11051)
+++ trunk/openlayers/tests/Layer.html	2011-01-22 12:04:40 UTC (rev 11052)
@@ -751,16 +751,27 @@
     
     function test_afterAdd(t) {
         
-        t.plan(1);
+        t.plan(4);
         
+        var log = [];
         var map = new OpenLayers.Map("map");
-        var layer = new OpenLayers.Layer(null, {isBaseLayer: true});
+        var layer = new OpenLayers.Layer(null, {
+            isBaseLayer: true,
+            eventListeners: {
+                "added": function(evt) {
+                    log.push(evt);
+                }
+            }
+        });
         var hasBase = false;
         layer.afterAdd = function() {
             hasBase = !!(layer.map && layer.map.baseLayer);
         }
         map.addLayer(layer);
         t.eq(hasBase, true, "when afterAdd is called, map has a base layer");
+        t.eq(log.length, 1, "added event triggered");
+        t.eq(log[0].map.id, map.id, "added listener argument with correct map");
+        t.eq(log[0].layer.id, layer.id, "added listener argument with correct layer");
         
     }
     
@@ -806,11 +817,18 @@
 
 
     function test_Layer_destroy (t) {
-        t.plan( 5 );    
+        t.plan( 8 );    
 
+        var log = [];
         var map = new OpenLayers.Map('map');
         
-        layer = new OpenLayers.Layer('Test Layer');
+        layer = new OpenLayers.Layer('Test Layer', {
+            eventListeners: {
+                "removed": function(evt) {
+                    log.push(evt);
+                }
+            }
+        });
 
         map.addLayer(layer);
 
@@ -822,6 +840,9 @@
         t.eq( layer.options, null, "layer.options is null after destroy" );
 
         t.eq(map.layers.length, 0, "layer removed from map");
+        t.eq(log.length, 1, "removed event triggered");
+        t.eq(log[0].map.id, map.id, "removed listener argument with correct map");
+        t.eq(log[0].layer.id, layer.id, "removed listener argument with correct layer");
         
         map.destroy();
 

Modified: trunk/openlayers/tests/Map.html
===================================================================
--- trunk/openlayers/tests/Map.html	2011-01-21 13:33:59 UTC (rev 11051)
+++ trunk/openlayers/tests/Map.html	2011-01-22 12:04:40 UTC (rev 11052)
@@ -699,11 +699,12 @@
     function test_Map_removeLayer(t) {
         t.plan(1);
         var f = function() {};
+        var events = {triggerEvent: f};
         var layers = [
-            {name: "fee", removeMap: f},
-            {name: "fi", removeMap: f},
-            {name: "fo", removeMap: f},
-            {name: "fum", removeMap: f}
+            {name: "fee", removeMap: f, events: events},
+            {name: "fi", removeMap: f, events: events},
+            {name: "fo", removeMap: f, events: events},
+            {name: "fum", removeMap: f, events: events}
         ];
         var map = {
             layers: layers,



More information about the Commits mailing list