Merge pull request #163 from CartoDB/maps_api_template

add support for maps_api_template
This commit is contained in:
javi santana 2015-04-08 10:34:04 +02:00
commit e96a1c7118
2 changed files with 51 additions and 21 deletions

View File

@ -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}', 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;

View File

@ -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");
});