Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update attribution on layer visibility changes #9943

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,12 @@ class Style extends Evented {
this._resetUpdates();
}

const sourcesUsedBefore = {};

for (const sourceId in this.sourceCaches) {
this.sourceCaches[sourceId].used = false;
const sourceCache = this.sourceCaches[sourceId];
sourcesUsedBefore[sourceId] = sourceCache.used;
sourceCache.used = false;
}

for (const layerId of this._order) {
Expand All @@ -425,6 +429,13 @@ class Style extends Evented {
}
}

for (const sourceId in sourcesUsedBefore) {
const sourceCache = this.sourceCaches[sourceId];
if (sourcesUsedBefore[sourceId] !== sourceCache.used) {
sourceCache.fire(new Event('data', {sourceDataType: 'visibility', dataType:'source', sourceId}));
}
}

this.light.recalculate(parameters);
this.z = parameters.zoom;

Expand Down
2 changes: 1 addition & 1 deletion src/ui/control/attribution_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class AttributionControl {
}

_updateData(e: any) {
if (e && (e.sourceDataType === 'metadata' || e.dataType === 'style')) {
if (e && (e.sourceDataType === 'metadata' || e.sourceDataType === 'visibility' || e.dataType === 'style')) {
this._updateAttributions();
this._updateEditLink();
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export type MapBoxZoomEvent = {
* @property {boolean} [isSourceLoaded] True if the event has a `dataType` of `source` and the source has no outstanding network requests.
* @property {Object} [source] The [style spec representation of the source](https://www.mapbox.com/mapbox-gl-style-spec/#sources) if the event has a `dataType` of `source`.
* @property {string} [sourceDataType] Included if the event has a `dataType` of `source` and the event signals
* that internal data has been received or changed. Possible values are `metadata` and `content`.
* that internal data has been received or changed. Possible values are `metadata`, `content` and `visibility`.
* @property {Object} [tile] The tile being loaded or changed, if the event has a `dataType` of `source` and
* the event is related to loading of a tile.
* @property {Coordinate} [coord] The coordinate of the tile if the event has a `dataType` of `source` and
Expand Down
24 changes: 24 additions & 0 deletions test/unit/ui/control/attribution.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,27 @@ test('AttributionControl hides attributions for sources that are not currently v
}
});
});

test('AttributionControl toggles attributions for sources whose visibility changes when zooming', (t) => {
const map = createMap(t);
const attribution = new AttributionControl();
map.addControl(attribution);

map.on('load', () => {
map.addSource('1', {type: 'geojson', data: {type: 'FeatureCollection', features: []}, attribution: 'Used'});
map.addLayer({id: '1', type: 'fill', source: '1', minzoom: 12});
});

map.on('data', (e) => {
if (e.dataType === 'source' && e.sourceDataType === 'metadata') {
t.equal(attribution._innerContainer.innerHTML, '');
map.setZoom(13);
}
if (e.dataType === 'source' && e.sourceDataType === 'visibility') {
if (map.getZoom() === 13) {
t.equal(attribution._innerContainer.innerHTML, 'Used');
t.end();
}
}
});
});