2019-09-17 20:57:53 +02:00
|
|
|
/*
|
|
|
|
* 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 { render, unmountComponentAtNode } from 'react-dom';
|
|
|
|
import { I18nProvider } from '@kbn/i18n/react';
|
2020-04-15 12:22:37 +02:00
|
|
|
import { CoreSetup, CoreStart } from 'kibana/public';
|
|
|
|
import { ExpressionsSetup, ExpressionsStart } from '../../../../../src/plugins/expressions/public';
|
|
|
|
import { EmbeddableSetup, EmbeddableStart } from '../../../../../src/plugins/embeddable/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import {
|
2019-12-09 12:31:49 +01:00
|
|
|
DataPublicPluginSetup,
|
|
|
|
DataPublicPluginStart,
|
2020-04-15 12:22:37 +02:00
|
|
|
} from '../../../../../src/plugins/data/public';
|
2019-09-17 20:57:53 +02:00
|
|
|
import {
|
|
|
|
Datasource,
|
|
|
|
Visualization,
|
|
|
|
EditorFrameSetup,
|
|
|
|
EditorFrameInstance,
|
|
|
|
EditorFrameStart,
|
|
|
|
} from '../types';
|
|
|
|
import { EditorFrame } from './editor_frame';
|
|
|
|
import { mergeTables } from './merge_tables';
|
2020-02-28 19:08:37 +01:00
|
|
|
import { formatColumn } from './format_column';
|
2019-09-17 20:57:53 +02:00
|
|
|
import { EmbeddableFactory } from './embeddable/embeddable_factory';
|
|
|
|
import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management';
|
|
|
|
|
|
|
|
export interface EditorFrameSetupPlugins {
|
2019-12-09 12:31:49 +01:00
|
|
|
data: DataPublicPluginSetup;
|
2020-04-15 12:22:37 +02:00
|
|
|
embeddable?: EmbeddableSetup;
|
2019-11-19 01:26:13 +01:00
|
|
|
expressions: ExpressionsSetup;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface EditorFrameStartPlugins {
|
2019-12-09 12:31:49 +01:00
|
|
|
data: DataPublicPluginStart;
|
2020-04-15 12:22:37 +02:00
|
|
|
embeddable?: EmbeddableStart;
|
2019-11-19 01:26:13 +01:00
|
|
|
expressions: ExpressionsStart;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 13:54:51 +01:00
|
|
|
async function collectAsyncDefinitions<T extends { id: string }>(
|
|
|
|
definitions: Array<T | Promise<T>>
|
|
|
|
) {
|
|
|
|
const resolvedDefinitions = await Promise.all(definitions);
|
|
|
|
const definitionMap: Record<string, T> = {};
|
|
|
|
resolvedDefinitions.forEach(definition => {
|
|
|
|
definitionMap[definition.id] = definition;
|
|
|
|
});
|
|
|
|
|
|
|
|
return definitionMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorFrameService {
|
2019-09-17 20:57:53 +02:00
|
|
|
constructor() {}
|
|
|
|
|
2020-02-13 13:54:51 +01:00
|
|
|
private readonly datasources: Array<Datasource | Promise<Datasource>> = [];
|
|
|
|
private readonly visualizations: Array<Visualization | Promise<Visualization>> = [];
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-03-16 20:37:42 +01:00
|
|
|
public setup(
|
|
|
|
core: CoreSetup<EditorFrameStartPlugins>,
|
|
|
|
plugins: EditorFrameSetupPlugins
|
|
|
|
): EditorFrameSetup {
|
2019-09-17 20:57:53 +02:00
|
|
|
plugins.expressions.registerFunction(() => mergeTables);
|
2020-02-28 19:08:37 +01:00
|
|
|
plugins.expressions.registerFunction(() => formatColumn);
|
2019-09-17 20:57:53 +02:00
|
|
|
|
2020-03-16 20:37:42 +01:00
|
|
|
const getStartServices = async () => {
|
|
|
|
const [coreStart, deps] = await core.getStartServices();
|
|
|
|
return {
|
|
|
|
capabilities: coreStart.application.capabilities,
|
|
|
|
savedObjectsClient: coreStart.savedObjects.client,
|
|
|
|
coreHttp: coreStart.http,
|
|
|
|
timefilter: deps.data.query.timefilter.timefilter,
|
|
|
|
expressionRenderer: deps.expressions.ReactExpressionRenderer,
|
|
|
|
indexPatternService: deps.data.indexPatterns,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-15 12:22:37 +02:00
|
|
|
if (plugins.embeddable) {
|
|
|
|
plugins.embeddable.registerEmbeddableFactory('lens', new EmbeddableFactory(getStartServices));
|
|
|
|
}
|
2020-03-16 20:37:42 +01:00
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
2019-12-04 14:36:21 +01:00
|
|
|
registerDatasource: datasource => {
|
2020-02-13 13:54:51 +01:00
|
|
|
this.datasources.push(datasource as Datasource<unknown, unknown>);
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
registerVisualization: visualization => {
|
2020-02-13 13:54:51 +01:00
|
|
|
this.visualizations.push(visualization as Visualization<unknown, unknown>);
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public start(core: CoreStart, plugins: EditorFrameStartPlugins): EditorFrameStart {
|
2020-02-13 13:54:51 +01:00
|
|
|
const createInstance = async (): Promise<EditorFrameInstance> => {
|
2019-09-17 20:57:53 +02:00
|
|
|
let domElement: Element;
|
2020-02-13 13:54:51 +01:00
|
|
|
const [resolvedDatasources, resolvedVisualizations] = await Promise.all([
|
|
|
|
collectAsyncDefinitions(this.datasources),
|
|
|
|
collectAsyncDefinitions(this.visualizations),
|
|
|
|
]);
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
2019-10-03 17:24:45 +02:00
|
|
|
mount: (element, { doc, onError, dateRange, query, filters, savedQuery, onChange }) => {
|
2019-09-17 20:57:53 +02:00
|
|
|
domElement = element;
|
2020-02-13 13:54:51 +01:00
|
|
|
const firstDatasourceId = Object.keys(resolvedDatasources)[0];
|
|
|
|
const firstVisualizationId = Object.keys(resolvedVisualizations)[0];
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
render(
|
|
|
|
<I18nProvider>
|
|
|
|
<EditorFrame
|
|
|
|
data-test-subj="lnsEditorFrame"
|
|
|
|
onError={onError}
|
2020-02-13 13:54:51 +01:00
|
|
|
datasourceMap={resolvedDatasources}
|
|
|
|
visualizationMap={resolvedVisualizations}
|
2019-09-17 20:57:53 +02:00
|
|
|
initialDatasourceId={getActiveDatasourceIdFromDoc(doc) || firstDatasourceId || null}
|
|
|
|
initialVisualizationId={
|
|
|
|
(doc && doc.visualizationType) || firstVisualizationId || null
|
|
|
|
}
|
|
|
|
core={core}
|
2020-02-11 19:47:36 +01:00
|
|
|
ExpressionRenderer={plugins.expressions.ReactExpressionRenderer}
|
2019-09-17 20:57:53 +02:00
|
|
|
doc={doc}
|
|
|
|
dateRange={dateRange}
|
|
|
|
query={query}
|
2019-10-03 17:24:45 +02:00
|
|
|
filters={filters}
|
|
|
|
savedQuery={savedQuery}
|
2019-09-17 20:57:53 +02:00
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
</I18nProvider>,
|
|
|
|
domElement
|
|
|
|
);
|
|
|
|
},
|
|
|
|
unmount() {
|
|
|
|
if (domElement) {
|
|
|
|
unmountComponentAtNode(domElement);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
createInstance,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|