L.Control.Attribution: encapsulate some code from L.Layer (#7764)

This commit is contained in:
johnd0e
2021-11-11 11:14:21 +03:00
committed by GitHub
parent daeb86b360
commit c1e6751f5c
2 changed files with 15 additions and 8 deletions

View File

@ -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) {

View File

@ -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) {