From c1e6751f5c3db1b710b9a63d2ea9919be9cbb574 Mon Sep 17 00:00:00 2001 From: johnd0e Date: Thu, 11 Nov 2021 11:14:21 +0300 Subject: [PATCH] L.Control.Attribution: encapsulate some code from L.Layer (#7764) --- src/control/Control.Attribution.js | 15 +++++++++++++++ src/layer/Layer.js | 8 -------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/control/Control.Attribution.js b/src/control/Control.Attribution.js index 48e738a83..6177c48e6 100644 --- a/src/control/Control.Attribution.js +++ b/src/control/Control.Attribution.js @@ -45,9 +45,24 @@ export var Attribution = Control.extend({ this._update(); + map.on('layeradd', this._addAttribution, this); + return this._container; }, + onRemove: function (map) { + map.off('layeradd', this._addAttribution, this); + }, + + _addAttribution: function (ev) { + if (ev.layer.getAttribution) { + this.addAttribution(ev.layer.getAttribution()); + ev.layer.once('remove', function () { + this.removeAttribution(ev.layer.getAttribution()); + }, this); + } + }, + // @method setPrefix(prefix: String): this // Sets the text before the attributions. setPrefix: function (prefix) { diff --git a/src/layer/Layer.js b/src/layer/Layer.js index 8d4df864c..ab5a8bc69 100644 --- a/src/layer/Layer.js +++ b/src/layer/Layer.js @@ -113,10 +113,6 @@ export var Layer = Evented.extend({ this.onAdd(map); - if (this.getAttribution && map.attributionControl) { - map.attributionControl.addAttribution(this.getAttribution()); - } - this.fire('add'); map.fire('layeradd', {layer: this}); } @@ -189,10 +185,6 @@ Map.include({ layer.onRemove(this); } - if (layer.getAttribution && this.attributionControl) { - this.attributionControl.removeAttribution(layer.getAttribution()); - } - delete this._layers[id]; if (this._loaded) {