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

Typescript map embeddable #61264

Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions x-pack/legacy/plugins/maps/public/actions/map_actions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
/* eslint-disable @typescript-eslint/consistent-type-definitions */

import { Filter, Query } from 'src/plugins/data/public';
import { AnyAction } from 'redux';
import { LAYER_TYPE } from '../../common/constants';
import { DataMeta, MapFilters } from '../../common/data_request_descriptor_types';

Expand All @@ -24,3 +26,45 @@ export function updateSourceProp(
value: unknown,
newLayerType?: LAYER_TYPE
): void;

export interface MapCenter {
nreese marked this conversation as resolved.
Show resolved Hide resolved
lat: number;
lon: number;
zoom: number;
}

export function setGotoWithCenter(config: MapCenter): AnyAction;

export function replaceLayerList(layerList: unknown[]): AnyAction;

export interface QueryGroup {
filters: Filter[];
query?: Query;
timeFilters: unknown;
refresh: unknown;
}

export function setQuery(query: QueryGroup): AnyAction;

export interface RefreshConfig {
isPaused: boolean;
interval: number;
}

export function setRefreshConfig(config: RefreshConfig): AnyAction;

export function disableScrollZoom(): AnyAction;

export function disableInteractive(): AnyAction;

export function disableTooltipControl(): AnyAction;

export function hideToolbarOverlay(): AnyAction;

export function hideLayerControl(): AnyAction;

export function hideViewControl(): AnyAction;

export function setHiddenLayers(hiddenLayerIds: string[]): AnyAction;

export function addLayerWithoutDataSync(layerDescriptor: unknown): AnyAction;
13 changes: 13 additions & 0 deletions x-pack/legacy/plugins/maps/public/actions/ui_actions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* 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.
*/

import { AnyAction } from 'redux';

export function setOpenTOCDetails(layerIds?: string[]): AnyAction;

export function setIsLayerTOCOpen(open: boolean): AnyAction;

export function setReadOnly(readOnly: boolean): AnyAction;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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 function getInitialLayers(layerListJSON?: string, initialLayers?: unknown[]): unknown[];
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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.
*/

import React from 'react';
import { Filter } from 'src/plugins/data/public';
import { RenderTooltipContentParams } from '../../embeddable/types';

export const GisMap: React.ComponentType<{
addFilters: ((filters: Filter[]) => void) | null;
renderTooltipContent?: (params: RenderTooltipContentParams) => React.ComponentType;
}>;
9 changes: 9 additions & 0 deletions x-pack/legacy/plugins/maps/public/embeddable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* 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 * from './map_embeddable';
export * from './map_embeddable_factory';
export * from './types';
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ import { Provider } from 'react-redux';
import { render, unmountComponentAtNode } from 'react-dom';
import 'mapbox-gl/dist/mapbox-gl.css';

import { Embeddable } from '../../../../../../src/legacy/core_plugins/embeddable_api/public/np_ready/public';
import { APPLY_FILTER_TRIGGER } from '../../../../../../src/plugins/ui_actions/public';
import { esFilters } from '../../../../../../src/plugins/data/public';

import { I18nContext } from 'ui/i18n';
import { npStart } from 'ui/new_platform';
import { Subscription } from 'rxjs';
import { Unsubscribe } from 'redux';
import {
Embeddable,
IContainer,
EmbeddableInput,
EmbeddableOutput,
} from '../../../../../../src/plugins/embeddable/public';
import { APPLY_FILTER_TRIGGER } from '../../../../../../src/plugins/ui_actions/public';
import {
esFilters,
IIndexPattern,
TimeRange,
Filter,
Query,
RefreshInterval,
} from '../../../../../../src/plugins/data/public';

