diff --git a/config/example.json b/config/example.json index e5ac3aaf7b..8487b5159d 100644 --- a/config/example.json +++ b/config/example.json @@ -12,6 +12,7 @@ ], "externs": [ "externs/bingmaps.js", + "externs/cartodb.js", "externs/bootstrap.js", "externs/closure-compiler.js", "externs/esrijson.js", diff --git a/config/examples-all.json b/config/examples-all.json index 40d452b4b3..3eee28d504 100644 --- a/config/examples-all.json +++ b/config/examples-all.json @@ -13,6 +13,7 @@ "externs": [ "externs/bingmaps.js", "externs/bootstrap.js", + "externs/cartodb.js", "externs/closure-compiler.js", "externs/esrijson.js", "externs/example.js", diff --git a/config/ol.json b/config/ol.json index 37039fac71..eb542af685 100644 --- a/config/ol.json +++ b/config/ol.json @@ -4,6 +4,7 @@ "compile": { "externs": [ "externs/bingmaps.js", + "externs/cartodb.js", "externs/closure-compiler.js", "externs/esrijson.js", "externs/geojson.js", diff --git a/externs/cartodb.js b/externs/cartodb.js new file mode 100644 index 0000000000..7d3c15e029 --- /dev/null +++ b/externs/cartodb.js @@ -0,0 +1,34 @@ +/** + * @externs + */ + + + +/** + * @constructor + */ +var CartoDBLayerInfo = function() {}; + + +/** + * @type {string} + */ +CartoDBLayerInfo.prototype.layergroupid; + + +/** + * @type {string} + */ +CartoDBLayerInfo.prototype.updated_at; + + +/** + * @type {Object} + */ +CartoDBLayerInfo.prototype.metadata; + + +/** + * @type {{http: string, https: string}} + */ +CartoDBLayerInfo.prototype.cdn_url; diff --git a/externs/olx.js b/externs/olx.js index 8348707365..30907968f4 100644 --- a/externs/olx.js +++ b/externs/olx.js @@ -6210,7 +6210,8 @@ olx.source.XYZOptions.prototype.urls; 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), * projection: ol.proj.ProjectionLike, @@ -6219,7 +6220,7 @@ olx.source.XYZOptions.prototype.wrapX; * wrapX: (boolean|undefined), * config: (Object|undefined), * map: (string|undefined), - * account: (string|undefined)}} + * account: string}} * @api */ olx.source.CartoDBOptions; @@ -6227,12 +6228,20 @@ olx.source.CartoDBOptions; /** * Attributions. - * @type {Array.|undefined} + * @type {olx.source.AttributionOption|undefined} * @api stable */ olx.source.CartoDBOptions.prototype.attributions; +/** + * Cache size. Default is `2048`. + * @type {number|undefined} + * @api + */ +olx.source.CartoDBOptions.prototype.cacheSize; + + /** * The `crossOrigin` attribute for loaded images. Note that you must provide a * `crossOrigin` value if you are using the WebGL renderer or if you want to @@ -6269,6 +6278,14 @@ olx.source.CartoDBOptions.prototype.projection; olx.source.CartoDBOptions.prototype.maxZoom; +/** + * Minimum zoom. + * @type {number|undefined} + * @api + */ +olx.source.CartoDBOptions.prototype.minZoom; + + /** * Whether to wrap the world horizontally. Default is `true`. * @type {boolean|undefined} @@ -6279,10 +6296,10 @@ olx.source.CartoDBOptions.prototype.wrapX; /** * If using anonymous maps, the CartoDB config to use. See - * {@link http://docs.cartodb.com/cartodb-platform/maps-api.html#anonymous-maps} + * {@link http://docs.cartodb.com/cartodb-platform/maps-api/anonymous-maps/} * for more detail. * If using named maps, a key-value lookup with the template parameters. - * See {@link http://docs.cartodb.com/cartodb-platform/maps-api.html#named-maps} + * See {@link http://docs.cartodb.com/cartodb-platform/maps-api/named-maps/} * for more detail. * @type {Object|undefined} * @api @@ -6292,7 +6309,7 @@ olx.source.CartoDBOptions.prototype.config; /** * If using named maps, this will be the name of the template to load. - * See {@link http://docs.cartodb.com/cartodb-platform/maps-api.html#named-maps} + * See {@link http://docs.cartodb.com/cartodb-platform/maps-api/named-maps/} * for more detail. * @type {string|undefined} * @api diff --git a/src/ol/source/cartodb.js b/src/ol/source/cartodb.js index afb9c8c262..9ff67ac463 100644 --- a/src/ol/source/cartodb.js +++ b/src/ol/source/cartodb.js @@ -14,12 +14,41 @@ goog.require('ol.source.XYZ'); * @api */ ol.source.CartoDB = function(options) { + + /** + * @type {string} + * @private + */ this.account_ = options.account; + + /** + * @type {string} + * @private + */ this.mapId_ = options.map || ''; + + /** + * @type {Object} + * @private + */ this.config_ = options.config || {}; + + /** + * @type {!Object.} + * @private + */ this.templateCache_ = {}; - delete options.map; - goog.base(this, options); + + goog.base(this, { + attributions: options.attributions, + cacheSize: options.cacheSize, + crossOrigin: options.crossOrigin, + logo: options.logo, + maxZoom: options.maxZoom !== undefined ? options.maxZoom : 18, + minZoom: options.minZoom, + projection: options.projection, + wrapX: options.wrapX + }); this.initializeMap_(); }; goog.inherits(ol.source.CartoDB, ol.source.XYZ); @@ -99,7 +128,7 @@ ol.source.CartoDB.prototype.handleInitResponse_ = function(paramHash, event) { if (client.status >= 200 && client.status < 300) { var response; try { - response = /** @type {Object} */(JSON.parse(client.responseText)); + response = /** @type {CartoDBLayerInfo} */(JSON.parse(client.responseText)); } catch (err) { this.setState(ol.source.State.ERROR); return; @@ -111,6 +140,7 @@ ol.source.CartoDB.prototype.handleInitResponse_ = function(paramHash, event) { } }; + /** * @private * @param {Event} event Event. @@ -119,14 +149,14 @@ ol.source.CartoDB.prototype.handleInitError_ = function(event) { this.setState(ol.source.State.ERROR); } + /** * Apply the new tile urls returned by carto db - * @param {Object} data Result of carto db call. + * @param {CartoDBLayerInfo} data Result of carto db call. * @private */ ol.source.CartoDB.prototype.applyTemplate_ = function(data) { - var layerId = data['layergroupid']; - var tilesUrl = 'https://' + data['cdn_url']['https'] + '/' + this.account_ + - '/api/v1/map/' + layerId + '/{z}/{x}/{y}.png'; + var tilesUrl = 'https://' + data.cdn_url.https + '/' + this.account_ + + '/api/v1/map/' + data.layergroupid + '/{z}/{x}/{y}.png'; this.setUrl(tilesUrl); }; diff --git a/test/spec/ol/source/cartodbsource.test.js b/test/spec/ol/source/cartodbsource.test.js index 9f62f6981c..201d276d84 100644 --- a/test/spec/ol/source/cartodbsource.test.js +++ b/test/spec/ol/source/cartodbsource.test.js @@ -8,7 +8,7 @@ describe('ol.source.CartoDB', function() { describe('constructor', function() { it('returns a CartoDB source', function() { var source = new ol.source.CartoDB({ - map: 'example', + account: 'documentation', config: {} }); expect(source).to.be.a(ol.source.XYZ);