2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function DataviewsWidgetsMapConfigAdapter () {
|
2016-05-26 17:40:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DataviewsWidgetsMapConfigAdapter;
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
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 || {};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
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 || {};
|
2019-10-22 01:07:24 +08:00
|
|
|
Object.keys(layerWidgets).forEach(function (widgetId) {
|
2016-05-27 01:32:58 +08:00
|
|
|
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
|
2019-10-22 01:07:24 +08:00
|
|
|
// delete layer.options.widgets;
|
2016-05-27 01:32:58 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// filters have to be rewritten also
|
|
|
|
var filters = getFilters(params);
|
|
|
|
var layersFilters = filters.layers || [];
|
|
|
|
filters.dataviews = filters.dataviews || {};
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
layersFilters.forEach(function (layerFilters) {
|
|
|
|
Object.keys(layerFilters).forEach(function (filterName) {
|
2016-05-27 01:32:58 +08:00
|
|
|
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
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function shouldAdapt (requestMapConfig) {
|
|
|
|
return Array.isArray(requestMapConfig.layers) && requestMapConfig.layers.some(function hasWidgets (layer) {
|
2016-05-27 01:32:58 +08:00
|
|
|
return layer.options && layer.options.widgets && Object.keys(layer.options.widgets).length > 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getLayerSourceId (layer) {
|
2016-05-27 01:32:58 +08:00
|
|
|
return layer.options.source && layer.options.source.id;
|
|
|
|
}
|
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function getFilters (params) {
|
2016-05-27 01:32:58 +08:00
|
|
|
var filters = {};
|
|
|
|
if (params.filters) {
|
|
|
|
try {
|
|
|
|
filters = JSON.parse(params.filters);
|
|
|
|
} catch (e) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filters;
|
|
|
|
}
|