From a434bd864044974452c855fa7a2216eb7c0d8fdc Mon Sep 17 00:00:00 2001 From: javi Date: Tue, 31 Mar 2015 15:20:35 +0200 Subject: [PATCH] add support for maps_api_template --- lib/torque/provider/windshaft.js | 71 ++++++++++++++++++++++---------- test/provider.windshaft.test.js | 1 + 2 files changed, 51 insertions(+), 21 deletions(-) diff --git a/lib/torque/provider/windshaft.js b/lib/torque/provider/windshaft.js index 293c5d9..db30e82 100644 --- a/lib/torque/provider/windshaft.js +++ b/lib/torque/provider/windshaft.js @@ -16,7 +16,7 @@ return str; } - var json = function (options) { + var windshaft = function (options) { this._ready = false; this._tileQueue = []; this.options = options; @@ -26,6 +26,13 @@ this.options.tiler_domain = options.tiler_domain || 'cartodb.com'; this.options.tiler_port = options.tiler_port || 80; + // backwards compatible + if (!options.maps_api_template) { + this._buildMapsApiTemplate(this.options); + } else { + this.options.maps_api_template = options.maps_api_template; + } + this.options.coordinates_data_type = this.options.coordinates_data_type || Uint8Array; if (this.options.data_aggregation) { @@ -40,7 +47,7 @@ } }; - json.prototype = { + windshaft.prototype = { /** * return the torque tile encoded in an efficient javascript @@ -339,31 +346,53 @@ } }, - _tilerHost: function() { - var opts = this.options; - var user = (opts.user_name || opts.user); - return opts.tiler_protocol + - "://" + (user ? user + "." : "") + + _buildMapsApiTemplate: function(opts) { + var user = opts.user_name || opts.user; + opts.maps_api_template = opts.tiler_protocol + + "://" + ((user) ? "{user}.":"") + opts.tiler_domain + ((opts.tiler_port != "") ? (":" + opts.tiler_port) : ""); }, - url: function() { + _tilerHost: function() { + var opts = this.options; + var user = opts.user_name || opts.user; + return opts.maps_api_template.replace('{user}', opts.user); + }, + + url: function () { var opts = this.options; - var protocol = opts.tiler_protocol || 'http'; - if (!this.options.cdn_url || this.options.no_cdn) { - return this._tilerHost(); - } - var h = protocol + "://" - if (protocol === 'http') { - h += "{s}."; - } var cdn_host = opts.cdn_url; - if(!cdn_host.http && !cdn_host.https) { - throw new Error("cdn_host should contain http and/or https entries"); + var has_empty_cdn = !cdn_host || (cdn_host && (!cdn_host.http && !cdn_host.https)); + + if (opts.no_cdn || has_empty_cdn) { + return this._tilerHost(); + } else { + var protocol = this.isHttps() ? 'https': 'http'; + var h = protocol + "://"; + if (!this.isHttps()) { + h += "{s}."; + } + var cdn_url = cdn_host[protocol]; + // build default template url if the cdn url is not templatized + // this is for backwards compatiblity, ideally we should use the url + // that tiler sends to us right away + if (!this._isUserTemplateUrl(cdn_url)) { + cdn_url = cdn_url + "/{user}"; + } + var user = opts.user_name || opts.user; + h += cdn_url.replace('{user}', user) + return h; } - h += cdn_host[protocol] + "/" + (opts.user_name || opts.user); - return h; + + }, + + _isUserTemplateUrl: function(t) { + return t && t.indexOf('{user}') !== -1; + }, + + isHttps: function() { + return this.options.maps_api_template.indexOf('https') === 0; }, _generateCartoCSS: function() { @@ -451,4 +480,4 @@ }; - module.exports = json; + module.exports = windshaft; diff --git a/test/provider.windshaft.test.js b/test/provider.windshaft.test.js index 55308d8..11a87a3 100644 --- a/test/provider.windshaft.test.js +++ b/test/provider.windshaft.test.js @@ -64,6 +64,7 @@ QUnit.module('provider.windshaft', { test("url cdn https", function() { windshaft.options.tiler_protocol = 'https'; + windshaft._buildMapsApiTemplate(windshaft.options); windshaft.options.cdn_url = { https: 'cartocdn.com' }; equal(windshaft.url(), "https://cartocdn.com/rambo"); });