diff --git a/lib/cartodb/controllers/named_maps.js b/lib/cartodb/controllers/named_maps.js index 590c3821..18aa746c 100644 --- a/lib/cartodb/controllers/named_maps.js +++ b/lib/cartodb/controllers/named_maps.js @@ -10,7 +10,7 @@ var cors = require('../middleware/cors'); var userMiddleware = require('../middleware/user'); function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend, - surrogateKeysCache, tablesExtentApi, metadataBackend) { + surrogateKeysCache, tablesExtentApi, metadataBackend, templateMaps) { BaseController.call(this, authApi, pgConnection); this.namedMapProviderCache = namedMapProviderCache; @@ -19,6 +19,7 @@ function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileB this.surrogateKeysCache = surrogateKeysCache; this.tablesExtentApi = tablesExtentApi; this.metadataBackend = metadataBackend; + this.templateMaps = templateMaps; } util.inherits(NamedMapsController, BaseController); @@ -126,6 +127,23 @@ NamedMapsController.prototype.staticMap = function(req, res) { function reqParams() { self.req2params(req, this); }, + function getLayerFilterFromPreviewLayers (err) { + assert.ifError(err); + + var next = this; + + self.getLayerFilterFromPreviewLayers(cdbUser, req.params.template_id, next); + }, + function applyPreviewLayers (err, layerVisibilityFilter) { + assert.ifError(err); + + if (layerVisibilityFilter) { + // overwrite 'all' default filter + req.params.layer = layerVisibilityFilter; + } + + return true; + }, function getNamedMapProvider(err) { assert.ifError(err); self.namedMapProviderCache.get( @@ -137,13 +155,9 @@ NamedMapsController.prototype.staticMap = function(req, res) { this ); }, - function layerVisibility(err, namedMapProvider) { + function prepareImageOptions(err, _namedMapProvider) { assert.ifError(err); - return decorateNamedMapProviderWithLayerVisibility(namedMapProvider); - }, - function prepareImageOptions(err, decoratedNamedMapProvider) { - assert.ifError(err); - namedMapProvider = decoratedNamedMapProvider; + namedMapProvider = _namedMapProvider; self.getStaticImageOptions(cdbUser, req.params, namedMapProvider, this); }, function getImage(err, imageOpts) { @@ -188,6 +202,53 @@ NamedMapsController.prototype.staticMap = function(req, res) { ); }; +NamedMapsController.prototype.getLayerFilterFromPreviewLayers = function (cdbUser, template_id, callback) { + var self = this; + + step( + function getTemplate () { + var next = this; + + self.templateMaps.getTemplate(cdbUser, template_id, next); + }, + function getLayerVisivilityFilter (err, template) { + assert.ifError(err); + + var next = this; + + if (!template || !template.view || !template.view.preview_layers) { + return next(); + } + + var previewLayers = template.view.preview_layers; + var layerVisibilityFilter = []; + + template.layergroup.layers.forEach(function (layer, index) { + if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) { + layerVisibilityFilter.push(''+index); + } + }); + + if (!layerVisibilityFilter.length) { + return next(); + } + + next(null, layerVisibilityFilter); + }, + function finish (err, layerVisibilityFilter) { + if (err) { + return callback(err); + } + + if (!layerVisibilityFilter) { + return callback(); + } + + callback(null, layerVisibilityFilter.join(',')); + } + ); +}; + var DEFAULT_ZOOM_CENTER = { zoom: 1, center: { @@ -200,46 +261,6 @@ function numMapper(n) { return +n; } -function decorateNamedMapProviderWithLayerVisibility (mamedMapProvider) { - var orginalGetMapConfig = mamedMapProvider.getMapConfig.bind(mamedMapProvider); - - mamedMapProvider.getMapConfig = function (callback) { - mamedMapProvider.getTemplate(function (err, template) { - if (err) { - return callback(err); - } - - if (!template.preview_layers) { - return orginalGetMapConfig(callback); - } - - var previewLayers = template.preview_layers; - orginalGetMapConfig(function (err, mapConfig, rendererParams, context) { - if (err) { - return callback(err); - } - - var layerVisibilityFilter = []; - - mapConfig.getLayers().forEach(function (layer, index) { - if (previewLayers[''+index] !== false && previewLayers[layer.id] !== false) { - layerVisibilityFilter.push(''+index); - } - }); - - if (layerVisibilityFilter.length) { - // overwrite 'all' default filter - rendererParams.layer = layerVisibilityFilter.join(','); - } - - callback(null, mapConfig, rendererParams, context); - }); - }); - }; - - return mamedMapProvider; -} - NamedMapsController.prototype.getStaticImageOptions = function(cdbUser, params, namedMapProvider, callback) { var self = this;