Skip to content

Commit

Permalink
create map when visualization is created
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Oct 5, 2016
1 parent f86f146 commit b9df49f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
16 changes: 14 additions & 2 deletions public/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import _ from 'lodash';

define(function (require) {
const _ = require('lodash');
return {
getMapStateFromVis: function(vis) {
const mapState = {
center: [15, 5],
zoom: 2
}
_.keys(vis.aggs).forEach(function(key) {
if(key !== 'vis' && _.has(vis.aggs[key], "params.mapCenter")) {
mapState.center = vis.aggs[key].params.mapCenter;
mapState.zoom = vis.aggs[key].params.mapZoom;
}
});
return mapState;
},
isGeoFilter: function(filter, field) {
if (filter.meta.key === field
|| _.has(filter, 'geo_bounding_box.' + field)
Expand Down
24 changes: 11 additions & 13 deletions public/visController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ define(function (require) {
let TileMapMap = Private(MapProvider);
const geoJsonConverter = Private(AggResponseGeoJsonGeoJsonProvider);
let map = null;
appendMap();

//Useful bits of ui/public/vislib_vis_type/buildChartData.js
function buildChartData(resp) {
Expand All @@ -42,25 +43,23 @@ define(function (require) {

$scope.$watch('esResponse', function (resp) {
if(resp) {
resizeArea();
const chartData = buildChartData(resp);
const geoMinMax = getGeoExtents(chartData);
chartData.geoJson.properties.allmin = geoMinMax.min;
chartData.geoJson.properties.allmax = geoMinMax.max;
if (map === null) {
appendMap({
center: _.get(chartData, 'geoJson.properties.center'),
zoom: _.get(chartData, 'geoJson.properties.zoom'),
valueFormatter: _.get(chartData, 'valueFormatter')
});
}
if (_.has(chartData, 'geohashGridAgg')) {
const agg = _.get(chartData, 'geohashGridAgg');
map.addFilters(getGeoFilters(agg.fieldName()));
}
if (_.get($scope.vis.params, 'overlay.wms.enabled')) {
addWmsOverlays();
}
map.addMarkers(chartData, $scope.vis.params);
map.addMarkers(
chartData,
$scope.vis.params,
Private(require('ui/agg_response/geo_json/_tooltip_formatter')),
_.get(chartData, 'valueFormatter', _.identity));
}
});

Expand Down Expand Up @@ -162,16 +161,15 @@ define(function (require) {
return features;
}

function appendMap(options) {
function appendMap() {
const initialMapState = utils.getMapStateFromVis($scope.vis);
var params = $scope.vis.params;
var container = $element[0].querySelector('.tilemap');
map = new TileMapMap(container, {
center: options.center,
zoom: options.zoom,
center: initialMapState.center,
zoom: initialMapState.zoom,
callbacks: callbacks,
mapType: params.mapType,
tooltipFormatter: Private(require('ui/agg_response/geo_json/_tooltip_formatter')),
valueFormatter: options.valueFormatter || _.identity,
attr: params,
editable: $scope.vis.getEditableVis() ? true : false
});
Expand Down
8 changes: 5 additions & 3 deletions public/vislib/_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ define(function (require) {
const centerArray = _.get(params, 'center') || defaultMapCenter;
this._mapCenter = L.latLng(centerArray[0], centerArray[1]);
this._mapZoom = _.get(params, 'zoom') || defaultMapZoom;
this._valueFormatter = params.valueFormatter || _.identity;
this._tooltipFormatter = params.tooltipFormatter || _.identity;
this._setAttr(params.attr);
this._isEditable = params.editable || false;

Expand Down Expand Up @@ -217,7 +215,7 @@ define(function (require) {
*
* @method _addMarkers
*/
TileMapMap.prototype.addMarkers = function (chartData, newParams) {
TileMapMap.prototype.addMarkers = function (chartData, newParams, tooltipFormatter, valueFormatter) {
if(newParams) {
this._setMarkerType(newParams.mapType);
this._setAttr(newParams);
Expand All @@ -226,6 +224,10 @@ define(function (require) {
this._chartData = chartData;
this._geoJson = _.get(chartData, 'geoJson');
}
if(tooltipFormatter) this._tooltipFormatter = tooltipFormatter;
if(valueFormatter) this._valueFormatter = valueFormatter;
if(!this._tooltipFormatter) return;

if (this._markers) this._markers.destroy();

this._markers = this._createMarkers({
Expand Down

0 comments on commit b9df49f

Please sign in to comment.