Skip to content

Commit

Permalink
fix: add / remove attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoCaimar committed Oct 20, 2023
1 parent 770bd46 commit c4a0b86
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
67 changes: 67 additions & 0 deletions spec/VectorBasemapLayerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,71 @@ describe('VectorBasemapLayer', function () {
expect(attributionUrls[0]).to.equal('https://static.arcgis.com/attribution/Vector/World_Basemap_v2');
});
});

describe('_setupAttribution', function () {
it('should add attribution for non itemId item', function () {
const key = 'ArcGIS:Streets';
const layer = new L.esri.Vector.VectorBasemapLayer(key, {
token: apikey
});
layer._ready = false;
let attributionValue = '';
const fakeMap = {
attributionControl: {
_container: { className: '', querySelector: () => {} },
addAttribution: function () {
attributionValue = arguments[0];
}
},
getSize: function () {
return { x: 0, y: 0 };
},
on: function () {}
};
layer.onAdd(fakeMap);
layer._setupAttribution();
expect(attributionValue).to.be.equal('<span class="esri-dynamic-attribution"></span>');
});

it('should add attribution for itemId item', function () {
const key = '3e1a00aeae81496587988075fe529f71';
const layer = new L.esri.Vector.VectorBasemapLayer(key, {
token: apikey
});
layer._ready = false;
let attributionValue = '?';
const fakeMap = {
attributionControl: {
_container: { className: '', querySelector: () => {} },
addAttribution: function () {
console.warn('addAttribution', arguments);
attributionValue = arguments[0];
}
},
getSize: function () {
return { x: 0, y: 0 };
},
on: function () {}
};
layer.onAdd(fakeMap);
layer._maplibreGL.getMaplibreMap = function () {
return {
style: {
stylesheet: {
sources: {
one: {
attribution: '@ my attribution',
copyrightText: '@ my copyright text'
}
}
}
}
};
};

layer._setupAttribution();
const expectedAttributionValue = '<span class="esri-dynamic-attribution">@ my attribution, @ my copyright text</span>';
expect(attributionValue).to.be.equal(expectedAttributionValue);
});
});
});
7 changes: 5 additions & 2 deletions src/VectorBasemapLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export var VectorBasemapLayer = VectorTileLayer.extend({
}
});

this._map.attributionControl.addAttribution('<span class="">' + allAttributions.join(', ') + '</span>');
this._map.attributionControl.addAttribution(`<span class="esri-dynamic-attribution">${allAttributions.join(', ')}</span>`);
} else {
// this is an enum
if (!this.options.attributionUrls) {
Expand Down Expand Up @@ -142,8 +142,11 @@ export var VectorBasemapLayer = VectorTileLayer.extend({

if (element && element.length > 0) {
const vectorAttribution = element[0].outerHTML;
// this doesn't work, not sure why.
// call removeAttribution twice here
// this is needed due to the 2 different ways that addAttribution is called inside _setupAttribution.
// leaflet attributionControl.removeAttribution method ignore a call when the attribution sent is not present there
map.attributionControl.removeAttribution(vectorAttribution);
map.attributionControl.removeAttribution('<span class="esri-dynamic-attribution"></span>');
}
}
},
Expand Down

0 comments on commit c4a0b86

Please sign in to comment.