Reworked attribution handling

This commit is contained in:
Tim Schaub
2017-10-08 14:40:40 -06:00
parent a5a0f5b98b
commit 2dd8fdb5b7
32 changed files with 346 additions and 391 deletions

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.BingMaps');
goog.require('ol');
goog.require('ol.Attribution');
goog.require('ol.TileUrlFunction');
goog.require('ol.extent');
goog.require('ol.net');
@@ -81,14 +80,12 @@ ol.inherits(ol.source.BingMaps, ol.source.TileImage);
* The attribution containing a link to the Microsoft® Bing™ Maps Platform APIs
* Terms Of Use.
* @const
* @type {ol.Attribution}
* @type {string}
* @api
*/
ol.source.BingMaps.TOS_ATTRIBUTION = new ol.Attribution({
html: '<a class="ol-attribution-bing-tos" ' +
ol.source.BingMaps.TOS_ATTRIBUTION = '<a class="ol-attribution-bing-tos" ' +
'href="https://www.microsoft.com/maps/product/terms.html">' +
'Terms of Use</a>'
});
'Terms of Use</a>';
/**
@@ -181,31 +178,32 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse = function(response)
var transform = ol.proj.getTransformFromProjections(
ol.proj.get('EPSG:4326'), this.getProjection());
var attributions = resource.imageryProviders.map(function(imageryProvider) {
var html = imageryProvider.attribution;
/** @type {Object.<string, Array.<ol.TileRange>>} */
var tileRanges = {};
imageryProvider.coverageAreas.forEach(function(coverageArea) {
var minZ = coverageArea.zoomMin;
var maxZ = Math.min(coverageArea.zoomMax, maxZoom);
var bbox = coverageArea.bbox;
var epsg4326Extent = [bbox[1], bbox[0], bbox[3], bbox[2]];
var extent = ol.extent.applyTransform(epsg4326Extent, transform);
var tileRange, z, zKey;
for (z = minZ; z <= maxZ; ++z) {
zKey = z.toString();
tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z);
if (zKey in tileRanges) {
tileRanges[zKey].push(tileRange);
} else {
tileRanges[zKey] = [tileRange];
this.setAttributions(function(frameState) {
var attributions = [];
var zoom = frameState.viewState.zoom;
resource.imageryProviders.map(function(imageryProvider) {
var intersects = false;
var coverageAreas = imageryProvider.coverageAreas;
for (var i = 0, ii = coverageAreas.length; i < ii; ++i) {
var coverageArea = coverageAreas[i];
if (zoom >= coverageArea.zoomMin && zoom <= coverageArea.zoomMax) {
var bbox = coverageArea.bbox;
var epsg4326Extent = [bbox[1], bbox[0], bbox[3], bbox[2]];
var extent = ol.extent.applyTransform(epsg4326Extent, transform);
if (ol.extent.intersects(extent, frameState.extent)) {
intersects = true;
break;
}
}
}
if (intersects) {
attributions.push(imageryProvider.attribution);
}
});
return new ol.Attribution({html: html, tileRanges: tileRanges});
attributions.push(ol.source.BingMaps.TOS_ATTRIBUTION);
return attributions;
});
attributions.push(ol.source.BingMaps.TOS_ATTRIBUTION);
this.setAttributions(attributions);
}
this.setLogo(brandLogoUri);

View File

@@ -169,7 +169,7 @@ ol.source.ImageArcGISRest.prototype.getImageInternal = function(extent, resoluti
projection, params);
this.image_ = new ol.Image(extent, resolution, pixelRatio,
this.getAttributions(), url, this.crossOrigin_, this.imageLoadFunction_);
url, this.crossOrigin_, this.imageLoadFunction_);
this.renderedRevision_ = this.getRevision();

View File

@@ -78,8 +78,7 @@ ol.source.ImageCanvas.prototype.getImageInternal = function(extent, resolution,
var canvasElement = this.canvasFunction_(
extent, resolution, pixelRatio, size, projection);
if (canvasElement) {
canvas = new ol.ImageCanvas(extent, resolution, pixelRatio,
this.getAttributions(), canvasElement);
canvas = new ol.ImageCanvas(extent, resolution, pixelRatio, canvasElement);
}
this.canvas_ = canvas;
this.renderedRevision_ = this.getRevision();

View File

@@ -141,7 +141,7 @@ ol.source.ImageMapGuide.prototype.getImageInternal = function(extent, resolution
var imageUrl = this.getUrl(this.url_, this.params_, extent, size,
projection);
image = new ol.Image(extent, resolution, pixelRatio,
this.getAttributions(), imageUrl, this.crossOrigin_,
imageUrl, this.crossOrigin_,
this.imageLoadFunction_);
ol.events.listen(image, ol.events.EventType.CHANGE,
this.handleImageChange, this);

View File

@@ -40,8 +40,7 @@ ol.source.ImageStatic = function(options) {
* @private
* @type {ol.Image}
*/
this.image_ = new ol.Image(imageExtent, undefined, 1, this.getAttributions(),
options.url, crossOrigin, imageLoadFunction);
this.image_ = new ol.Image(imageExtent, undefined, 1, options.url, crossOrigin, imageLoadFunction);
/**
* @private

View File

@@ -225,7 +225,7 @@ ol.source.ImageWMS.prototype.getImageInternal = function(extent, resolution, pix
projection, params);
this.image_ = new ol.Image(requestExtent, resolution, pixelRatio,
this.getAttributions(), url, this.crossOrigin_, this.imageLoadFunction_);
url, this.crossOrigin_, this.imageLoadFunction_);
this.renderedRevision_ = this.getRevision();

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.OSM');
goog.require('ol');
goog.require('ol.Attribution');
goog.require('ol.source.XYZ');
@@ -51,11 +50,9 @@ ol.inherits(ol.source.OSM, ol.source.XYZ);
* The attribution containing a link to the OpenStreetMap Copyright and License
* page.
* @const
* @type {ol.Attribution}
* @type {string}
* @api
*/
ol.source.OSM.ATTRIBUTION = new ol.Attribution({
html: '&copy; ' +
ol.source.OSM.ATTRIBUTION = '&copy; ' +
'<a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> ' +
'contributors.'
});
'contributors.';

View File

@@ -292,8 +292,7 @@ ol.source.Raster.prototype.onWorkerComplete_ = function(frameState, err, output,
var width = Math.round(ol.extent.getWidth(extent) / resolution);
var height = Math.round(ol.extent.getHeight(extent) / resolution);
context = ol.dom.createCanvasContext2D(width, height);
this.renderedImageCanvas_ = new ol.ImageCanvas(
extent, resolution, 1, this.getAttributions(), context.canvas);
this.renderedImageCanvas_ = new ol.ImageCanvas(extent, resolution, 1, context.canvas);
}
context.putImageData(output, 0, 0);

View File

@@ -35,7 +35,13 @@ ol.source.Source = function(options) {
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = ol.source.Source.toAttributionsArray_(options.attributions);
this.attributions_ = null;
/**
* @private
* @type {?ol.Attribution2}
*/
this.attributions2_ = this.adaptAttributions_(options.attributions);
/**
* @private
@@ -60,36 +66,60 @@ ol.source.Source = function(options) {
ol.inherits(ol.source.Source, ol.Object);
/**
* Turns various ways of defining an attribution to an array of `ol.Attributions`.
*
* @param {ol.AttributionLike|undefined}
* attributionLike The attributions as string, array of strings,
* `ol.Attribution`, array of `ol.Attribution` or undefined.
* @return {Array.<ol.Attribution>} The array of `ol.Attribution` or null if
* `undefined` was given.
* Turns the attributions option into an attributions function.
* @param {ol.AttributionLike|undefined} attributionLike The attribution option.
* @return {?ol.Attribution2} An attribution function (or null).
*/
ol.source.Source.toAttributionsArray_ = function(attributionLike) {
if (typeof attributionLike === 'string') {
return [new ol.Attribution({html: attributionLike})];
} else if (attributionLike instanceof ol.Attribution) {
return [attributionLike];
} else if (Array.isArray(attributionLike)) {
var len = attributionLike.length;
var attributions = new Array(len);
for (var i = 0; i < len; i++) {
var item = attributionLike[i];
if (typeof item === 'string') {
attributions[i] = new ol.Attribution({html: item});
} else {
attributions[i] = item;
}
}
return attributions;
} else {
ol.source.Source.prototype.adaptAttributions_ = function(attributionLike) {
if (!attributionLike) {
return null;
}
};
if (attributionLike instanceof ol.Attribution) {
// TODO: remove attributions_ in next major release
this.attributions_ = [attributionLike];
return function(frameState) {
return [attributionLike.getHTML()];
};
}
if (Array.isArray(attributionLike)) {
if (attributionLike[0] instanceof ol.Attribution) {
// TODO: remove attributions_ in next major release
this.attributions_ = attributionLike;
var attributions = attributionLike.map(function(attribution) {
return attribution.getHTML();
});
return function(frameState) {
return attributions;
};
}
// TODO: remove attributions_ in next major release
this.attributions_ = attributionLike.map(function(attribution) {
return new ol.Attribution({html: attribution});
});
return function(frameState) {
return attributionLike;
};
}
if (typeof attributionLike === 'function') {
return attributionLike;
}
// TODO: remove attributions_ in next major release
this.attributions_ = [
new ol.Attribution({html: attributionLike})
];
return function(frameState) {
return [attributionLike];
};
};
/**
* @param {ol.Coordinate} coordinate Coordinate.
@@ -115,6 +145,15 @@ ol.source.Source.prototype.getAttributions = function() {
};
/**
* Get the attribution function for the source.
* @return {?ol.Attribution2} Attribution function.
*/
ol.source.Source.prototype.getAttributions2 = function() {
return this.attributions2_;
};
/**
* Get the logo of the source.
* @return {string|olx.LogoOptions|undefined} Logo.
@@ -172,12 +211,12 @@ ol.source.Source.prototype.refresh = function() {
/**
* Set the attributions of the source.
* @param {ol.AttributionLike|undefined} attributions Attributions.
* Can be passed as `string`, `Array<string>`, `{@link ol.Attribution}`,
* `Array<{@link ol.Attribution}>` or `undefined`.
* Can be passed as `string`, `Array<string>`, `{@link ol.Attribution2}`,
* or `undefined`.
* @api
*/
ol.source.Source.prototype.setAttributions = function(attributions) {
this.attributions_ = ol.source.Source.toAttributionsArray_(attributions);
this.attributions2_ = this.adaptAttributions_(attributions);
this.changed();
};

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.Stamen');
goog.require('ol');
goog.require('ol.Attribution');
goog.require('ol.source.OSM');
goog.require('ol.source.XYZ');
@@ -44,14 +43,12 @@ ol.inherits(ol.source.Stamen, ol.source.XYZ);
/**
* @const
* @type {Array.<ol.Attribution>}
* @type {Array.<string>}
*/
ol.source.Stamen.ATTRIBUTIONS = [
new ol.Attribution({
html: 'Map tiles by <a href="https://stamen.com/">Stamen Design</a>, ' +
'Map tiles by <a href="https://stamen.com/">Stamen Design</a>, ' +
'under <a href="https://creativecommons.org/licenses/by/3.0/">CC BY' +
' 3.0</a>.'
}),
' 3.0</a>.',
ol.source.OSM.ATTRIBUTION
];

View File

@@ -7,7 +7,6 @@
goog.provide('ol.source.TileJSON');
goog.require('ol');
goog.require('ol.Attribution');
goog.require('ol.TileUrlFunction');
goog.require('ol.asserts');
goog.require('ol.extent');
@@ -136,23 +135,17 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function(tileJSON) {
this.tileUrlFunction =
ol.TileUrlFunction.createFromTemplates(tileJSON.tiles, tileGrid);
if (tileJSON.attribution !== undefined && !this.getAttributions()) {
if (tileJSON.attribution !== undefined && !this.getAttributions2()) {
var attributionExtent = extent !== undefined ?
extent : epsg4326Projection.getExtent();
/** @type {Object.<string, Array.<ol.TileRange>>} */
var tileRanges = {};
var z, zKey;
for (z = minZoom; z <= maxZoom; ++z) {
zKey = z.toString();
tileRanges[zKey] =
[tileGrid.getTileRangeForExtentAndZ(attributionExtent, z)];
}
this.setAttributions([
new ol.Attribution({
html: tileJSON.attribution,
tileRanges: tileRanges
})
]);
this.setAttributions(function(frameState) {
if (ol.extent.intersects(attributionExtent, frameState.extent)) {
return [tileJSON.attribution];
}
return null;
});
}
this.tileJSON_ = tileJSON;
this.setState(ol.source.State.READY);

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.TileUTFGrid');
goog.require('ol');
goog.require('ol.Attribution');
goog.require('ol.Tile');
goog.require('ol.TileState');
goog.require('ol.TileUrlFunction');
@@ -198,20 +197,13 @@ ol.source.TileUTFGrid.prototype.handleTileJSONResponse = function(tileJSON) {
if (tileJSON.attribution !== undefined) {
var attributionExtent = extent !== undefined ?
extent : epsg4326Projection.getExtent();
/** @type {Object.<string, Array.<ol.TileRange>>} */
var tileRanges = {};
var z, zKey;
for (z = minZoom; z <= maxZoom; ++z) {
zKey = z.toString();
tileRanges[zKey] =
[tileGrid.getTileRangeForExtentAndZ(attributionExtent, z)];
}
this.setAttributions([
new ol.Attribution({
html: tileJSON.attribution,
tileRanges: tileRanges
})
]);
this.setAttributions(function(frameState) {
if (ol.extent.intersects(attributionExtent, frameState.extent)) {
return [tileJSON.attribution];
}
return null;
});
}
this.setState(ol.source.State.READY);