Move adaptAttributions out of the class, set default Options values

This commit is contained in:
Frederic Junod
2018-10-08 16:13:27 +02:00
parent 02b2be5b40
commit 94da8dd0e2

View File

@@ -32,8 +32,8 @@ import SourceState from './State.js';
* @typedef {Object} Options * @typedef {Object} Options
* @property {AttributionLike} [attributions] * @property {AttributionLike} [attributions]
* @property {import("../proj.js").ProjectionLike} projection * @property {import("../proj.js").ProjectionLike} projection
* @property {SourceState} [state] * @property {SourceState} [state='ready']
* @property {boolean} [wrapX] * @property {boolean} [wrapX=false]
*/ */
@@ -64,7 +64,7 @@ class Source extends BaseObject {
* @private * @private
* @type {?Attribution} * @type {?Attribution}
*/ */
this.attributions_ = this.adaptAttributions_(options.attributions); this.attributions_ = adaptAttributions(options.attributions);
/** /**
* This source is currently loading data. Sources that defer loading to the * This source is currently loading data. Sources that defer loading to the
@@ -88,30 +88,6 @@ class Source extends BaseObject {
} }
/**
* Turns the attributions option into an attributions function.
* @param {AttributionLike|undefined} attributionLike The attribution option.
* @return {?Attribution} An attribution function (or null).
*/
adaptAttributions_(attributionLike) {
if (!attributionLike) {
return null;
}
if (Array.isArray(attributionLike)) {
return function(frameState) {
return attributionLike;
};
}
if (typeof attributionLike === 'function') {
return attributionLike;
}
return function(frameState) {
return [attributionLike];
};
}
/** /**
* Get the attribution function for the source. * Get the attribution function for the source.
* @return {?Attribution} Attribution function. * @return {?Attribution} Attribution function.
@@ -167,7 +143,7 @@ class Source extends BaseObject {
* @api * @api
*/ */
setAttributions(attributions) { setAttributions(attributions) {
this.attributions_ = this.adaptAttributions_(attributions); this.attributions_ = adaptAttributions(attributions);
this.changed(); this.changed();
} }
@@ -195,4 +171,29 @@ class Source extends BaseObject {
Source.prototype.forEachFeatureAtCoordinate = VOID; Source.prototype.forEachFeatureAtCoordinate = VOID;
/**
* Turns the attributions option into an attributions function.
* @param {AttributionLike|undefined} attributionLike The attribution option.
* @return {?Attribution} An attribution function (or null).
*/
function adaptAttributions(attributionLike) {
if (!attributionLike) {
return null;
}
if (Array.isArray(attributionLike)) {
return function(frameState) {
return attributionLike;
};
}
if (typeof attributionLike === 'function') {
return attributionLike;
}
return function(frameState) {
return [attributionLike];
};
}
export default Source; export default Source;