[Maps] Add back removed logic copying feature properties for injected data (#49400) (#49699)

* Add back removed logic copying feature properties for injected data prior to modification

* Back out injected data and assign featureCollection to sourceDescriptor with __ prefix

* Review feedback. Ensure __featureCollection is always initialized minimally to a geojson object with an empty features array

* Check for null/undefined
This commit is contained in:
Aaron Caldwell 2019-10-30 07:57:41 -06:00 committed by GitHub
parent 8a8b042872
commit 742e8e16bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 79 deletions

View file

@ -44,7 +44,6 @@ describe('kibana.yml configured with map.tilemap.url', () => {
expect(layers).toEqual([{
alpha: 1,
__dataRequests: [],
__injectedData: null,
id: layers[0].id,
applyGlobalQuery: true,
label: null,
@ -87,7 +86,6 @@ describe('EMS is enabled', () => {
expect(layers).toEqual([{
alpha: 1,
__dataRequests: [],
__injectedData: null,
id: layers[0].id,
applyGlobalQuery: true,
label: null,

View file

@ -9,7 +9,6 @@ import { EuiIcon, EuiLoadingSpinner } from '@elastic/eui';
import turf from 'turf';
import turfBooleanContains from '@turf/boolean-contains';
import { DataRequest } from './util/data_request';
import { InjectedData } from './util/injected_data';
import {
MB_SOURCE_ID_LAYER_ID_PREFIX_DELIMITER,
SOURCE_DATA_ID_ORIGIN
@ -32,11 +31,6 @@ export class AbstractLayer {
} else {
this._dataRequests = [];
}
if (this._descriptor.__injectedData) {
this._injectedData = new InjectedData(this._descriptor.__injectedData);
} else {
this._injectedData = null;
}
}
static getBoundDataForSource(mbMap, sourceId) {
@ -48,7 +42,6 @@ export class AbstractLayer {
const layerDescriptor = { ...options };
layerDescriptor.__dataRequests = _.get(options, '__dataRequests', []);
layerDescriptor.__injectedData = _.get(options, '__injectedData', null);
layerDescriptor.id = _.get(options, 'id', uuid());
layerDescriptor.label = options.label && options.label.length > 0 ? options.label : null;
layerDescriptor.minZoom = _.get(options, 'minZoom', 0);
@ -287,10 +280,6 @@ export class AbstractLayer {
return this._dataRequests.find(dataRequest => dataRequest.getDataId() === id);
}
getInjectedData() {
return this._injectedData ? this._injectedData.getData() : null;
}
isLayerLoading() {
return this._dataRequests.some(dataRequest => dataRequest.isLoading());
}

View file

@ -35,9 +35,32 @@ export class GeojsonFileSource extends AbstractVectorSource {
applyGlobalQuery: DEFAULT_APPLY_GLOBAL_QUERY
}
static createDescriptor(name) {
static createDescriptor(geoJson, name) {
// Wrap feature as feature collection if needed
let featureCollection;
if (!geoJson) {
featureCollection = {
type: 'FeatureCollection',
features: []
};
} else if (geoJson.type === 'FeatureCollection') {
featureCollection = geoJson;
} else if (geoJson.type === 'Feature') {
featureCollection = {
type: 'FeatureCollection',
features: [geoJson]
};
} else { // Missing or incorrect type
featureCollection = {
type: 'FeatureCollection',
features: []
};
}
return {
type: GeojsonFileSource.type,
__featureCollection: featureCollection,
name
};
}
@ -85,16 +108,9 @@ export class GeojsonFileSource extends AbstractVectorSource {
onPreviewSource(null);
return;
}
const sourceDescriptor = GeojsonFileSource.createDescriptor(name);
const sourceDescriptor = GeojsonFileSource.createDescriptor(geojsonFile, name);
const source = new GeojsonFileSource(sourceDescriptor, inspectorAdapters);
const featureCollection = (geojsonFile.type === 'Feature')
? {
type: 'FeatureCollection',
features: [{ ...geojsonFile }]
}
: geojsonFile;
onPreviewSource(source, { __injectedData: featureCollection });
onPreviewSource(source);
};
};
@ -125,6 +141,22 @@ export class GeojsonFileSource extends AbstractVectorSource {
);
}
async getGeoJsonWithMeta() {
const copiedPropsFeatures = this._descriptor.__featureCollection.features
.map(feature => ({
type: 'Feature',
geometry: feature.geometry,
properties: feature.properties ? { ...feature.properties } : {}
}));
return {
data: {
type: 'FeatureCollection',
features: copiedPropsFeatures
},
meta: {}
};
}
async getDisplayName() {
return this._descriptor.name;
}
@ -136,8 +168,4 @@ export class GeojsonFileSource extends AbstractVectorSource {
shouldBeIndexed() {
return GeojsonFileSource.isIndexingSource;
}
isInjectedData() {
return true;
}
}

View file

@ -115,10 +115,6 @@ export class AbstractSource {
return AbstractSource.isIndexingSource;
}
isInjectedData() {
return false;
}
supportsElasticsearchFilters() {
return false;
}

View file

@ -1,21 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export class InjectedData {
constructor(data) {
this._descriptor = { data };
}
getData() {
return this._descriptor.data;
}
hasData() {
return !!this._descriptor.data;
}
}

View file

@ -132,18 +132,6 @@ export class VectorLayer extends AbstractLayer {
return true;
}
getInjectedData() {
const featureCollection = super.getInjectedData();
if (!featureCollection) {
return null;
}
// Set default visible property on data
featureCollection.features.forEach(
feature => _.set(feature, `properties.${FEATURE_VISIBLE_PROPERTY_NAME}`, true)
);
return featureCollection;
}
getCustomIconAndTooltipContent() {
const featureCollection = this._getSourceFeatureCollection();
@ -498,16 +486,7 @@ export class VectorLayer extends AbstractLayer {
startLoading, stopLoading, onLoadError, registerCancelCallback, dataFilters
}) {
if (this._source.isInjectedData()) {
const featureCollection = this.getInjectedData();
return {
refreshed: false,
featureCollection
};
}
const requestToken = Symbol(`layer-source-refresh:${ this.getId()} - source`);
const searchFilters = this._getSearchFilters(dataFilters);
const canSkip = await this._canSkipSourceUpdate(this._source, SOURCE_DATA_ID_ORIGIN, searchFilters);
if (canSkip) {
@ -582,12 +561,8 @@ export class VectorLayer extends AbstractLayer {
}
_getSourceFeatureCollection() {
if (this._source.isInjectedData()) {
return this.getInjectedData();
} else {
const sourceDataRequest = this.getSourceDataRequest();
return sourceDataRequest ? sourceDataRequest.getData() : null;
}
const sourceDataRequest = this.getSourceDataRequest();
return sourceDataRequest ? sourceDataRequest.getData() : null;
}
_syncFeatureCollectionWithMb(mbMap) {