give layer an "added" and "removed" event. p=mpriour,me r=me (closes #2983)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11052 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
ahocevar
2011-01-22 12:04:40 +00:00
parent f73fb689a8
commit 4a9460b314
5 changed files with 39 additions and 40 deletions

View File

@@ -81,9 +81,15 @@ OpenLayers.Layer = OpenLayers.Class({
* 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

View File

@@ -50,28 +50,6 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
*/
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 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
* 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 @@ OpenLayers.Layer.Bing = OpenLayers.Class(OpenLayers.Layer.XYZ, {
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});
},
/**

View File

@@ -943,6 +943,7 @@ OpenLayers.Map = OpenLayers.Class({
}
this.events.triggerEvent("addlayer", {layer: layer});
layer.events.triggerEvent("added", {map: this, layer: layer});
layer.afterAdd();
},
@@ -1017,6 +1018,7 @@ OpenLayers.Map = OpenLayers.Class({
this.resetLayersZIndex();
this.events.triggerEvent("removelayer", {layer: layer});
layer.events.triggerEvent("removed", {map: this, layer: layer})
},
/**

View File

@@ -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();

View File

@@ -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,