From 231fb6ddf3c616be57f72a28cb5f2047a2910dd0 Mon Sep 17 00:00:00 2001 From: Thomas Neirynck Date: Thu, 30 Apr 2020 12:08:20 -0400 Subject: [PATCH] add typing --- .../maps/public/embeddable/lazy/index.ts | 2 + .../maps/public/embeddable/map_embeddable.tsx | 29 +--------- .../embeddable/map_embeddable_factory.ts | 56 ++++++++++++------- .../plugins/maps/public/embeddable/types.ts | 38 +++++++++++++ 4 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 x-pack/plugins/maps/public/embeddable/types.ts diff --git a/x-pack/plugins/maps/public/embeddable/lazy/index.ts b/x-pack/plugins/maps/public/embeddable/lazy/index.ts index df2638d2054656..7475d56ee076f7 100644 --- a/x-pack/plugins/maps/public/embeddable/lazy/index.ts +++ b/x-pack/plugins/maps/public/embeddable/lazy/index.ts @@ -6,6 +6,8 @@ // These are map-dependencies of the embeddable. // By lazy-loading these, the Maps-app can register the embeddable when the plugin mounts, without actually pulling all the code. + +// @ts-ignore export * from '../../angular/services/gis_map_saved_object_loader'; export * from '../map_embeddable'; export * from '../../kibana_services'; diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx b/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx index 795b7a37d43d90..b1cd0f097f69fa 100644 --- a/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx +++ b/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx @@ -15,7 +15,6 @@ import { EuiLoadingSpinner } from '@elastic/eui'; import { Embeddable, IContainer, - EmbeddableInput, EmbeddableOutput, } from '../../../../../src/plugins/embeddable/public'; import { APPLY_FILTER_TRIGGER } from '../../../../../src/plugins/ui_actions/public'; @@ -43,7 +42,6 @@ import { setHiddenLayers, setMapSettings, } from '../actions/map_actions'; -import { MapCenterAndZoom } from '../../common/descriptor_types'; import { setReadOnly, setIsLayerTOCOpen, setOpenTOCDetails } from '../actions/ui_actions'; import { getIsLayerTOCOpen, getOpenTOCDetails } from '../selectors/ui_selectors'; import { @@ -56,31 +54,8 @@ import { MAP_SAVED_OBJECT_TYPE } from '../../common/constants'; import { RenderToolTipContent } from '../layers/tooltips/tooltip_property'; import { getUiActions, getCoreI18n } from '../kibana_services'; -interface MapEmbeddableConfig { - editUrl?: string; - indexPatterns: IIndexPattern[]; - editable: boolean; - title?: string; - layerList: unknown[]; - settings?: MapSettings; -} - -export interface MapEmbeddableInput extends EmbeddableInput { - timeRange?: TimeRange; - filters: Filter[]; - query?: Query; - refreshConfig: RefreshInterval; - isLayerTOCOpen: boolean; - openTOCDetails?: string[]; - disableTooltipControl?: boolean; - disableInteractive?: boolean; - hideToolbarOverlay?: boolean; - hideLayerControl?: boolean; - hideViewControl?: boolean; - mapCenter?: MapCenterAndZoom; - hiddenLayers?: string[]; - hideFilterActions?: boolean; -} +import { MapEmbeddableInput, MapEmbeddableConfig } from './types'; +export { MapEmbeddableInput, MapEmbeddableConfig }; export interface MapEmbeddableOutput extends EmbeddableOutput { indexPatterns: IIndexPattern[]; diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts index a0218de4e7d7c0..7e3a8387bed116 100644 --- a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts +++ b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts @@ -6,39 +6,54 @@ import _ from 'lodash'; import { i18n } from '@kbn/i18n'; +import { AnyAction } from 'redux'; import { IIndexPattern } from 'src/plugins/data/public'; import { + Embeddable, EmbeddableFactoryDefinition, IContainer, } from '../../../../../src/plugins/embeddable/public'; import '../index.scss'; import { createMapPath, MAP_SAVED_OBJECT_TYPE, APP_ICON } from '../../common/constants'; - -let whenModulesLoadedPromise = null; - -let getMapsSavedObjectLoader = null; -let MapEmbeddable; -let MapEmbeddableInput = null; -let getIndexPatternService; -let getHttp; -let getMapsCapabilities = null; -let createMapStore = null; -let addLayerWithoutDataSync = null; -let getQueryableUniqueIndexPatternIds = null; -let getInitialLayers = null; -let mergeInputWithSavedMap = null; - -async function waitForMapDependencies() { - if (whenModulesLoadedPromise !== null) { +import { MapStore, MapStoreState } from '../reducers/store'; +import { MapEmbeddableConfig, MapEmbeddableInput } from './types'; +import { MapEmbeddableOutput } from './map_embeddable'; +import { RenderToolTipContent } from '../layers/tooltips/tooltip_property'; +import { EventHandlers } from '../reducers/non_serializable_instances'; + +let whenModulesLoadedPromise: Promise; + +let getMapsSavedObjectLoader: any; +let MapEmbeddable: new ( + config: MapEmbeddableConfig, + initialInput: MapEmbeddableInput, + parent?: IContainer, + renderTooltipContent?: RenderToolTipContent, + eventHandlers?: EventHandlers +) => Embeddable; + +let getIndexPatternService: () => { + get: (id: string) => IIndexPattern | undefined; +}; +let getHttp: () => any; +let getMapsCapabilities: () => any; +let createMapStore: () => MapStore; +let addLayerWithoutDataSync: (layerDescriptor: unknown) => AnyAction; +let getQueryableUniqueIndexPatternIds: (state: MapStoreState) => string[]; +let getInitialLayers: (layerListJSON?: string, initialLayers?: unknown[]) => unknown[]; +let mergeInputWithSavedMap: any; + +async function waitForMapDependencies(): Promise { + if (typeof whenModulesLoadedPromise !== 'undefined') { return whenModulesLoadedPromise; } whenModulesLoadedPromise = new Promise(async resolve => { ({ + // @ts-ignore getMapsSavedObjectLoader, getQueryableUniqueIndexPatternIds, MapEmbeddable, - MapEmbeddableInput, getIndexPatternService, getHttp, getMapsCapabilities, @@ -82,7 +97,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { async _getIndexPatterns(layerList: unknown[]): Promise { // Need to extract layerList from store to get queryable index pattern ids const store = createMapStore(); - let queryableIndexPatternIds; + let queryableIndexPatternIds: string[]; try { layerList.forEach((layerDescriptor: unknown) => { store.dispatch(addLayerWithoutDataSync(layerDescriptor)); @@ -98,6 +113,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { const promises = queryableIndexPatternIds.map(async indexPatternId => { try { + // @ts-ignore return await getIndexPatternService().get(indexPatternId); } catch (error) { // Unable to load index pattern, better to not throw error so map embeddable can render @@ -116,7 +132,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { createFromSavedObject = async ( savedObjectId: string, - input: MapsEmbeddableInput, + input: MapEmbeddableInput, parent?: IContainer ) => { await waitForMapDependencies(); diff --git a/x-pack/plugins/maps/public/embeddable/types.ts b/x-pack/plugins/maps/public/embeddable/types.ts new file mode 100644 index 00000000000000..d287c431e50c29 --- /dev/null +++ b/x-pack/plugins/maps/public/embeddable/types.ts @@ -0,0 +1,38 @@ +/* + * 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 { IIndexPattern } from '../../../../../src/plugins/data/common/index_patterns'; +import { MapSettings } from '../reducers/map'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { EmbeddableInput } from '../../../../../src/plugins/embeddable/public/lib/embeddables'; +import { Filter, Query, RefreshInterval, TimeRange } from '../../../../../src/plugins/data/common'; +import { MapCenterAndZoom } from '../../common/descriptor_types'; + +export interface MapEmbeddableConfig { + editUrl?: string; + indexPatterns: IIndexPattern[]; + editable: boolean; + title?: string; + layerList: unknown[]; + settings?: MapSettings; +} + +export interface MapEmbeddableInput extends EmbeddableInput { + timeRange?: TimeRange; + filters: Filter[]; + query?: Query; + refreshConfig: RefreshInterval; + isLayerTOCOpen: boolean; + openTOCDetails?: string[]; + disableTooltipControl?: boolean; + disableInteractive?: boolean; + hideToolbarOverlay?: boolean; + hideLayerControl?: boolean; + hideViewControl?: boolean; + mapCenter?: MapCenterAndZoom; + hiddenLayers?: string[]; + hideFilterActions?: boolean; +}