-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
415 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
x-pack/plugins/maps/public/legacy_visualizations/region_map/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { regionMapVisType } from './region_map_vis_type'; | ||
export { createRegionMapFn } from './region_map_fn'; | ||
export { regionMapRenderer } from './region_map_renderer'; |
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/maps/public/legacy_visualizations/region_map/region_map_editor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 type { VisEditorOptionsProps } from 'src/plugins/visualizations/public'; | ||
import { Vis } from '../../../../../../src/plugins/visualizations/public'; | ||
import { getData, getShareService } from '../../kibana_services'; | ||
import { ViewInMaps } from '../view_in_maps'; | ||
import { extractLayerDescriptorParams } from './utils'; | ||
import { RegionMapVisParams } from './types'; | ||
import { title } from './region_map_vis_type'; | ||
|
||
export function RegionMapEditor(props: VisEditorOptionsProps) { | ||
const onClick = (e: React.MouseEvent<HTMLButtonElement>) => { | ||
e.preventDefault(); | ||
|
||
const locator = getShareService().url.locators.get('MAPS_APP_REGION_MAP_LOCATOR'); | ||
if (!locator) return; | ||
|
||
const query = getData().query; | ||
locator.navigate({ | ||
...extractLayerDescriptorParams((props.vis as unknown) as Vis<RegionMapVisParams>), | ||
filters: query.filterManager.getFilters(), | ||
query: query.queryString.getQuery(), | ||
timeRange: query.timefilter.timefilter.getTime(), | ||
}); | ||
}; | ||
|
||
return <ViewInMaps onClick={onClick} visualizationLabel={title} />; | ||
} |
62 changes: 62 additions & 0 deletions
62
x-pack/plugins/maps/public/legacy_visualizations/region_map/region_map_fn.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import type { Filter, Query, TimeRange } from '../../../../../../src/plugins/data/common'; | ||
import type { ExpressionValueSearchContext } from '../../../../../../src/plugins/data/common/search/expressions/kibana_context_type'; | ||
import type { | ||
ExpressionFunctionDefinition, | ||
Render, | ||
} from '../../../../../../src/plugins/expressions/public'; | ||
import { RegionMapVisConfig } from './types'; | ||
|
||
interface Arguments { | ||
visConfig: string; | ||
} | ||
|
||
export interface RegionMapVisRenderValue { | ||
visType: 'region_map'; | ||
visConfig: RegionMapVisConfig; | ||
filters?: Filter[]; | ||
query?: Query; | ||
timeRange?: TimeRange; | ||
} | ||
|
||
export type RegionMapExpressionFunctionDefinition = ExpressionFunctionDefinition< | ||
'regionmap', | ||
ExpressionValueSearchContext, | ||
Arguments, | ||
Promise<Render<RegionMapVisRenderValue>> | ||
>; | ||
|
||
export const createRegionMapFn = (): RegionMapExpressionFunctionDefinition => ({ | ||
name: 'regionmap', | ||
type: 'render', | ||
help: i18n.translate('xpack.maps.regionMap.function.help', { | ||
defaultMessage: 'Regionmap visualization', | ||
}), | ||
args: { | ||
visConfig: { | ||
types: ['string'], | ||
default: '"{}"', | ||
help: '', | ||
}, | ||
}, | ||
async fn(input, args) { | ||
return { | ||
type: 'render', | ||
as: 'region_map_vis', | ||
value: { | ||
visType: 'region_map', | ||
visConfig: JSON.parse(args.visConfig), | ||
filters: input.filters, | ||
query: Array.isArray(input.query) ? input.query[0] : input.query, | ||
timeRange: input.timeRange, | ||
}, | ||
}; | ||
}, | ||
}); |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/maps/public/legacy_visualizations/region_map/region_map_renderer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* 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 { render, unmountComponentAtNode } from 'react-dom'; | ||
import type { ExpressionRenderDefinition } from 'src/plugins/expressions'; | ||
import { RegionMapVisRenderValue } from './region_map_fn'; | ||
import { RegionMapVisualization } from './region_map_visualization'; | ||
|
||
export const regionMapRenderer = { | ||
name: 'region_map_vis', | ||
reuseDomNode: true, | ||
render: async (domNode, { filters, query, timeRange, visConfig }, handlers) => { | ||
handlers.onDestroy(() => { | ||
unmountComponentAtNode(domNode); | ||
}); | ||
|
||
render( | ||
<RegionMapVisualization | ||
filters={filters} | ||
query={query} | ||
timeRange={timeRange} | ||
visConfig={visConfig} | ||
/>, | ||
domNode | ||
); | ||
}, | ||
} as ExpressionRenderDefinition<RegionMapVisRenderValue>; |
44 changes: 44 additions & 0 deletions
44
x-pack/plugins/maps/public/legacy_visualizations/region_map/region_map_vis_type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { VisTypeDefinition } from '../../../../../../src/plugins/visualizations/public'; | ||
import { toExpressionAst } from './to_ast'; | ||
import { RegionMapVisParams } from './types'; | ||
import { RegionMapEditor } from './region_map_editor'; | ||
|
||
export const title = i18n.translate('xpack.maps.regionMapMap.vis.title', { | ||
defaultMessage: 'Region Map', | ||
}); | ||
|
||
export const regionMapVisType = { | ||
name: 'region_map', | ||
title, | ||
icon: 'visMapRegion', | ||
description: i18n.translate('xpack.maps.regionMap.vis.description', { | ||
defaultMessage: 'Show metrics on a thematic map. Use one of the \ | ||
provided base maps, or add your own. Darker colors represent higher values.', | ||
}), | ||
editorConfig: { | ||
optionTabs: [ | ||
{ | ||
name: '', | ||
title: '', | ||
editor: RegionMapEditor, | ||
}, | ||
], | ||
}, | ||
visConfig: { | ||
defaults: { | ||
colorSchema: 'Yellow to Red', | ||
mapZoom: 2, | ||
mapCenter: [0, 0], | ||
}, | ||
}, | ||
toExpressionAst, | ||
requiresSearch: true, | ||
} as VisTypeDefinition<RegionMapVisParams>; |
97 changes: 97 additions & 0 deletions
97
x-pack/plugins/maps/public/legacy_visualizations/region_map/region_map_visualization.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* 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, { Component, RefObject } from 'react'; | ||
import uuid from 'uuid/v4'; | ||
import { EuiLoadingChart } from '@elastic/eui'; | ||
import type { Filter, Query, TimeRange } from '../../../../../../src/plugins/data/common'; | ||
import type { Embeddable } from '../../../../../../src/plugins/embeddable/public'; | ||
import type { MapEmbeddableInput, MapEmbeddableOutput } from '../../embeddable'; | ||
import { lazyLoadMapModules } from '../../lazy_load_bundle'; | ||
import { RegionMapVisConfig } from './types'; | ||
|
||
interface Props { | ||
filters?: Filter[]; | ||
query?: Query; | ||
timeRange?: TimeRange; | ||
visConfig: RegionMapVisConfig; | ||
} | ||
|
||
interface State { | ||
isLoaded: boolean; | ||
} | ||
|
||
export class RegionMapVisualization extends Component<Props, State> { | ||
private _isMounted = false; | ||
private _mapEmbeddable?: Embeddable<MapEmbeddableInput, MapEmbeddableOutput> | undefined; | ||
private readonly _embeddableRef: RefObject<HTMLDivElement> = React.createRef<HTMLDivElement>(); | ||
|
||
state: State = { isLoaded: false }; | ||
|
||
componentDidMount() { | ||
this._isMounted = true; | ||
this._load(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this._isMounted = false; | ||
if (this._mapEmbeddable) { | ||
this._mapEmbeddable.destroy(); | ||
} | ||
} | ||
|
||
componentDidUpdate() { | ||
if (this._mapEmbeddable) { | ||
this._mapEmbeddable.updateInput({ | ||
filters: this.props.filters, | ||
query: this.props.query, | ||
timeRange: this.props.timeRange, | ||
}); | ||
} | ||
} | ||
|
||
async _load() { | ||
const mapModules = await lazyLoadMapModules(); | ||
if (!this._isMounted) { | ||
return; | ||
} | ||
|
||
this.setState({ isLoaded: true }); | ||
|
||
this._mapEmbeddable = new mapModules.MapEmbeddable( | ||
{ | ||
editable: false, | ||
}, | ||
{ | ||
id: uuid(), | ||
attributes: { | ||
title: '', | ||
layerListJSON: JSON.stringify([ | ||
mapModules.createBasemapLayerDescriptor(), | ||
mapModules.createRegionMapLayerDescriptor(this.props.visConfig.layerDescriptorParams), | ||
]), | ||
}, | ||
mapCenter: { | ||
lat: this.props.visConfig.mapCenter[0], | ||
lon: this.props.visConfig.mapCenter[1], | ||
zoom: this.props.visConfig.mapZoom, | ||
}, | ||
} | ||
); | ||
if (this._embeddableRef.current) { | ||
this._mapEmbeddable.render(this._embeddableRef.current); | ||
} | ||
} | ||
|
||
render() { | ||
if (!this.state.isLoaded) { | ||
return <EuiLoadingChart mono size="l" />; | ||
} | ||
|
||
return <div className="mapLegacyVisualizationContainer" ref={this._embeddableRef} />; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
x-pack/plugins/maps/public/legacy_visualizations/region_map/to_ast.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 { | ||
buildExpression, | ||
buildExpressionFunction, | ||
} from '../../../../../../src/plugins/expressions/public'; | ||
import { VisToExpressionAst } from '../../../../../../src/plugins/visualizations/public'; | ||
import { RegionMapExpressionFunctionDefinition } from './region_map_fn'; | ||
import { RegionMapVisParams } from './types'; | ||
import { extractLayerDescriptorParams } from './utils'; | ||
|
||
export const toExpressionAst: VisToExpressionAst<RegionMapVisParams> = (vis) => { | ||
const regionMap = buildExpressionFunction<RegionMapExpressionFunctionDefinition>('regionmap', { | ||
visConfig: JSON.stringify({ | ||
...vis.params, | ||
mapCenter: vis.uiState.get('mapCenter', [0, 0]), | ||
mapZoom: parseInt(vis.uiState.get('mapZoom', 2), 10), | ||
layerDescriptorParams: extractLayerDescriptorParams(vis), | ||
}), | ||
}); | ||
|
||
const ast = buildExpression([regionMap]); | ||
|
||
return ast.toAst(); | ||
}; |
Oops, something went wrong.