diff --git a/externs/olx.js b/externs/olx.js index 4f634dbb1c..dbbe89847a 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -3903,6 +3903,13 @@ olx.render.ToContextOptions.prototype.pixelRatio; olx.source; +/** + * @typedef {string|Array.|ol.Attribution|Array.} + * @api + */ +olx.source.AttributionOption; + + /** * @typedef {{cacheSize: (number|undefined), * culture: (string|undefined), @@ -3989,7 +3996,7 @@ olx.source.BingMapsOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * distance: (number|undefined), * extent: (ol.Extent|undefined), * format: (ol.format.Feature|undefined), @@ -4005,7 +4012,7 @@ olx.source.ClusterOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.ClusterOptions.prototype.attributions; @@ -4115,7 +4122,7 @@ olx.source.TileUTFGridOptions.prototype.url; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * logo: (string|olx.LogoOptions|undefined), @@ -4140,7 +4147,7 @@ olx.source.TileImageOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.TileImageOptions.prototype.attributions; @@ -4287,7 +4294,7 @@ olx.source.TileImageOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * format: (ol.format.Feature|undefined), * logo: (string|olx.LogoOptions|undefined), @@ -4311,13 +4318,12 @@ olx.source.VectorTileOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.VectorTileOptions.prototype.attributions; - /** * Cache size. Default is `128`. * @type {number|undefined} @@ -4640,7 +4646,7 @@ olx.source.TileDebugOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * maxZoom: (number|undefined), @@ -4656,7 +4662,7 @@ olx.source.OSMOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.OSMOptions.prototype.attributions; @@ -4740,7 +4746,7 @@ olx.source.OSMOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * canvasFunction: ol.CanvasFunctionType, * logo: (string|olx.LogoOptions|undefined), * projection: ol.proj.ProjectionLike, @@ -4754,7 +4760,7 @@ olx.source.ImageCanvasOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.ImageCanvasOptions.prototype.attributions; @@ -4819,7 +4825,7 @@ olx.source.ImageCanvasOptions.prototype.state; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * logo: (string|olx.LogoOptions|undefined), * projection: ol.proj.ProjectionLike, * ratio: (number|undefined), @@ -4833,7 +4839,7 @@ olx.source.ImageVectorOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.ImageVectorOptions.prototype.attributions; @@ -4951,7 +4957,7 @@ olx.source.RasterOptions.prototype.operationType; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * crossOrigin: (null|string|undefined), * hidpi: (boolean|undefined), * serverType: (ol.source.wms.ServerType|string|undefined), @@ -4969,7 +4975,7 @@ olx.source.ImageWMSOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.ImageWMSOptions.prototype.attributions; @@ -5149,7 +5155,7 @@ olx.source.StamenOptions.prototype.url; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * crossOrigin: (null|string|undefined), * imageExtent: (ol.Extent), * imageLoadFunction: (ol.ImageLoadFunctionType|undefined), @@ -5164,7 +5170,7 @@ olx.source.ImageStaticOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.ImageStaticOptions.prototype.attributions; @@ -5233,7 +5239,7 @@ olx.source.ImageStaticOptions.prototype.url; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * params: (Object.|undefined), @@ -5252,7 +5258,7 @@ olx.source.TileArcGISRestOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.TileArcGISRestOptions.prototype.attributions; @@ -5369,7 +5375,7 @@ olx.source.TileArcGISRestOptions.prototype.urls; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * jsonp: (boolean|undefined), @@ -5386,7 +5392,7 @@ olx.source.TileJSONOptions; * Optional attributions for the source. If provided, these will be used * instead of any attribution data advertised by the server. If not provided, * any attributions advertised by the server will be used. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.TileJSONOptions.prototype.attributions; @@ -5460,7 +5466,7 @@ olx.source.TileJSONOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * params: Object., * crossOrigin: (null|string|undefined), @@ -5483,7 +5489,7 @@ olx.source.TileWMSOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.TileWMSOptions.prototype.attributions; @@ -5639,7 +5645,7 @@ olx.source.TileWMSOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * features: (Array.|ol.Collection.|undefined), * format: (ol.format.Feature|undefined), * loader: (ol.FeatureLoader|undefined), @@ -5655,7 +5661,7 @@ olx.source.VectorOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.VectorOptions.prototype.attributions; @@ -5759,7 +5765,7 @@ olx.source.VectorOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (string|null|undefined), * logo: (string|olx.LogoOptions|undefined), @@ -5789,7 +5795,7 @@ olx.source.WMTSOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api */ olx.source.WMTSOptions.prototype.attributions; @@ -5975,7 +5981,7 @@ olx.source.WMTSOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * logo: (string|olx.LogoOptions|undefined), @@ -5999,7 +6005,7 @@ olx.source.XYZOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.XYZOptions.prototype.attributions; @@ -6150,7 +6156,7 @@ olx.source.XYZOptions.prototype.wrapX; /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * crossOrigin: (null|string|undefined), * logo: (string|olx.LogoOptions|undefined), @@ -6165,7 +6171,7 @@ olx.source.ZoomifyOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.ZoomifyOptions.prototype.attributions; diff --git a/src/ol/source/imagesource.js b/src/ol/source/imagesource.js index f6c4f52977..7f72b1f104 100644 --- a/src/ol/source/imagesource.js +++ b/src/ol/source/imagesource.js @@ -3,7 +3,6 @@ goog.provide('ol.source.ImageEvent'); goog.require('goog.asserts'); goog.require('ol.events.Event'); -goog.require('ol.Attribution'); goog.require('ol.ImageState'); goog.require('ol.array'); goog.require('ol.extent'); @@ -13,7 +12,7 @@ goog.require('ol.source.Source'); /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * extent: (null|ol.Extent|undefined), * logo: (string|olx.LogoOptions|undefined), * projection: ol.proj.ProjectionLike, diff --git a/src/ol/source/imagestaticsource.js b/src/ol/source/imagestaticsource.js index d482f23ef1..2d046d1c17 100644 --- a/src/ol/source/imagestaticsource.js +++ b/src/ol/source/imagestaticsource.js @@ -20,10 +20,6 @@ goog.require('ol.source.Image'); * @api stable */ ol.source.ImageStatic = function(options) { - - var attributions = options.attributions !== undefined ? - options.attributions : null; - var imageExtent = options.imageExtent; var crossOrigin = options.crossOrigin !== undefined ? @@ -34,7 +30,7 @@ ol.source.ImageStatic = function(options) { options.imageLoadFunction : ol.source.Image.defaultImageLoadFunction; goog.base(this, { - attributions: attributions, + attributions: options.attributions, logo: options.logo, projection: ol.proj.get(options.projection) }); @@ -43,7 +39,7 @@ ol.source.ImageStatic = function(options) { * @private * @type {ol.Image} */ - this.image_ = new ol.Image(imageExtent, undefined, 1, attributions, + this.image_ = new ol.Image(imageExtent, undefined, 1, this.getAttributions(), options.url, crossOrigin, imageLoadFunction); /** diff --git a/src/ol/source/source.js b/src/ol/source/source.js index ed9e8a3d6a..792a0fb5b2 100644 --- a/src/ol/source/source.js +++ b/src/ol/source/source.js @@ -21,7 +21,7 @@ ol.source.State = { /** - * @typedef {{attributions: (Array.|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.} */ - 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.} 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.} attributions Attributions. + * @param {olx.source.AttributionOption|undefined} attributions Attributions. + * Can be passed as `string`, `Array`, `{@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(); }; diff --git a/src/ol/source/tilesource.js b/src/ol/source/tilesource.js index 57d7f5099e..8ad0a4213d 100644 --- a/src/ol/source/tilesource.js +++ b/src/ol/source/tilesource.js @@ -5,7 +5,6 @@ goog.provide('ol.source.TileOptions'); goog.require('goog.asserts'); goog.require('ol.events.Event'); goog.require('ol'); -goog.require('ol.Attribution'); goog.require('ol.Extent'); goog.require('ol.TileCache'); goog.require('ol.TileRange'); @@ -18,7 +17,7 @@ goog.require('ol.tilegrid.TileGrid'); /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * extent: (ol.Extent|undefined), * logo: (string|olx.LogoOptions|undefined), diff --git a/src/ol/source/urltilesource.js b/src/ol/source/urltilesource.js index 2a80ef9132..7745dc5078 100644 --- a/src/ol/source/urltilesource.js +++ b/src/ol/source/urltilesource.js @@ -11,7 +11,7 @@ goog.require('ol.source.TileEvent'); /** - * @typedef {{attributions: (Array.|undefined), + * @typedef {{attributions: (olx.source.AttributionOption|undefined), * cacheSize: (number|undefined), * extent: (ol.Extent|undefined), * logo: (string|olx.LogoOptions|undefined), diff --git a/test/spec/ol/source/source.test.js b/test/spec/ol/source/source.test.js index 49b5e523ed..fb1c4a2683 100644 --- a/test/spec/ol/source/source.test.js +++ b/test/spec/ol/source/source.test.js @@ -11,6 +11,74 @@ describe('ol.source.Source', function() { }); }); + describe('config option `attributions`', function() { + it('accepts undefined', function() { + var source = new ol.source.Source({}); + var attributions = source.getAttributions(); + expect(attributions).to.be(null); + }); + it('accepts a single string', function() { + var source = new ol.source.Source({ + attributions: 'Humpty' + }); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(1); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + }); + it('accepts an array of strings', function() { + var source = new ol.source.Source({ + attributions: ['Humpty', 'Dumpty'] + }); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1].getHTML()).to.be('Dumpty'); + }); + it('accepts a single ol.Attribution', function() { + var passedAttribution = new ol.Attribution({html: 'Humpty'}); + var source = new ol.source.Source({ + attributions: passedAttribution + }); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(1); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0]).to.be(passedAttribution); + }); + it('accepts an array of ol.Attribution', function() { + var firstAttribution = new ol.Attribution({html: 'Humpty'}); + var secondAttribution = new ol.Attribution({html: 'Dumpty'}); + var source = new ol.source.Source({ + attributions: [firstAttribution, secondAttribution] + }); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0]).to.be(firstAttribution); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1]).to.be(secondAttribution); + }); + it('accepts an array with a string and an ol.Attribution', function() { + var attribution = new ol.Attribution({html: 'Dumpty'}); + var source = new ol.source.Source({ + attributions: ['Humpty', attribution] + }); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1]).to.be(attribution); + }); + }); + describe('#refresh()', function() { it('dispatches the change event', function() { var source = new ol.source.Source({ @@ -23,7 +91,77 @@ describe('ol.source.Source', function() { }); }); + describe('#setAttributions`', function() { + var source = null; + + beforeEach(function() { + source = new ol.source.Source({ + attributions: 'before' + }); + }); + afterEach(function() { + source = null; + }); + + it('accepts undefined', function() { + source.setAttributions(); + var attributions = source.getAttributions(); + expect(attributions).to.be(null); + }); + it('accepts a single string', function() { + source.setAttributions('Humpty'); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(1); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + }); + it('accepts an array of strings', function() { + source.setAttributions(['Humpty', 'Dumpty']); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1].getHTML()).to.be('Dumpty'); + }); + it('accepts a single ol.Attribution', function() { + var passedAttribution = new ol.Attribution({html: 'Humpty'}); + source.setAttributions(passedAttribution); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(1); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0]).to.be(passedAttribution); + }); + it('accepts an array of ol.Attribution', function() { + var firstAttribution = new ol.Attribution({html: 'Humpty'}); + var secondAttribution = new ol.Attribution({html: 'Dumpty'}); + source.setAttributions([firstAttribution, secondAttribution]); + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0]).to.be(firstAttribution); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1]).to.be(secondAttribution); + }); + it('accepts an array with a string and an ol.Attribution', function() { + var attribution = new ol.Attribution({html: 'Dumpty'}); + source.setAttributions(['Humpty', attribution]) + var attributions = source.getAttributions(); + expect(attributions).to.not.be(null); + expect(attributions).to.have.length(2); + expect(attributions[0]).to.be.an(ol.Attribution); + expect(attributions[0].getHTML()).to.be('Humpty'); + expect(attributions[1]).to.be.an(ol.Attribution); + expect(attributions[1]).to.be(attribution); + }); + }); + }); +goog.require('ol.Attribution'); goog.require('ol.proj'); goog.require('ol.source.Source');