From b1f70b5c1c1a24ef698b5838d883fa5766208754 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Mon, 24 Feb 2014 20:18:44 +0200 Subject: [PATCH] fix layer.addTo(group), add layer removeFrom, #2420 --- debug/map/map.html | 22 ++++----- src/layer/Layer.js | 111 +++++++++++++++++++++++---------------------- src/map/Map.js | 4 +- 3 files changed, 69 insertions(+), 68 deletions(-) diff --git a/debug/map/map.html b/debug/map/map.html index 60544a76..2bcd369e 100644 --- a/debug/map/map.html +++ b/debug/map/map.html @@ -33,28 +33,26 @@ function populate() { for (var i = 0; i < 10; i++) { - markers.addLayer(new L.Marker(getRandomLatLng(map))); + L.marker(getRandomLatLng(map)).addTo(markers); } return false; } - markers.bindPopup("

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.

Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.

"); - - map.addLayer(markers); + markers.bindPopup("

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.

Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque.

").addTo(map); populate(); L.DomUtil.get('populate').onclick = populate; function logEvent(e) { console.log(e.type); } - map.on('click', logEvent); -// -// map.on('movestart', logEvent); -// map.on('move', logEvent); -// map.on('moveend', logEvent); -// -// map.on('zoomstart', logEvent); -// map.on('zoomend', logEvent); + // map.on('click', logEvent); + + // map.on('movestart', logEvent); + // map.on('move', logEvent); + // map.on('moveend', logEvent); + + // map.on('zoomstart', logEvent); + // map.on('zoomend', logEvent); diff --git a/src/layer/Layer.js b/src/layer/Layer.js index 6938300b..32b2c44b 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -6,30 +6,33 @@ L.Layer = L.Evented.extend({ }, addTo: function (map) { - var id = L.stamp(this); - if (map._layers[id]) { return this; } - map._layers[id] = this; - - this._zoomAnimated = map._zoomAnimated; - - if (this.beforeAdd) { - this.beforeAdd(map); - } - - this._mapToAdd = map; - map.whenReady(this._layerAdd, this); - + map.addLayer(this); return this; }, - _layerAdd: function () { - var map = this._mapToAdd; + remove: function () { + return this.removeFrom(this._map || this._mapToAdd); + }, + + removeFrom: function (obj) { + if (obj) { + obj.removeLayer(this); + } + return this; + }, + + getPane: function (name) { + return this._map.getPane(name ? (this.options[name] || name) : this.options.pane); + }, + + _layerAdd: function (e) { + var map = e.target; // check in case layer gets added and then removed before the map is ready - if (!map) { return; } + if (!map.hasLayer(this)) { return; } this._map = map; - this._mapToAdd = null; + this._zoomAnimated = map._zoomAnimated; this.onAdd(map); @@ -43,53 +46,53 @@ L.Layer = L.Evented.extend({ this.fire('add'); map.fire('layeradd', {layer: this}); - }, - - remove: function () { - - var id = L.stamp(this), - map = this._map || this._mapToAdd; - - if (!map || !map._layers[id]) { return this; } - - if (map._loaded) { - this.onRemove(map); - } - - if (this.getAttribution && map.attributionControl) { - map.attributionControl.removeAttribution(this.getAttribution()); - } - - if (this.getEvents) { - map.off(this.getEvents(), this); - } - - delete map._layers[id]; - - if (map._loaded) { - map.fire('layerremove', {layer: this}); - this.fire('remove'); - } - - this._map = this._mapToAdd = null; - - return this; - }, - - getPane: function (name) { - return this._map.getPane(name ? (this.options[name] || name) : this.options.pane); } }); L.Map.include({ addLayer: function (layer) { - layer.addTo(this); + var id = L.stamp(layer); + if (this._layers[id]) { return layer; } + this._layers[id] = layer; + + layer._mapToAdd = this; + + if (layer.beforeAdd) { + layer.beforeAdd(this); + } + + this.whenReady(layer._layerAdd, layer); + return this; }, removeLayer: function (layer) { - layer.remove(); + var id = L.stamp(layer); + + if (!this._layers[id]) { return this; } + + if (this._loaded) { + layer.onRemove(this); + } + + if (layer.getAttribution && this.attributionControl) { + this.attributionControl.removeAttribution(layer.getAttribution()); + } + + if (layer.getEvents) { + this.off(layer.getEvents(), layer); + } + + delete this._layers[id]; + + if (this._loaded) { + this.fire('layerremove', {layer: layer}); + layer.fire('remove'); + } + + layer._map = layer._mapToAdd = null; + return this; }, diff --git a/src/map/Map.js b/src/map/Map.js index 00cdfc15..9f0b54cf 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -222,7 +222,7 @@ L.Map = L.Evented.extend({ } this._clearHandlers(); - + if (this._loaded) { this.fire('unload'); } @@ -598,7 +598,7 @@ L.Map = L.Evented.extend({ whenReady: function (callback, context) { if (this._loaded) { - callback.call(context || this, this); + callback.call(context || this, {target: this}); } else { this.on('load', callback, context); }