import { GisMap } from '../connected_components/gis_map';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { createMapStore } from '../../../../../plugins/maps/public/reducers/store';
import { npStart } from 'ui/new_platform';
import { createMapStore, MapStore } from '../../../../../plugins/maps/public/reducers/store';
import {
setGotoWithCenter,
replaceLayerList,
Expand All @@ -32,6 +45,7 @@ import {
hideLayerControl,
hideViewControl,
setHiddenLayers,
MapCenter,
} from '../actions/map_actions';
import { setReadOnly, setIsLayerTOCOpen, setOpenTOCDetails } from '../actions/ui_actions';
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../selectors/ui_selectors';
Expand All @@ -42,11 +56,60 @@ import {
} from '../../../../../plugins/maps/public/reducers/non_serializable_instances';
import { getMapCenter, getMapZoom, getHiddenLayerIds } from '../selectors/map_selectors';
import { MAP_SAVED_OBJECT_TYPE } from '../../common/constants';
import { RenderTooltipContentParams } from './types';

interface MapConfig {
stacey-gammon marked this conversation as resolved.
Show resolved Hide resolved
editUrl?: string;
indexPatterns: IIndexPattern[];
editable: boolean;
title?: string;
layerList: unknown[];
}

export interface MapEmbeddableInput extends EmbeddableInput {
timeRange?: TimeRange;
stacey-gammon marked this conversation as resolved.
Show resolved Hide resolved
filters: Filter[];
query?: Query;
refresh?: unknown;
refreshConfig: RefreshInterval;
isLayerTOCOpen: boolean;
openTOCDetails?: string[];
disableTooltipControl?: boolean;
disableInteractive?: boolean;
hideToolbarOverlay?: boolean;
hideLayerControl?: boolean;
hideViewControl?: boolean;
mapCenter?: MapCenter;
hiddenLayers?: string[];
hideFilterActions?: boolean;
}

export class MapEmbeddable extends Embeddable {
export interface MapOutput extends EmbeddableOutput {
stacey-gammon marked this conversation as resolved.
Show resolved Hide resolved
indexPatterns: IIndexPattern[];
}

export class MapEmbeddable extends Embeddable<MapEmbeddableInput, MapOutput> {
type = MAP_SAVED_OBJECT_TYPE;

constructor(config, initialInput, parent, renderTooltipContent, eventHandlers) {
private _renderTooltipContent?: (params: RenderTooltipContentParams) => React.ComponentType;
private _eventHandlers?: unknown;
private _layerList: unknown[];
private _store: MapStore;
private _subscription: Subscription;
private _prevTimeRange?: TimeRange;
private _prevQuery?: Query;
private _prevRefreshConfig?: RefreshInterval;
private _prevFilters?: Filter[];
private _domNode?: HTMLElement;
private _unsubscribeFromStore?: Unsubscribe;

constructor(
config: MapConfig,
initialInput: MapEmbeddableInput,
parent?: IContainer,
renderTooltipContent?: (params: unknown) => React.ComponentType,
eventHandlers?: unknown
) {
super(
initialInput,
{
Expand All @@ -70,7 +133,7 @@ export class MapEmbeddable extends Embeddable {
return getInspectorAdapters(this._store.getState());
}

onContainerStateChanged(containerState) {
onContainerStateChanged(containerState: MapEmbeddableInput) {
if (
!_.isEqual(containerState.timeRange, this._prevTimeRange) ||
!_.isEqual(containerState.query, this._prevQuery) ||
Expand All @@ -84,7 +147,12 @@ export class MapEmbeddable extends Embeddable {
}
}

_dispatchSetQuery({ query, timeRange, filters, refresh }) {
_dispatchSetQuery({
query,
timeRange,
filters,
refresh,
}: Pick<MapEmbeddableInput, 'query' | 'timeRange' | 'filters' | 'refresh'>) {
this._prevTimeRange = timeRange;
this._prevQuery = query;
this._prevFilters = filters;
Expand All @@ -98,7 +166,7 @@ export class MapEmbeddable extends Embeddable {
);
}

_dispatchSetRefreshConfig({ refreshConfig }) {
_dispatchSetRefreshConfig({ refreshConfig }: Pick<MapEmbeddableInput, 'refreshConfig'>) {
this._prevRefreshConfig = refreshConfig;
this._store.dispatch(
setRefreshConfig({
Expand All @@ -113,7 +181,7 @@ export class MapEmbeddable extends Embeddable {
* @param {HTMLElement} domNode
* @param {ContainerState} containerState
*/
render(domNode) {
render(domNode: HTMLElement) {
this._store.dispatch(setEventHandlers(this._eventHandlers));
this._store.dispatch(setReadOnly(true));
this._store.dispatch(disableScrollZoom());
Expand All @@ -127,23 +195,22 @@ export class MapEmbeddable extends Embeddable {
}

if (_.has(this.input, 'disableInteractive') && this.input.disableInteractive) {
this._store.dispatch(disableInteractive(this.input.disableInteractive));
this._store.dispatch(disableInteractive());
}

if (_.has(this.input, 'disableTooltipControl') && this.input.disableTooltipControl) {
this._store.dispatch(disableTooltipControl(this.input.disableTooltipControl));
this._store.dispatch(disableTooltipControl());
}

if (_.has(this.input, 'hideToolbarOverlay') && this.input.hideToolbarOverlay) {
this._store.dispatch(hideToolbarOverlay(this.input.hideToolbarOverlay));
this._store.dispatch(hideToolbarOverlay());
}

if (_.has(this.input, 'hideLayerControl') && this.input.hideLayerControl) {
this._store.dispatch(hideLayerControl(this.input.hideLayerControl));
this._store.dispatch(hideLayerControl());
}

if (_.has(this.input, 'hideViewControl') && this.input.hideViewControl) {
this._store.dispatch(hideViewControl(this.input.hideViewControl));
this._store.dispatch(hideViewControl());
}

if (this.input.mapCenter) {
Expand Down Expand Up @@ -182,12 +249,12 @@ export class MapEmbeddable extends Embeddable {
});
}

async setLayerList(layerList) {
async setLayerList(layerList: unknown[]) {
this._layerList = layerList;
return await this._store.dispatch(replaceLayerList(this._layerList));
}

addFilters = filters => {
addFilters = (filters: Filter[]) => {
npStart.plugins.uiActions.executeTriggerActions(APPLY_FILTER_TRIGGER, {
embeddable: this,
filters,
Expand All @@ -213,7 +280,7 @@ export class MapEmbeddable extends Embeddable {
this._dispatchSetQuery({
query: this._prevQuery,
timeRange: this._prevTimeRange,
filters: this._prevFilters,
filters: this._prevFilters ?? [],
refresh: true,
});
}
Expand All @@ -222,7 +289,7 @@ export class MapEmbeddable extends Embeddable {
const center = getMapCenter(this._store.getState());
const zoom = getMapZoom(this._store.getState());

const mapCenter = this.input.mapCenter || {};
const mapCenter = this.input.mapCenter || undefined;
if (
!mapCenter ||
mapCenter.lat !== center.lat ||
Expand All @@ -233,7 +300,7 @@ export class MapEmbeddable extends Embeddable {
mapCenter: {
lat: center.lat,
lon: center.lon,
zoom: zoom,
zoom,
},
});
}
Expand Down
Loading