Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] Add ordering to layer wizard registration API #122999

Merged
merged 7 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions x-pack/plugins/maps/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ export {
SOURCE_TYPES,
STYLE_TYPE,
SYMBOLIZE_AS_TYPES,
LAYER_WIZARD_CATEGORY,
MAX_ZOOM,
MIN_ZOOM,
VECTOR_SHAPE_TYPE,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

piggy-backing of this PR, but not really related to the ordering-issue. But the export of these types is required for #122862 as well

} from './constants';

export type { FieldFormatter } from './constants';

export type {
EMSFileSourceDescriptor,
ESTermSourceDescriptor,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/maps/public/classes/layers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
*/

export type { LayerWizard, LayerWizardWithMeta, RenderWizardArguments } from './wizards';
export { getLayerWizards, registerLayerWizard } from './wizards';
export { getLayerWizards, registerLayerWizardExternal } from './wizards';
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { LayerTemplate } from './layer_template';
import { ChoroplethLayerIcon } from '../icons/cloropleth_layer_icon';

export const choroplethLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.choropleth.desc', {
defaultMessage: 'Shaded areas to compare statistics across boundaries',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ClientFileCreateSourceEditor, UPLOAD_STEPS } from './wizard';
import { getFileUpload } from '../../../../kibana_services';

