Skip to content

Commit

Permalink
[Maps] Add 'InjectedData' class and revise so File Upload Features ar…
Browse files Browse the repository at this point in the history
…e assigned to new InjectedData instances (#46381) (#47158)

* Add 'PushedData' class and revise so data is assigned to new instances

* Update 'pushedData' refs to 'injectedData'

* Set default visibility to true on injected data

* Review feedback

* Update tests to account for injectedData

* Review feedback
  • Loading branch information
Aaron Caldwell authored Oct 3, 2019
1 parent 3e445c2 commit 5e0dc09
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('kibana.yml configured with map.tilemap.url', () => {
expect(layers).toEqual([{
alpha: 1,
__dataRequests: [],
__injectedData: null,
id: layers[0].id,
applyGlobalQuery: true,
label: null,
Expand Down Expand Up @@ -86,6 +87,7 @@ describe('EMS is enabled', () => {
expect(layers).toEqual([{
alpha: 1,
__dataRequests: [],
__injectedData: null,
id: layers[0].id,
applyGlobalQuery: true,
label: null,
Expand Down
16 changes: 15 additions & 1 deletion x-pack/legacy/plugins/maps/public/layers/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { EuiIcon, EuiLoadingSpinner } from '@elastic/eui';
import turf from 'turf';
import turfBooleanContains from '@turf/boolean-contains';
import { DataRequest } from './util/data_request';
import { MB_SOURCE_ID_LAYER_ID_PREFIX_DELIMITER, SOURCE_DATA_ID_ORIGIN } from '../../common/constants';
import { InjectedData } from './util/injected_data';
import {
MB_SOURCE_ID_LAYER_ID_PREFIX_DELIMITER,
SOURCE_DATA_ID_ORIGIN
} from '../../common/constants';
import uuid from 'uuid/v4';
import { copyPersistentState } from '../reducers/util';
import { i18n } from '@kbn/i18n';
Expand All @@ -28,6 +32,11 @@ 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) {
Expand All @@ -39,6 +48,7 @@ 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);
Expand Down Expand Up @@ -277,6 +287,10 @@ 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,9 @@ export class GeojsonFileSource extends AbstractVectorSource {
applyGlobalQuery: DEFAULT_APPLY_GLOBAL_QUERY
}

static createDescriptor(geoJson, name) {
// Wrap feature as feature collection if needed
const featureCollection = (geoJson.type === 'Feature')
? {
type: 'FeatureCollection',
features: [{ ...geoJson }]
}
: geoJson;

static createDescriptor(name) {
return {
type: GeojsonFileSource.type,
featureCollection,
name
};
}
Expand Down Expand Up @@ -95,9 +86,16 @@ export class GeojsonFileSource extends AbstractVectorSource {
onPreviewSource(null);
return;
}
const sourceDescriptor = GeojsonFileSource.createDescriptor(geojsonFile, name);
const sourceDescriptor = GeojsonFileSource.createDescriptor(name);
const source = new GeojsonFileSource(sourceDescriptor, inspectorAdapters);
onPreviewSource(source);
const featureCollection = (geojsonFile.type === 'Feature')
? {
type: 'FeatureCollection',
features: [{ ...geojsonFile }]
}
: geojsonFile;

onPreviewSource(source, { __injectedData: featureCollection });
};
};

Expand Down Expand Up @@ -128,22 +126,6 @@ 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;
}
Expand All @@ -156,4 +138,7 @@ export class GeojsonFileSource extends AbstractVectorSource {
return GeojsonFileSource.isIndexingSource;
}

isInjectedData() {
return true;
}
}
4 changes: 4 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/sources/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export class AbstractSource {
return AbstractSource.isIndexingSource;
}

isInjectedData() {
return false;
}

supportsElasticsearchFilters() {
return false;
}
Expand Down
21 changes: 21 additions & 0 deletions x-pack/legacy/plugins/maps/public/layers/util/injected_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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;
}

}

52 changes: 39 additions & 13 deletions x-pack/legacy/plugins/maps/public/layers/vector_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,20 @@ 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 sourceDataRequest = this.getSourceDataRequest();
const featureCollection = sourceDataRequest ? sourceDataRequest.getData() : null;
const featureCollection = this._getSourceFeatureCollection();

const noResultsIcon = (
<EuiIcon
Expand All @@ -145,9 +156,10 @@ export class VectorLayer extends AbstractLayer {
if (!featureCollection || featureCollection.features.length === 0) {
return {
icon: noResultsIcon,
tooltipContent: i18n.translate('xpack.maps.vectorLayer.noResultsFoundTooltip', {
defaultMessage: `No results found.`
})
tooltipContent: i18n.translate(
'xpack.maps.vectorLayer.noResultsFoundTooltip', {
defaultMessage: `No results found.`
})
};
}

Expand All @@ -162,7 +174,7 @@ export class VectorLayer extends AbstractLayer {
};
}


const sourceDataRequest = this.getSourceDataRequest();
const { tooltipContent, areResultsTrimmed } = this._source.getSourceTooltipContent(sourceDataRequest);
return {
icon: this._style.getIcon(),
Expand Down Expand Up @@ -464,7 +476,17 @@ export class VectorLayer extends AbstractLayer {
}
}

async _syncSource({ startLoading, stopLoading, onLoadError, registerCancelCallback, dataFilters }) {
async _syncSource({
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`);

Expand All @@ -481,10 +503,10 @@ export class VectorLayer extends AbstractLayer {
try {
startLoading(SOURCE_DATA_ID_ORIGIN, requestToken, searchFilters);
const layerName = await this.getDisplayName();
const {
data: featureCollection,
meta
} = await this._source.getGeoJsonWithMeta(layerName, searchFilters, registerCancelCallback.bind(null, requestToken));
const { data: featureCollection, meta } =
await this._source.getGeoJsonWithMeta(layerName, searchFilters,
registerCancelCallback.bind(null, requestToken)
);
this._assignIdsToFeatures(featureCollection);
stopLoading(SOURCE_DATA_ID_ORIGIN, requestToken, featureCollection, meta);
return {
Expand Down Expand Up @@ -544,8 +566,12 @@ export class VectorLayer extends AbstractLayer {
}

_getSourceFeatureCollection() {
const sourceDataRequest = this.getSourceDataRequest();
return sourceDataRequest ? sourceDataRequest.getData() : null;
if (this._source.isInjectedData()) {
return this.getInjectedData();
} else {
const sourceDataRequest = this.getSourceDataRequest();
return sourceDataRequest ? sourceDataRequest.getData() : null;
}
}

_syncFeatureCollectionWithMb(mbMap) {
Expand Down

0 comments on commit 5e0dc09

Please sign in to comment.