From bedda42c3e281f0279f27f6dc8d0d8a05fdca145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Ber=C3=A9nyi?= Date: Fri, 29 Apr 2016 15:37:28 +0200 Subject: [PATCH 1/3] Ignore duplicate attributions. See https://github.com/openlayers/ol3/issues/5297 --- src/ol/control/attribution.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ol/control/attribution.js b/src/ol/control/attribution.js index 0963d4060f..2fce9c8735 100644 --- a/src/ol/control/attribution.js +++ b/src/ol/control/attribution.js @@ -182,11 +182,21 @@ ol.control.Attribution.prototype.getSourceAttributions = function(frameState) { } else { intersectsTileRange = false; } + var attributionAlreadyAdded = false; if (intersectsTileRange) { if (sourceAttributionKey in hiddenAttributions) { delete hiddenAttributions[sourceAttributionKey]; } - attributions[sourceAttributionKey] = sourceAttribution; + for (var existingKey in attributions) { + if (attributions.hasOwnProperty(existingKey)) { + if (attributions[existingKey].getHTML() === sourceAttribution.getHTML()) { + attributionAlreadyAdded = true; + } + } + } + if (!attributionAlreadyAdded) { + attributions[sourceAttributionKey] = sourceAttribution; + } } else { hiddenAttributions[sourceAttributionKey] = sourceAttribution; } From adacfb64538bc547f96e2555a5a765cd0a7788ea Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Fri, 9 Dec 2016 22:17:02 +0100 Subject: [PATCH 2/3] Add test for duplicated attribution --- test/spec/ol/control/attribution.test.js | 76 ++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/spec/ol/control/attribution.test.js diff --git a/test/spec/ol/control/attribution.test.js b/test/spec/ol/control/attribution.test.js new file mode 100644 index 0000000000..ca2d8d6403 --- /dev/null +++ b/test/spec/ol/control/attribution.test.js @@ -0,0 +1,76 @@ +goog.provide('ol.test.control.Attribution'); + +goog.require('ol.Map'); +goog.require('ol.View'); +goog.require('ol.control.Attribution'); +goog.require('ol.layer.Tile'); +goog.require('ol.source.Tile'); + +describe('ol.control.Attribution', function() { + + var map, target; + beforeEach(function() { + target = document.createElement('div'); + target.style.width = target.style.height = '100px'; + document.body.appendChild(target); + map = new ol.Map({ + target: target, + controls: [new ol.control.Attribution({ + collapsed: false, + collapsible: false + })], + layers: [ + new ol.layer.Tile({ + source: new ol.source.Tile({ + projection: 'EPSG:3857', + tileGrid: ol.tilegrid.createXYZ(), + attributions: 'foo' + }) + }), + new ol.layer.Tile({ + source: new ol.source.Tile({ + projection: 'EPSG:3857', + tileGrid: ol.tilegrid.createXYZ(), + attributions: 'bar' + }) + }), + new ol.layer.Tile({ + source: new ol.source.Tile({ + projection: 'EPSG:3857', + tileGrid: ol.tilegrid.createXYZ(), + attributions: 'foo' + }) + }) + ], + view: new ol.View({ + center: [0, 0], + zoom: 0 + }) + }); + map.getLayers().forEach(function(layer) { + var source = layer.getSource(); + source.getTile = function() { + var tile = new ol.Tile([0, 0, -1], 2 /* LOADED */); + tile.getImage = function() { + var image = new Image(); + image.width = 256; + image.height = 256; + return image; + }; + return tile; + }; + }); + }); + + afterEach(function() { + map.setTarget(null); + document.body.removeChild(target); + }); + + it('does not add duplicate attributions', function() { + map.renderSync(); + var attribution = document.querySelectorAll('.ol-attribution li'); + expect(attribution.length).to.be(3); // first
  • is the logo + }); + +}); From bb62360213eb19020c7aeb824bac255e58836111 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sat, 10 Dec 2016 11:36:27 -0700 Subject: [PATCH 3/3] Create a lookup of unique attributions --- src/ol/control/attribution.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/ol/control/attribution.js b/src/ol/control/attribution.js index 2fce9c8735..c6126cbee8 100644 --- a/src/ol/control/attribution.js +++ b/src/ol/control/attribution.js @@ -157,6 +157,7 @@ ol.control.Attribution.prototype.getSourceAttributions = function(frameState) { var attributions = ol.obj.assign({}, frameState.attributions); /** @type {Object.} */ var hiddenAttributions = {}; + var uniqueAttributions = {}; var projection = /** @type {!ol.proj.Projection} */ (frameState.viewState.projection); for (i = 0, ii = layerStatesArray.length; i < ii; i++) { source = layerStatesArray[i].layer.getSource(); @@ -182,19 +183,13 @@ ol.control.Attribution.prototype.getSourceAttributions = function(frameState) { } else { intersectsTileRange = false; } - var attributionAlreadyAdded = false; if (intersectsTileRange) { if (sourceAttributionKey in hiddenAttributions) { delete hiddenAttributions[sourceAttributionKey]; } - for (var existingKey in attributions) { - if (attributions.hasOwnProperty(existingKey)) { - if (attributions[existingKey].getHTML() === sourceAttribution.getHTML()) { - attributionAlreadyAdded = true; - } - } - } - if (!attributionAlreadyAdded) { + var html = sourceAttribution.getHTML(); + if (!(html in uniqueAttributions)) { + uniqueAttributions[html] = true; attributions[sourceAttributionKey] = sourceAttribution; } } else {