Skip to content

Commit

Permalink
add typing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasneirynck committed Apr 30, 2020
1 parent d39d2df commit 231fb6d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 47 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/maps/public/embeddable/lazy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
29 changes: 2 additions & 27 deletions x-pack/plugins/maps/public/embeddable/map_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -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[];
Expand Down
56 changes: 36 additions & 20 deletions x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>;

let getMapsSavedObjectLoader: any;
let MapEmbeddable: new (
config: MapEmbeddableConfig,
initialInput: MapEmbeddableInput,
parent?: IContainer,
renderTooltipContent?: RenderToolTipContent,
eventHandlers?: EventHandlers
) => Embeddable<MapEmbeddableInput, MapEmbeddableOutput>;

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<boolean> {
if (typeof whenModulesLoadedPromise !== 'undefined') {
return whenModulesLoadedPromise;
}

whenModulesLoadedPromise = new Promise(async resolve => {
({
// @ts-ignore
getMapsSavedObjectLoader,
getQueryableUniqueIndexPatternIds,
MapEmbeddable,
MapEmbeddableInput,
getIndexPatternService,
getHttp,
getMapsCapabilities,
Expand Down Expand Up @@ -82,7 +97,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
async _getIndexPatterns(layerList: unknown[]): Promise<IIndexPattern[]> {
// 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));
Expand All @@ -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
Expand All @@ -116,7 +132,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {

createFromSavedObject = async (
savedObjectId: string,
input: MapsEmbeddableInput,
input: MapEmbeddableInput,
parent?: IContainer
) => {
await waitForMapDependencies();
Expand Down
38 changes: 38 additions & 0 deletions x-pack/plugins/maps/public/embeddable/types.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 231fb6d

Please sign in to comment.