Accept simpler attributions in sources

This commit is contained in:
Marc Jansen
2016-03-10 11:13:54 +01:00
parent 74006a91fe
commit df4f1c1389
7 changed files with 217 additions and 47 deletions

View File

@@ -21,7 +21,7 @@ ol.source.State = {
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* @typedef {{attributions: (olx.source.AttributionOption|undefined),
* logo: (string|olx.LogoOptions|undefined),
* projection: ol.proj.ProjectionLike,
* state: (ol.source.State|undefined),
@@ -57,8 +57,7 @@ ol.source.Source = function(options) {
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = options.attributions !== undefined ?
options.attributions : null;
this.attributions_ = ol.source.Source.toAttributionsArray_(options.attributions);
/**
* @private
@@ -82,6 +81,37 @@ ol.source.Source = function(options) {
};
goog.inherits(ol.source.Source, ol.Object);
/**
* Turns various ways of defining an attribution to an array of `ol.Attributions`.
*
* @param {olx.source.AttributionOption|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.
*/
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 {
return null;
}
}
/**
* @param {ol.Coordinate} coordinate Coordinate.
@@ -161,11 +191,13 @@ ol.source.Source.prototype.refresh = function() {
/**
* Set the attributions of the source.
* @param {Array.<ol.Attribution>} attributions Attributions.
* @param {olx.source.AttributionOption|undefined} attributions Attributions.
* Can be passed as `string`, `Array<string>`, `{@link ol.Attribution}`,
* `Array<{@link ol.Attribution}>` or `undefined`.
* @api
*/
ol.source.Source.prototype.setAttributions = function(attributions) {
this.attributions_ = attributions;
this.attributions_ = ol.source.Source.toAttributionsArray_(attributions);
this.changed();
};