diff --git a/public/utils.js b/public/utils.js index 4a039af..071ef1e 100644 --- a/public/utils.js +++ b/public/utils.js @@ -103,15 +103,28 @@ define(function (require) { }; }, getMapStateFromVis: function(vis) { - const mapState = { - center: [15, 5], - zoom: 2 - } + const mapState = {}; + //Visualizations created in 5.x will have map state in uiState if (vis.hasUiState()) { const uiStateCenter = vis.uiStateVal('mapCenter'); - if(uiStateCenter) mapState.center = uiStateCenter; const uiStateZoom = vis.uiStateVal('mapZoom'); - if(uiStateZoom) mapState.zoom = uiStateZoom; + if(uiStateCenter && uiStateZoom) { + mapState.center = uiStateCenter; + mapState.zoom = uiStateZoom; + } + } + //Visualizations created in 4.x will have map state in segment aggregation + if (!_.has(mapState, 'center') && !_.has(mapState, 'zoom')) { + const agg = this.getAggConfig(vis.aggs, 'segment'); + if (agg) { + mapState.center = _.get(agg, 'params.mapCenter'); + mapState.zoom = _.get(agg, 'params.mapZoom'); + } + } + //Provide defaults if no state found + if (!_.has(mapState, 'center') && !_.has(mapState, 'zoom')) { + mapState.center = [15, 5]; + mapState.zoom = 2; } return mapState; },