Clean up attribution control
Do not use map.getLayers() anymore, but get layers from frameState.layersArray.
This commit is contained in:
@@ -3,17 +3,14 @@
|
|||||||
goog.provide('ol.control.Attribution');
|
goog.provide('ol.control.Attribution');
|
||||||
|
|
||||||
goog.require('goog.array');
|
goog.require('goog.array');
|
||||||
goog.require('goog.asserts');
|
|
||||||
goog.require('goog.dom');
|
goog.require('goog.dom');
|
||||||
goog.require('goog.dom.TagName');
|
goog.require('goog.dom.TagName');
|
||||||
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.FrameState');
|
||||||
goog.require('ol.TileRange');
|
|
||||||
goog.require('ol.control.Control');
|
goog.require('ol.control.Control');
|
||||||
goog.require('ol.css');
|
goog.require('ol.css');
|
||||||
goog.require('ol.source.Source');
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -67,33 +64,33 @@ goog.inherits(ol.control.Attribution, ol.control.Control);
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {?Object.<string, Object.<string, ol.TileRange>>} usedTiles Used
|
* @param {?ol.FrameState} frameState Frame state.
|
||||||
* tiles.
|
|
||||||
* @param {Object.<string, ol.source.Source>} sources Sources.
|
|
||||||
* @return {Object.<string, ol.Attribution>} Attributions.
|
* @return {Object.<string, ol.Attribution>} Attributions.
|
||||||
*/
|
*/
|
||||||
ol.control.Attribution.prototype.getTileSourceAttributions =
|
ol.control.Attribution.prototype.getTileSourceAttributions =
|
||||||
function(usedTiles, sources) {
|
function(frameState) {
|
||||||
/** @type {Object.<string, ol.Attribution>} */
|
/** @type {Object.<string, ol.Attribution>} */
|
||||||
var attributions = {};
|
var attributions = {};
|
||||||
var i, ii, tileRanges, tileSource, tileSourceAttribution,
|
var i, ii, j, jj, tileRanges, tileSource, tileSourceAttribution,
|
||||||
tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z;
|
tileSourceAttributionKey, tileSourceAttributions, tileSourceKey;
|
||||||
for (tileSourceKey in usedTiles) {
|
var layers = frameState.layersArray;
|
||||||
goog.asserts.assert(tileSourceKey in sources);
|
for (i = 0, ii = layers.length; i < ii; i++) {
|
||||||
tileSource = sources[tileSourceKey];
|
tileSource = layers[i].getSource();
|
||||||
tileSourceAttributions = tileSource.getAttributions();
|
tileSourceKey = goog.getUid(tileSource).toString();
|
||||||
if (goog.isNull(tileSourceAttributions)) {
|
if (tileSourceKey in frameState.usedTiles) {
|
||||||
continue;
|
tileSourceAttributions = tileSource.getAttributions();
|
||||||
}
|
if (goog.isNull(tileSourceAttributions)) {
|
||||||
tileRanges = usedTiles[tileSourceKey];
|
|
||||||
for (i = 0, ii = tileSourceAttributions.length; i < ii; ++i) {
|
|
||||||
tileSourceAttribution = tileSourceAttributions[i];
|
|
||||||
tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString();
|
|
||||||
if (tileSourceAttributionKey in attributions) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (tileSourceAttribution.intersectsAnyTileRange(tileRanges)) {
|
tileRanges = frameState.usedTiles[tileSourceKey];
|
||||||
attributions[tileSourceAttributionKey] = tileSourceAttribution;
|
for (j = 0, jj = tileSourceAttributions.length; j < jj; j++) {
|
||||||
|
tileSourceAttribution = tileSourceAttributions[j];
|
||||||
|
tileSourceAttributionKey =
|
||||||
|
goog.getUid(tileSourceAttribution).toString();
|
||||||
|
if (!(tileSourceAttributionKey in attributions) &&
|
||||||
|
tileSourceAttribution.intersectsAnyTileRange(tileRanges)) {
|
||||||
|
attributions[tileSourceAttributionKey] = tileSourceAttribution;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -123,64 +120,34 @@ ol.control.Attribution.prototype.updateElement_ = function(frameState) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var map = this.getMap();
|
|
||||||
|
|
||||||
/** @type {Object.<string, boolean>} */
|
|
||||||
var attributionsToRemove = {};
|
|
||||||
/** @type {Object.<string, ol.source.Source>} */
|
|
||||||
var sources = {};
|
|
||||||
var layers = map.getLayers();
|
|
||||||
if (goog.isDef(layers)) {
|
|
||||||
layers.forEach(function(layer) {
|
|
||||||
var source = layer.getSource();
|
|
||||||
sources[goog.getUid(source).toString()] = source;
|
|
||||||
var attributions = source.getAttributions();
|
|
||||||
if (!goog.isNull(attributions)) {
|
|
||||||
var attribution, i, ii;
|
|
||||||
for (i = 0, ii = attributions.length; i < ii; ++i) {
|
|
||||||
attribution = attributions[i];
|
|
||||||
attributionKey = goog.getUid(attribution).toString();
|
|
||||||
attributionsToRemove[attributionKey] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @type {Object.<string, ol.Attribution>} */
|
/** @type {Object.<string, ol.Attribution>} */
|
||||||
var attributions = goog.object.clone(frameState.attributions);
|
var attributions = goog.object.clone(frameState.attributions);
|
||||||
var tileSourceAttributions = this.getTileSourceAttributions(
|
goog.object.extend(attributions, this.getTileSourceAttributions(frameState));
|
||||||
frameState.usedTiles, sources);
|
|
||||||
goog.object.extend(attributions, tileSourceAttributions);
|
|
||||||
|
|
||||||
/** @type {Array.<number>} */
|
var attributionElement, attributionKey;
|
||||||
var attributionKeys =
|
for (attributionKey in this.attributionElements_) {
|
||||||
goog.array.map(goog.object.getKeys(attributions), Number);
|
if (attributionKey in attributions) {
|
||||||
goog.array.sort(attributionKeys);
|
|
||||||
var i, ii, attributionElement, attributionKey;
|
|
||||||
for (i = 0, ii = attributionKeys.length; i < ii; ++i) {
|
|
||||||
attributionKey = attributionKeys[i].toString();
|
|
||||||
if (attributionKey in this.attributionElements_) {
|
|
||||||
if (!this.attributionElementRenderedVisible_[attributionKey]) {
|
if (!this.attributionElementRenderedVisible_[attributionKey]) {
|
||||||
goog.style.showElement(this.attributionElements_[attributionKey], true);
|
goog.style.showElement(this.attributionElements_[attributionKey], true);
|
||||||
this.attributionElementRenderedVisible_[attributionKey] = true;
|
this.attributionElementRenderedVisible_[attributionKey] = true;
|
||||||
}
|
}
|
||||||
} else {
|
delete attributions[attributionKey];
|
||||||
attributionElement = goog.dom.createElement(goog.dom.TagName.LI);
|
|
||||||
attributionElement.innerHTML = attributions[attributionKey].getHTML();
|
|
||||||
goog.dom.appendChild(this.ulElement_, attributionElement);
|
|
||||||
this.attributionElements_[attributionKey] = attributionElement;
|
|
||||||
this.attributionElementRenderedVisible_[attributionKey] = true;
|
|
||||||
}
|
}
|
||||||
delete attributionsToRemove[attributionKey];
|
else {
|
||||||
|
goog.dom.removeNode(this.attributionElements_[attributionKey]);
|
||||||
|
delete this.attributionElements_[attributionKey];
|
||||||
|
delete this.attributionElementRenderedVisible_[attributionKey];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (attributionKey in attributions) {
|
||||||
|
attributionElement = goog.dom.createElement(goog.dom.TagName.LI);
|
||||||
|
attributionElement.innerHTML = attributions[attributionKey].getHTML();
|
||||||
|
goog.dom.appendChild(this.ulElement_, attributionElement);
|
||||||
|
this.attributionElements_[attributionKey] = attributionElement;
|
||||||
|
this.attributionElementRenderedVisible_[attributionKey] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (attributionKey in attributionsToRemove) {
|
var renderVisible = !goog.object.isEmpty(this.attributionElements_);
|
||||||
goog.dom.removeNode(this.attributionElements_[attributionKey]);
|
|
||||||
delete this.attributionElements_[attributionKey];
|
|
||||||
delete this.attributionElementRenderedVisible_[attributionKey];
|
|
||||||
}
|
|
||||||
|
|
||||||
var renderVisible = !goog.array.isEmpty(attributionKeys);
|
|
||||||
if (this.renderedVisible_ != renderVisible) {
|
if (this.renderedVisible_ != renderVisible) {
|
||||||
goog.style.showElement(this.element, renderVisible);
|
goog.style.showElement(this.element, renderVisible);
|
||||||
this.renderedVisible_ = renderVisible;
|
this.renderedVisible_ = renderVisible;
|
||||||
|
|||||||
Reference in New Issue
Block a user