export const uploadLayerWizardConfig: LayerWizard = {
order: 10,
categories: [],
description: i18n.translate('xpack.maps.fileUploadWizard.description', {
defaultMessage: 'Index GeoJSON data in Elasticsearch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ export type {
LayerWizardWithMeta,
RenderWizardArguments,
} from './layer_wizard_registry';
export { getLayerWizards, registerLayerWizard } from './layer_wizard_registry';
export { getLayerWizards, registerLayerWizardExternal } from './layer_wizard_registry';
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import React from 'react';
import {
getLayerWizards,
registerLayerWizardInternal,
registerLayerWizardExternal,
} from './layer_wizard_registry';
import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';

describe('LayerWizardRegistryTest', () => {
it('should enforce ordering', async () => {
registerLayerWizardExternal({
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: '',
icon: '',
title: 'foo',
renderWizard(): React.ReactElement<any> {
return <></>;
},
order: 100,
});

registerLayerWizardInternal({
order: 1,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: '',
icon: '',
title: 'foobar',
renderWizard(): React.ReactElement<any> {
return <></>;
},
});

registerLayerWizardInternal({
order: 1,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: '',
icon: '',
title: 'bar',
renderWizard(): React.ReactElement<any> {
return <></>;
},
});

const wizards = await getLayerWizards();

expect(wizards[0].title).toBe('foobar');
expect(wizards[1].title).toBe('bar');
expect(wizards[2].title).toBe('foo');
});

it('external users must add order higher than 99 ', async () => {
expect(() => {
registerLayerWizardExternal({
order: 99,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: '',
icon: '',
title: 'bar',
renderWizard(): React.ReactElement<any> {
return <></>;
},
});
}).toThrow(`layerWizard.order should be greater than or equal to '100'`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ import { ReactElement, FunctionComponent } from 'react';
import type { LayerDescriptor } from '../../../../common/descriptor_types';
import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';

export type LayerWizard = {
title: string;
categories: LAYER_WIZARD_CATEGORY[];
order: number;
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
description: string;
icon: string | FunctionComponent<any>;
renderWizard(renderWizardArguments: RenderWizardArguments): ReactElement<any>;
prerequisiteSteps?: Array<{ id: string; label: string }>;
disabledReason?: string;
getIsDisabled?: () => Promise<boolean> | boolean;
isBeta?: boolean;
checkVisibility?: () => Promise<boolean>;
showFeatureEditTools?: boolean;
};

export type RenderWizardArguments = {
previewLayers: (layerDescriptors: LayerDescriptor[]) => void;
mapColors: string[];
Expand All @@ -27,28 +42,14 @@ export type RenderWizardArguments = {
advanceToNextStep: () => void;
};

export type LayerWizard = {
categories: LAYER_WIZARD_CATEGORY[];
checkVisibility?: () => Promise<boolean>;
description: string;
disabledReason?: string;
getIsDisabled?: () => Promise<boolean> | boolean;
isBeta?: boolean;
icon: string | FunctionComponent<any>;
prerequisiteSteps?: Array<{ id: string; label: string }>;
renderWizard(renderWizardArguments: RenderWizardArguments): ReactElement<any>;
title: string;
showFeatureEditTools?: boolean;
};

export type LayerWizardWithMeta = LayerWizard & {
isVisible: boolean;
isDisabled: boolean;
};

const registry: LayerWizard[] = [];

export function registerLayerWizard(layerWizard: LayerWizard) {
export function registerLayerWizardInternal(layerWizard: LayerWizard) {
registry.push({
checkVisibility: async () => {
return true;
Expand All @@ -61,6 +62,13 @@ export function registerLayerWizard(layerWizard: LayerWizard) {
});
}

export function registerLayerWizardExternal(layerWizard: LayerWizard) {
if (layerWizard.order < 100) {
throw new Error(`layerWizard.order should be greater than or equal to '100`);
}
registerLayerWizardInternal(layerWizard);
}

export async function getLayerWizards(): Promise<LayerWizardWithMeta[]> {
const promises = registry.map(async (layerWizard: LayerWizard) => {
return {
Expand All @@ -69,7 +77,11 @@ export async function getLayerWizards(): Promise<LayerWizardWithMeta[]> {
isDisabled: await layerWizard.getIsDisabled!(),
};
});
return (await Promise.all(promises)).filter(({ isVisible }) => {
return isVisible;
});
return (await Promise.all(promises))
.filter(({ isVisible }) => {
return isVisible;
})
.sort((wizard1: LayerWizardWithMeta, wizard2: LayerWizardWithMeta) => {
return wizard1.order - wizard2.order;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { registerLayerWizard } from './layer_wizard_registry';
import { registerLayerWizardInternal } from './layer_wizard_registry';
import { uploadLayerWizardConfig } from './file_upload_wizard';
import {
esDocumentsLayerWizardConfig,
Expand All @@ -16,17 +16,12 @@ import {
heatmapLayerWizardConfig,
} from '../../sources/es_geo_grid_source';
import { geoLineLayerWizardConfig } from '../../sources/es_geo_line_source';
// @ts-ignore
import { point2PointLayerWizardConfig } from '../../sources/es_pew_pew_source';
// @ts-ignore
import { point2PointLayerWizardConfig } from '../../sources/es_pew_pew_source/point_2_point_layer_wizard';
import { emsBoundariesLayerWizardConfig } from '../../sources/ems_file_source';
// @ts-ignore
import { emsBaseMapLayerWizardConfig } from '../../sources/ems_tms_source';
// @ts-ignore
import { kibanaBasemapLayerWizardConfig } from '../../sources/kibana_tilemap_source';
import { kibanaBasemapLayerWizardConfig } from '../../sources/kibana_tilemap_source/kibana_base_map_layer_wizard';
import { tmsLayerWizardConfig } from '../../sources/xyz_tms_source';
// @ts-ignore
import { wmsLayerWizardConfig } from '../../sources/wms_source';
import { wmsLayerWizardConfig } from '../../sources/wms_source/wms_layer_wizard';
import { mvtVectorSourceWizardConfig } from '../../sources/mvt_single_layer_vector_source';
import { ObservabilityLayerWizardConfig } from './solution_layers/observability';
import { SecurityLayerWizardConfig } from './solution_layers/security';
Expand All @@ -39,31 +34,23 @@ export function registerLayerWizards() {
return;
}

// Registration order determines display order
registerLayerWizard(uploadLayerWizardConfig);
registerLayerWizard(esDocumentsLayerWizardConfig);
// @ts-ignore
registerLayerWizard(choroplethLayerWizardConfig);
registerLayerWizard(clustersLayerWizardConfig);
// @ts-ignore
registerLayerWizard(heatmapLayerWizardConfig);
registerLayerWizard(esTopHitsLayerWizardConfig);
registerLayerWizard(geoLineLayerWizardConfig);
// @ts-ignore
registerLayerWizard(point2PointLayerWizardConfig);
// @ts-ignore
registerLayerWizard(emsBoundariesLayerWizardConfig);
registerLayerWizard(newVectorLayerWizardConfig);
// @ts-ignore
registerLayerWizard(emsBaseMapLayerWizardConfig);
// @ts-ignore
registerLayerWizard(kibanaBasemapLayerWizardConfig);
registerLayerWizard(tmsLayerWizardConfig);
// @ts-ignore
registerLayerWizard(wmsLayerWizardConfig);
registerLayerWizardInternal(uploadLayerWizardConfig);
registerLayerWizardInternal(esDocumentsLayerWizardConfig);
registerLayerWizardInternal(choroplethLayerWizardConfig);
registerLayerWizardInternal(clustersLayerWizardConfig);
registerLayerWizardInternal(heatmapLayerWizardConfig);
registerLayerWizardInternal(esTopHitsLayerWizardConfig);
registerLayerWizardInternal(geoLineLayerWizardConfig);
registerLayerWizardInternal(point2PointLayerWizardConfig);
registerLayerWizardInternal(emsBoundariesLayerWizardConfig);
registerLayerWizardInternal(newVectorLayerWizardConfig);
registerLayerWizardInternal(emsBaseMapLayerWizardConfig);
registerLayerWizardInternal(kibanaBasemapLayerWizardConfig);
registerLayerWizardInternal(tmsLayerWizardConfig);
registerLayerWizardInternal(wmsLayerWizardConfig);

registerLayerWizard(mvtVectorSourceWizardConfig);
registerLayerWizard(ObservabilityLayerWizardConfig);
registerLayerWizard(SecurityLayerWizardConfig);
registerLayerWizardInternal(mvtVectorSourceWizardConfig);
registerLayerWizardInternal(ObservabilityLayerWizardConfig);
registerLayerWizardInternal(SecurityLayerWizardConfig);
registered = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LAYER_WIZARD_CATEGORY } from '../../../../../common/constants';
const ADD_VECTOR_DRAWING_LAYER = 'ADD_VECTOR_DRAWING_LAYER';

export const newVectorLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.newVectorLayerWizard.description', {
defaultMessage: 'Draw shapes on the map and index in Elasticsearch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { APM_INDEX_PATTERN_ID } from './create_layer_descriptor';
import { getIndexPatternService } from '../../../../../kibana_services';

export const ObservabilityLayerWizardConfig: LayerWizard = {
order: 20,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH, LAYER_WIZARD_CATEGORY.SOLUTIONS],
getIsDisabled: async () => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getSecurityIndexPatterns } from './security_index_pattern_utils';
import { SecurityLayerTemplate } from './security_layer_template';

export const SecurityLayerWizardConfig: LayerWizard = {
order: 20,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH, LAYER_WIZARD_CATEGORY.SOLUTIONS],
getIsDisabled: async () => {
const indexPatterns = await getSecurityIndexPatterns();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function getDescription() {
}

export const emsBoundariesLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
checkVisibility: async () => {
const emsSettings = getEMSSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function getDescription() {
}

export const emsBaseMapLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
checkVisibility: async () => {
const emsSettings = getEMSSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { NUMERICAL_COLOR_PALETTES } from '../../styles/color_palettes';
import { ClustersLayerIcon } from '../../layers/wizards/icons/clusters_layer_icon';

export const clustersLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.esGridClustersDescription', {
defaultMessage: 'Geospatial data grouped in grids with metrics for each gridded cell',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { GRID_RESOLUTION, LAYER_WIZARD_CATEGORY, RENDER_AS } from '../../../../c
import { HeatmapLayerIcon } from '../../layers/wizards/icons/heatmap_layer_icon';

export const heatmapLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.esGridHeatmapDescription', {
defaultMessage: 'Geospatial data grouped in grids to show density',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getIsGoldPlus } from '../../../licensed_features';
import { TracksLayerIcon } from '../../layers/wizards/icons/tracks_layer_icon';

export const geoLineLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.esGeoLineDescription', {
defaultMessage: 'Create lines from points',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ColorDynamicOptions, SizeDynamicOptions } from '../../../../common/desc
import { Point2PointLayerIcon } from '../../layers/wizards/icons/point_2_point_layer_icon';

export const point2PointLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.pewPewDescription', {
defaultMessage: 'Aggregated data paths between the source and destination',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function createDefaultLayerDescriptor(
}

export const esDocumentsLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.esSearchDescription', {
defaultMessage: 'Points, lines, and polygons from Elasticsearch',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ESSearchSourceDescriptor } from '../../../../../common/descriptor_types
import { ESSearchSource } from '../es_search_source';

export const esTopHitsLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.ELASTICSEARCH],
description: i18n.translate('xpack.maps.source.topHitsDescription', {
defaultMessage:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getKibanaTileMap } from '../../../util';
import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';

export const kibanaBasemapLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
checkVisibility: async () => {
const tilemap = getKibanaTileMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TiledSingleLayerVectorSourceSettings } from '../../../../common/descrip
import { VectorTileLayerIcon } from '../../layers/wizards/icons/vector_tile_layer_icon';

export const mvtVectorSourceWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: i18n.translate('xpack.maps.source.mvtVectorSourceWizard', {
defaultMessage: 'Data service implementing the Mapbox vector tile specification',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';
import { WebMapServiceLayerIcon } from '../../layers/wizards/icons/web_map_service_layer_icon';

export const wmsLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: i18n.translate('xpack.maps.source.wmsDescription', {
defaultMessage: 'Maps from OGC Standard WMS',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { LAYER_WIZARD_CATEGORY } from '../../../../common/constants';
import { WorldMapLayerIcon } from '../../layers/wizards/icons/world_map_layer_icon';

export const tmsLayerWizardConfig: LayerWizard = {
order: 10,
categories: [LAYER_WIZARD_CATEGORY.REFERENCE],
description: i18n.translate('xpack.maps.source.ems_xyzDescription', {
defaultMessage: 'Raster image tile map service using {z}/{x}/{y} url pattern.',
Expand Down
Loading