Attribution control reads attribs from frame state
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
// FIXME handle date line wrap
|
// FIXME handle date line wrap
|
||||||
// FIXME does not handle image sources
|
|
||||||
|
|
||||||
goog.provide('ol.control.Attribution');
|
goog.provide('ol.control.Attribution');
|
||||||
|
|
||||||
@@ -10,11 +9,12 @@ goog.require('goog.events');
|
|||||||
goog.require('goog.object');
|
goog.require('goog.object');
|
||||||
goog.require('goog.style');
|
goog.require('goog.style');
|
||||||
goog.require('ol.Attribution');
|
goog.require('ol.Attribution');
|
||||||
|
goog.require('ol.FrameState');
|
||||||
goog.require('ol.MapEvent');
|
goog.require('ol.MapEvent');
|
||||||
goog.require('ol.MapEventType');
|
goog.require('ol.MapEventType');
|
||||||
goog.require('ol.TileRange');
|
goog.require('ol.TileRange');
|
||||||
goog.require('ol.control.Control');
|
goog.require('ol.control.Control');
|
||||||
goog.require('ol.source.TileSource');
|
goog.require('ol.source.Source');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -66,16 +66,46 @@ ol.control.Attribution = function(attributionOptions) {
|
|||||||
goog.inherits(ol.control.Attribution, ol.control.Control);
|
goog.inherits(ol.control.Attribution, ol.control.Control);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {?Object.<string, Object.<string, ol.TileRange>>} usedTiles Used
|
||||||
|
* tiles.
|
||||||
|
* @param {Object.<string, ol.source.Source>} sources Sources.
|
||||||
|
* @return {Object.<string, ol.Attribution>} Attributions.
|
||||||
|
*/
|
||||||
|
ol.control.Attribution.prototype.getTileSourceAttributions =
|
||||||
|
function(usedTiles, sources) {
|
||||||
|
/** @type {Object.<string, ol.Attribution>} */
|
||||||
|
var attributions = {};
|
||||||
|
var i, tileRanges, tileSource, tileSourceAttribution,
|
||||||
|
tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z;
|
||||||
|
for (tileSourceKey in usedTiles) {
|
||||||
|
goog.asserts.assert(tileSourceKey in sources);
|
||||||
|
tileSource = sources[tileSourceKey];
|
||||||
|
tileSourceAttributions = tileSource.getAttributions();
|
||||||
|
if (goog.isNull(tileSourceAttributions)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
tileRanges = usedTiles[tileSourceKey];
|
||||||
|
for (i = 0; i < tileSourceAttributions.length; ++i) {
|
||||||
|
tileSourceAttribution = tileSourceAttributions[i];
|
||||||
|
tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString();
|
||||||
|
if (tileSourceAttributionKey in attributions) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (tileSourceAttribution.intersectsAnyTileRange(tileRanges)) {
|
||||||
|
attributions[tileSourceAttributionKey] = tileSourceAttribution;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return attributions;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.MapEvent} mapEvent Map event.
|
* @param {ol.MapEvent} mapEvent Map event.
|
||||||
*/
|
*/
|
||||||
ol.control.Attribution.prototype.handleMapPostrender = function(mapEvent) {
|
ol.control.Attribution.prototype.handleMapPostrender = function(mapEvent) {
|
||||||
var frameState = mapEvent.frameState;
|
this.updateElement_(mapEvent.frameState);
|
||||||
if (goog.isNull(frameState)) {
|
|
||||||
this.updateElement_(null);
|
|
||||||
} else {
|
|
||||||
this.updateElement_(frameState.usedTiles);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -99,12 +129,11 @@ ol.control.Attribution.prototype.setMap = function(map) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @param {?Object.<string, Object.<string, ol.TileRange>>} usedTiles Used
|
* @param {?ol.FrameState} frameState Frame state.
|
||||||
* tiles.
|
|
||||||
*/
|
*/
|
||||||
ol.control.Attribution.prototype.updateElement_ = function(usedTiles) {
|
ol.control.Attribution.prototype.updateElement_ = function(frameState) {
|
||||||
|
|
||||||
if (goog.isNull(usedTiles)) {
|
if (goog.isNull(frameState)) {
|
||||||
if (this.renderedVisible_) {
|
if (this.renderedVisible_) {
|
||||||
goog.style.showElement(this.element, false);
|
goog.style.showElement(this.element, false);
|
||||||
this.renderedVisible_ = false;
|
this.renderedVisible_ = false;
|
||||||
@@ -116,15 +145,13 @@ ol.control.Attribution.prototype.updateElement_ = function(usedTiles) {
|
|||||||
|
|
||||||
/** @type {Object.<string, boolean>} */
|
/** @type {Object.<string, boolean>} */
|
||||||
var attributionsToRemove = {};
|
var attributionsToRemove = {};
|
||||||
/** @type {Object.<string, ol.source.TileSource>} */
|
/** @type {Object.<string, ol.source.Source>} */
|
||||||
var tileSources = {};
|
var sources = {};
|
||||||
var layers = map.getLayers();
|
var layers = map.getLayers();
|
||||||
if (goog.isDef(layers)) {
|
if (goog.isDef(layers)) {
|
||||||
layers.forEach(function(layer) {
|
layers.forEach(function(layer) {
|
||||||
var source = layer.getSource();
|
var source = layer.getSource();
|
||||||
if (source instanceof ol.source.TileSource) {
|
sources[goog.getUid(source).toString()] = source;
|
||||||
tileSources[goog.getUid(source).toString()] = source;
|
|
||||||
}
|
|
||||||
var attributions = source.getAttributions();
|
var attributions = source.getAttributions();
|
||||||
if (!goog.isNull(attributions)) {
|
if (!goog.isNull(attributions)) {
|
||||||
var attribution, i;
|
var attribution, i;
|
||||||
@@ -138,34 +165,16 @@ ol.control.Attribution.prototype.updateElement_ = function(usedTiles) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @type {Object.<string, ol.Attribution>} */
|
/** @type {Object.<string, ol.Attribution>} */
|
||||||
var attributions = {};
|
var attributions = goog.object.clone(frameState.attributions);
|
||||||
var i, tileRanges, tileSource, tileSourceAttribution,
|
var tileSourceAttributions = this.getTileSourceAttributions(
|
||||||
tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z;
|
frameState.usedTiles, sources);
|
||||||
for (tileSourceKey in usedTiles) {
|
goog.object.extend(attributions, tileSourceAttributions);
|
||||||
goog.asserts.assert(tileSourceKey in tileSources);
|
|
||||||
tileSource = tileSources[tileSourceKey];
|
|
||||||
tileSourceAttributions = tileSource.getAttributions();
|
|
||||||
if (goog.isNull(tileSourceAttributions)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
tileRanges = usedTiles[tileSourceKey];
|
|
||||||
for (i = 0; i < tileSourceAttributions.length; ++i) {
|
|
||||||
tileSourceAttribution = tileSourceAttributions[i];
|
|
||||||
tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString();
|
|
||||||
if (tileSourceAttributionKey in attributions) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (tileSourceAttribution.intersectsAnyTileRange(tileRanges)) {
|
|
||||||
attributions[tileSourceAttributionKey] = tileSourceAttribution;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {Array.<number>} */
|
/** @type {Array.<number>} */
|
||||||
var attributionKeys =
|
var attributionKeys =
|
||||||
goog.array.map(goog.object.getKeys(attributions), Number);
|
goog.array.map(goog.object.getKeys(attributions), Number);
|
||||||
goog.array.sort(attributionKeys);
|
goog.array.sort(attributionKeys);
|
||||||
var attributionElement, attributionKey;
|
var i, attributionElement, attributionKey;
|
||||||
for (i = 0; i < attributionKeys.length; ++i) {
|
for (i = 0; i < attributionKeys.length; ++i) {
|
||||||
attributionKey = attributionKeys[i].toString();
|
attributionKey = attributionKeys[i].toString();
|
||||||
if (attributionKey in this.attributionElements_) {
|
if (attributionKey in this.attributionElements_) {
|
||||||
|
|||||||
Reference in New Issue
Block a user