kibana/x-pack/plugins/lens/public/datatable_visualization/expression.tsx
Marco Liberati 3c6b85469b
[Lens] Move Lens functions to common (#105455)
* 🚚 First move batch to common

* 🚚 Second batch of move

* 🏷️ Import types only

* 🚚 Third batch

* 🚚 Fourth batch move

* 🚚 Another module moved

* 🚚 More function moved

* 🚚 Last bit of move

*  Reduce page load bundle size

* 🐛 Fix import issue

* 🐛 More import fix

*  Registered functions on the server

* 🐛 Expose datatable_column as well

*  Add server side expression test

* 🚚 Moved back render functions to public

*  Add a timezone arg to time_scale

* 🔥 Remove timezone arg

* 🔥 Remove server side code for now

* 👌 Integrated feedback

* 🚚 Move back datatable render function

* 🏷️ Fix imports

* 🏷️ Fix missing export

* 🚚 Move render functions back!

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
2021-07-26 15:26:29 +02:00

94 lines
3 KiB
TypeScript

/*
* 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 ReactDOM from 'react-dom';
import { i18n } from '@kbn/i18n';
import { I18nProvider } from '@kbn/i18n/react';
import type { IAggType } from 'src/plugins/data/public';
import { PaletteRegistry } from 'src/plugins/charts/public';
import { IUiSettingsClient } from 'kibana/public';
import { ExpressionRenderDefinition } from 'src/plugins/expressions';
import { DatatableComponent } from './components/table_basic';
import type { ILensInterpreterRenderHandlers } from '../types';
import type { FormatFactory } from '../../common';
import { DatatableProps } from '../../common/expressions';
export { datatableColumn, getDatatable } from '../../common/expressions';
export const getDatatableRenderer = (dependencies: {
formatFactory: FormatFactory;
getType: Promise<(name: string) => IAggType>;
paletteService: PaletteRegistry;
uiSettings: IUiSettingsClient;
}): ExpressionRenderDefinition<DatatableProps> => ({
name: 'lens_datatable_renderer',
displayName: i18n.translate('xpack.lens.datatable.visualizationName', {
defaultMessage: 'Datatable',
}),
help: '',
validate: () => undefined,
reuseDomNode: true,
render: async (
domNode: Element,
config: DatatableProps,
handlers: ILensInterpreterRenderHandlers
) => {
const resolvedGetType = await dependencies.getType;
const { hasCompatibleActions } = handlers;
// An entry for each table row, whether it has any actions attached to
// ROW_CLICK_TRIGGER trigger.
let rowHasRowClickTriggerActions: boolean[] = [];
if (hasCompatibleActions) {
const table = Object.values(config.data.tables)[0];
if (!!table) {
rowHasRowClickTriggerActions = await Promise.all(
table.rows.map(async (row, rowIndex) => {
try {
const hasActions = await hasCompatibleActions({
name: 'tableRowContextMenuClick',
data: {
rowIndex,
table,
columns: config.args.columns.map((column) => column.columnId),
},
});
return hasActions;
} catch {
return false;
}
})
);
}
}
ReactDOM.render(
<I18nProvider>
<DatatableComponent
{...config}
formatFactory={dependencies.formatFactory}
dispatchEvent={handlers.event}
renderMode={handlers.getRenderMode()}
paletteService={dependencies.paletteService}
getType={resolvedGetType}
rowHasRowClickTriggerActions={rowHasRowClickTriggerActions}
uiSettings={dependencies.uiSettings}
/>
</I18nProvider>,
domNode,
() => {
handlers.done();
}
);
handlers.onDestroy(() => ReactDOM.unmountComponentAtNode(domNode));
},
});