Skip to content

Commit

Permalink
[Maps] allow adding multiple layers (elastic#67544)
Browse files Browse the repository at this point in the history
* [Maps] allow adding multiple layers

* update RenderWizardArguments arguments

* fix toc_entry jest test

* fix tslint error

* cleanup

* remove __transientLayerId from store signature

* rename setSelectedLayerToFirstPreviewLayer

* revert changes to es_search_source/create_source_editor.js
  • Loading branch information
nreese committed May 28, 2020
1 parent 9de5b29 commit 9d1e4d4
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 150 deletions.
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/maps/public/angular/map_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
setGotoWithCenter,
replaceLayerList,
setQuery,
clearTransientLayerStateAndCloseFlyout,
setMapSettings,
enableFullScreen,
updateFlyout,
Expand Down Expand Up @@ -535,7 +534,6 @@ app.controller(
addHelpMenuToAppChrome();

async function doSave(saveOptions) {
await store.dispatch(clearTransientLayerStateAndCloseFlyout());
savedMap.syncWithStore(store.getState());
let id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export type SourceDescriptor =
export type LayerDescriptor = {
__dataRequests?: DataRequestDescriptor[];
__isInErrorState?: boolean;
__isPreviewLayer?: boolean;
__errorMessage?: string;
__trackedLayerDescriptor?: LayerDescriptor;
alpha?: number;
Expand Down
64 changes: 42 additions & 22 deletions x-pack/plugins/maps/public/actions/layer_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import { Query } from 'src/plugins/data/public';
import { MapStoreState } from '../reducers/store';
import {
getLayerById,
getLayerList,
getLayerListRaw,
getSelectedLayerId,
getMapReady,
getTransientLayerId,
} from '../selectors/map_selectors';
import { FLYOUT_STATE } from '../reducers/ui';
import { cancelRequest } from '../reducers/non_serializable_instances';
Expand All @@ -27,7 +27,6 @@ import {
SET_JOINS,
SET_LAYER_VISIBILITY,
SET_SELECTED_LAYER,
SET_TRANSIENT_LAYER,
SET_WAITING_FOR_READY_HIDDEN_LAYERS,
TRACK_CURRENT_LAYER_STATE,
UPDATE_LAYER_ORDER,
Expand Down Expand Up @@ -139,6 +138,41 @@ export function addLayerWithoutDataSync(layerDescriptor: LayerDescriptor) {
};
}

export function addPreviewLayers(layerDescriptors: LayerDescriptor[]) {
return (dispatch: Dispatch) => {
dispatch<any>(removePreviewLayers());

layerDescriptors.forEach((layerDescriptor) => {
dispatch<any>(addLayer({ ...layerDescriptor, __isPreviewLayer: true }));
});
};
}

export function removePreviewLayers() {
return (dispatch: Dispatch, getState: () => MapStoreState) => {
getLayerList(getState()).forEach((layer) => {
if (layer.isPreviewLayer()) {
dispatch<any>(removeLayer(layer.getId()));
}
});
};
}

export function promotePreviewLayers() {
return (dispatch: Dispatch, getState: () => MapStoreState) => {
getLayerList(getState()).forEach((layer) => {
if (layer.isPreviewLayer()) {
dispatch({
type: UPDATE_LAYER_PROP,
id: layer.getId(),
propName: '__isPreviewLayer',
newValue: false,
});
}
});
};
}

export function setLayerVisibility(layerId: string, makeVisible: boolean) {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
// if the current-state is invisible, we also want to sync data
Expand Down Expand Up @@ -193,31 +227,17 @@ export function setSelectedLayer(layerId: string | null) {
};
}

export function removeTransientLayer() {
export function setFirstPreviewLayerToSelectedLayer() {
return async (dispatch: Dispatch, getState: () => MapStoreState) => {
const transientLayerId = getTransientLayerId(getState());
if (transientLayerId) {
await dispatch<any>(removeLayerFromLayerList(transientLayerId));
await dispatch<any>(setTransientLayer(null));
const firstPreviewLayer = getLayerList(getState()).find((layer) => {
return layer.isPreviewLayer();
});
if (firstPreviewLayer) {
dispatch<any>(setSelectedLayer(firstPreviewLayer.getId()));
}
};
}

export function setTransientLayer(layerId: string | null) {
return {
type: SET_TRANSIENT_LAYER,
transientLayerId: layerId,
};
}

export function clearTransientLayerStateAndCloseFlyout() {
return async (dispatch: Dispatch) => {
await dispatch(updateFlyout(FLYOUT_STATE.NONE));
await dispatch<any>(setSelectedLayer(null));
await dispatch<any>(removeTransientLayer());
};
}

export function updateLayerOrder(newLayerOrder: number[]) {
return {
type: UPDATE_LAYER_ORDER,
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/maps/public/actions/map_action_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

export const SET_SELECTED_LAYER = 'SET_SELECTED_LAYER';
export const SET_TRANSIENT_LAYER = 'SET_TRANSIENT_LAYER';
export const UPDATE_LAYER_ORDER = 'UPDATE_LAYER_ORDER';
export const ADD_LAYER = 'ADD_LAYER';
export const SET_LAYER_ERROR_STATUS = 'SET_LAYER_ERROR_STATUS';
Expand Down
5 changes: 5 additions & 0 deletions x-pack/plugins/maps/public/classes/layers/layer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface ILayer {
getInFlightRequestTokens(): symbol[];
getPrevRequestToken(dataId: string): symbol | undefined;
destroy: () => void;
isPreviewLayer: () => boolean;
}
export type Footnote = {
icon: ReactElement<any>;
Expand Down Expand Up @@ -179,6 +180,10 @@ export class AbstractLayer implements ILayer {
return this.getSource().isJoinable();
}

isPreviewLayer(): boolean {
return !!this._descriptor.__isPreviewLayer;
}

supportsElasticsearchFilters(): boolean {
return this.getSource().isESSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ReactElement } from 'react';
import { LayerDescriptor } from '../../../common/descriptor_types';

export type RenderWizardArguments = {
previewLayer: (layerDescriptor: LayerDescriptor | null, isIndexingSource?: boolean) => void;
previewLayers: (layerDescriptors: LayerDescriptor[], isIndexingSource?: boolean) => void;
mapColors: string[];
// upload arguments
isIndexingTriggered: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ export class ObservabilityLayerTemplate extends Component<RenderWizardArguments,
};

_previewLayer() {
this.props.previewLayer(
createLayerDescriptor({
layer: this.state.layer,
metric: this.state.metric,
display: this.state.display,
})
);
const layerDescriptor = createLayerDescriptor({
layer: this.state.layer,
metric: this.state.metric,
display: this.state.display,
});
this.props.previewLayers(layerDescriptor ? [layerDescriptor] : []);
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const uploadLayerWizardConfig: LayerWizard = {
icon: 'importAction',
isIndexingSource: true,
renderWizard: ({
previewLayer,
previewLayers,
mapColors,
isIndexingTriggered,
onRemove,
Expand All @@ -38,13 +38,13 @@ export const uploadLayerWizardConfig: LayerWizard = {
}: RenderWizardArguments) => {
function previewGeojsonFile(geojsonFile: unknown, name: string) {
if (!geojsonFile) {
previewLayer(null);
previewLayers([]);
return;
}
const sourceDescriptor = GeojsonFileSource.createDescriptor(geojsonFile, name);
const layerDescriptor = VectorLayer.createDescriptor({ sourceDescriptor }, mapColors);
// TODO figure out a better way to handle passing this information back to layer_addpanel
previewLayer(layerDescriptor, true);
previewLayers([layerDescriptor], true);
}

function viewIndexedData(indexResponses: {
Expand Down Expand Up @@ -72,7 +72,7 @@ export const uploadLayerWizardConfig: LayerWizard = {
)
);
if (!indexPatternId || !geoField) {
previewLayer(null);
previewLayers([]);
} else {
const esSearchSourceConfig = {
indexPatternId,
Expand All @@ -85,7 +85,7 @@ export const uploadLayerWizardConfig: LayerWizard = {
? SCALING_TYPES.CLUSTERS
: SCALING_TYPES.LIMIT,
};
previewLayer(createDefaultLayerDescriptor(esSearchSourceConfig, mapColors));
previewLayers([createDefaultLayerDescriptor(esSearchSourceConfig, mapColors)]);
importSuccessHandler(indexResponses);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export const emsBoundariesLayerWizardConfig: LayerWizard = {
defaultMessage: 'Administrative boundaries from Elastic Maps Service',
}),
icon: 'emsApp',
renderWizard: ({ previewLayer, mapColors }: RenderWizardArguments) => {
renderWizard: ({ previewLayers, mapColors }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: Partial<EMSFileSourceDescriptor>) => {
const sourceDescriptor = EMSFileSource.createDescriptor(sourceConfig);
const layerDescriptor = VectorLayer.createDescriptor({ sourceDescriptor }, mapColors);
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};
return <EMSFileCreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export const emsBaseMapLayerWizardConfig: LayerWizard = {
defaultMessage: 'Tile map service from Elastic Maps Service',
}),
icon: 'emsApp',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
const layerDescriptor = VectorTileLayer.createDescriptor({
sourceDescriptor: EMSTMSSource.createDescriptor(sourceConfig),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return <TileServiceSelect onTileSelect={onSourceConfigChange} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ export const clustersLayerWizardConfig: LayerWizard = {
defaultMessage: 'Geospatial data grouped in grids with metrics for each gridded cell',
}),
icon: 'logoElasticsearch',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: Partial<ESGeoGridSourceDescriptor>) => {
if (!sourceConfig) {
previewLayer(null);
previewLayers([]);
return;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export const clustersLayerWizardConfig: LayerWizard = {
},
}),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ export const heatmapLayerWizardConfig: LayerWizard = {
defaultMessage: 'Geospatial data grouped in grids to show density',
}),
icon: 'logoElasticsearch',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: Partial<ESGeoGridSourceDescriptor>) => {
if (!sourceConfig) {
previewLayer(null);
previewLayers([]);
return;
}

const layerDescriptor = HeatmapLayer.createDescriptor({
sourceDescriptor: ESGeoGridSource.createDescriptor(sourceConfig),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export const point2PointLayerWizardConfig: LayerWizard = {
defaultMessage: 'Aggregated data paths between the source and destination',
}),
icon: 'logoElasticsearch',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
if (!sourceConfig) {
previewLayer(null);
previewLayers([]);
return;
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export const point2PointLayerWizardConfig: LayerWizard = {
},
}),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return <CreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export const esDocumentsLayerWizardConfig: LayerWizard = {
defaultMessage: 'Vector data from a Kibana index pattern',
}),
icon: 'logoElasticsearch',
renderWizard: ({ previewLayer, mapColors }: RenderWizardArguments) => {
renderWizard: ({ previewLayers, mapColors }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
if (!sourceConfig) {
previewLayer(null);
previewLayers([]);
return;
}

previewLayer(createDefaultLayerDescriptor(sourceConfig, mapColors));
previewLayers([createDefaultLayerDescriptor(sourceConfig, mapColors)]);
};
return <CreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export const kibanaRegionMapLayerWizardConfig: LayerWizard = {
defaultMessage: 'Vector data from hosted GeoJSON configured in kibana.yml',
}),
icon: 'logoKibana',
renderWizard: ({ previewLayer, mapColors }: RenderWizardArguments) => {
renderWizard: ({ previewLayers, mapColors }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
const sourceDescriptor = KibanaRegionmapSource.createDescriptor(sourceConfig);
const layerDescriptor = VectorLayer.createDescriptor({ sourceDescriptor }, mapColors);
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return <CreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export const kibanaBasemapLayerWizardConfig: LayerWizard = {
defaultMessage: 'Tile map service configured in kibana.yml',
}),
icon: 'logoKibana',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = () => {
const layerDescriptor = TileLayer.createDescriptor({
sourceDescriptor: KibanaTilemapSource.createDescriptor(),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};
return <CreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const mvtVectorSourceWizardConfig: LayerWizard = {
defaultMessage: 'Vector source wizard',
}),
icon: 'grid',
renderWizard: ({ previewLayer, mapColors }: RenderWizardArguments) => {
renderWizard: ({ previewLayers, mapColors }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: MVTSingleLayerVectorSourceConfig) => {
const sourceDescriptor = MVTSingleLayerVectorSource.createDescriptor(sourceConfig);
const layerDescriptor = TiledVectorLayer.createDescriptor({ sourceDescriptor }, mapColors);
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};

return <MVTSingleLayerVectorSourceEditor onSourceConfigChange={onSourceConfigChange} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ export const wmsLayerWizardConfig: LayerWizard = {
defaultMessage: 'Maps from OGC Standard WMS',
}),
icon: 'grid',
renderWizard: ({ previewLayer }: RenderWizardArguments) => {
renderWizard: ({ previewLayers }: RenderWizardArguments) => {
const onSourceConfigChange = (sourceConfig: unknown) => {
if (!sourceConfig) {
previewLayer(null);
previewLayers([]);
return;
}

const layerDescriptor = TileLayer.createDescriptor({
sourceDescriptor: WMSSource.createDescriptor(sourceConfig),
});
previewLayer(layerDescriptor);
previewLayers([layerDescriptor]);
};
return <WMSCreateSourceEditor onSourceConfigChange={onSourceConfigChange} />;
},
Expand Down
Loading

0 comments on commit 9d1e4d4

Please sign in to comment.