2016-05-26 17:40:03 +08:00
|
|
|
function DataviewsWidgetsMapConfigAdapter() {
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DataviewsWidgetsMapConfigAdapter;
|
|
|
|
|
|
|
|
|
|
|
|
DataviewsWidgetsMapConfigAdapter.prototype.getMapConfig = function(user, requestMapConfig, params, context, callback) {
|
2016-05-27 01:32:58 +08:00
|
|
|
if (!shouldAdapt(requestMapConfig)) {
|
|
|
|
return callback(null, requestMapConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare placeholders for new dataviews created from widgets
|
|
|
|
requestMapConfig.analyses = requestMapConfig.analyses || [];
|
|
|
|
requestMapConfig.dataviews = requestMapConfig.dataviews || {};
|
|
|
|
|
|
|
|
requestMapConfig.layers.forEach(function(layer, index) {
|
2016-06-06 17:46:27 +08:00
|
|
|
var layerSourceId = getLayerSourceId(layer);
|
|
|
|
|
|
|
|
if (!layer.options.widgets) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!layerSourceId && !layer.options.sql) {
|
2016-06-03 02:17:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-27 01:32:58 +08:00
|
|
|
var dataviewSourceId = layerSourceId || 'cdb-layer-source-' + index;
|
|
|
|
// Append a new analysis if layer has no source id but sql.
|
|
|
|
if (!layerSourceId) {
|
|
|
|
requestMapConfig.analyses.push(
|
|
|
|
{
|
|
|
|
id: dataviewSourceId,
|
|
|
|
type: 'source',
|
|
|
|
params: {
|
|
|
|
query: layer.options.sql
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var source = { id: dataviewSourceId };
|
2016-06-03 01:51:16 +08:00
|
|
|
var layerWidgets = layer.options.widgets || {};
|
2016-05-27 01:32:58 +08:00
|
|
|
Object.keys(layerWidgets).forEach(function(widgetId) {
|
|
|
|
var dataview = layerWidgets[widgetId];
|
|
|
|
requestMapConfig.dataviews[widgetId] = {
|
|
|
|
source: source,
|
|
|
|
type: dataview.type,
|
|
|
|
options: dataview.options
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
layer.options.source = source;
|
|
|
|
|
|
|
|
delete layer.options.sql;
|
|
|
|
// don't delete widgets for now as it might be useful for old clients
|
|
|
|
//delete layer.options.widgets;
|
|
|
|
});
|
|
|
|
|
|
|
|
// filters have to be rewritten also
|
|
|
|
var filters = getFilters(params);
|
|
|
|
var layersFilters = filters.layers || [];
|
|
|
|
filters.dataviews = filters.dataviews || {};
|
|
|
|
|
|
|
|
layersFilters.forEach(function(layerFilters) {
|
|
|
|
Object.keys(layerFilters).forEach(function(filterName) {
|
|
|
|
if (!filters.dataviews.hasOwnProperty(filterName)) {
|
|
|
|
filters.dataviews[filterName] = layerFilters[filterName];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
delete filters.layers;
|
|
|
|
|
|
|
|
params.filters = JSON.stringify(filters);
|
|
|
|
|
2016-05-26 17:40:03 +08:00
|
|
|
return callback(null, requestMapConfig);
|
|
|
|
};
|
2016-05-27 01:32:58 +08:00
|
|
|
|
|
|
|
function shouldAdapt(requestMapConfig) {
|
|
|
|
return Array.isArray(requestMapConfig.layers) && requestMapConfig.layers.some(function hasWidgets(layer) {
|
|
|
|
return layer.options && layer.options.widgets && Object.keys(layer.options.widgets).length > 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getLayerSourceId(layer) {
|
|
|
|
return layer.options.source && layer.options.source.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFilters(params) {
|
|
|
|
var filters = {};
|
|
|
|
if (params.filters) {
|
|
|
|
try {
|
|
|
|
filters = JSON.parse(params.filters);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filters;
|
|
|
|
}
|