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.
|
|
|
|
*/
|
|
|
|
|
2020-09-29 19:00:29 +02:00
|
|
|
import './expression.scss';
|
|
|
|
|
2020-05-19 11:14:26 +02:00
|
|
|
import React, { useMemo } from 'react';
|
2019-09-17 20:57:53 +02:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import { i18n } from '@kbn/i18n';
|
2020-04-30 23:44:16 +02:00
|
|
|
import { I18nProvider } from '@kbn/i18n/react';
|
2020-12-14 13:28:23 +01:00
|
|
|
import {
|
|
|
|
EuiBasicTable,
|
|
|
|
EuiFlexGroup,
|
|
|
|
EuiButtonIcon,
|
|
|
|
EuiFlexItem,
|
|
|
|
EuiToolTip,
|
2020-12-15 12:37:59 +01:00
|
|
|
Direction,
|
|
|
|
EuiScreenReaderOnly,
|
|
|
|
EuiIcon,
|
2020-12-14 13:28:23 +01:00
|
|
|
EuiBasicTableColumn,
|
|
|
|
EuiTableActionsColumnType,
|
|
|
|
} from '@elastic/eui';
|
2021-01-11 16:51:00 +01:00
|
|
|
|
2020-04-30 23:44:16 +02:00
|
|
|
import { IAggType } from 'src/plugins/data/public';
|
2020-12-15 12:37:59 +01:00
|
|
|
import { Datatable, DatatableColumnMeta, RenderMode } from 'src/plugins/expressions';
|
2020-05-15 12:01:27 +02:00
|
|
|
import {
|
|
|
|
FormatFactory,
|
|
|
|
ILensInterpreterRenderHandlers,
|
2020-12-15 12:37:59 +01:00
|
|
|
LensEditEvent,
|
2020-05-15 12:01:27 +02:00
|
|
|
LensFilterEvent,
|
|
|
|
LensMultiTable,
|
2020-12-14 13:28:23 +01:00
|
|
|
LensTableRowContextMenuEvent,
|
2020-05-15 12:01:27 +02:00
|
|
|
} from '../types';
|
2019-10-25 22:32:59 +02:00
|
|
|
import {
|
2020-02-11 19:47:36 +01:00
|
|
|
ExpressionFunctionDefinition,
|
|
|
|
ExpressionRenderDefinition,
|
2020-04-15 12:22:37 +02:00
|
|
|
} from '../../../../../src/plugins/expressions/public';
|
2019-09-25 17:11:13 +02:00
|
|
|
import { VisualizationContainer } from '../visualization_container';
|
2020-05-19 11:14:26 +02:00
|
|
|
import { EmptyPlaceholder } from '../shared_components';
|
2020-05-29 09:38:07 +02:00
|
|
|
import { desanitizeFilterContext } from '../utils';
|
2020-09-15 11:24:02 +02:00
|
|
|
import { LensIconChartDatatable } from '../assets/chart_datatable';
|
2021-01-11 16:51:00 +01:00
|
|
|
import { getSortingCriteria } from './sorting';
|
2020-09-15 11:24:02 +02:00
|
|
|
|
2020-12-15 12:37:59 +01:00
|
|
|
export const LENS_EDIT_SORT_ACTION = 'sort';
|
|
|
|
|
|
|
|
export interface LensSortActionData {
|
|
|
|
columnId: string | undefined;
|
|
|
|
direction: 'asc' | 'desc' | 'none';
|
|
|
|
}
|
|
|
|
|
|
|
|
type LensSortAction = LensEditEvent<typeof LENS_EDIT_SORT_ACTION>;
|
|
|
|
|
|
|
|
// This is a way to circumvent the explicit "any" forbidden type
|
|
|
|
type TableRowField = Datatable['rows'][number] & { rowIndex: number };
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
export interface DatatableColumns {
|
|
|
|
columnIds: string[];
|
2020-12-15 12:37:59 +01:00
|
|
|
sortBy: string;
|
|
|
|
sortDirection: string;
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Args {
|
2020-02-11 19:47:36 +01:00
|
|
|
title: string;
|
2020-10-01 18:02:37 +02:00
|
|
|
description?: string;
|
2020-02-11 19:47:36 +01:00
|
|
|
columns: DatatableColumns & { type: 'lens_datatable_columns' };
|
2019-09-17 20:57:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface DatatableProps {
|
|
|
|
data: LensMultiTable;
|
|
|
|
args: Args;
|
|
|
|
}
|
|
|
|
|
2020-04-30 23:44:16 +02:00
|
|
|
type DatatableRenderProps = DatatableProps & {
|
|
|
|
formatFactory: FormatFactory;
|
2020-05-15 12:01:27 +02:00
|
|
|
onClickValue: (data: LensFilterEvent['data']) => void;
|
2020-12-15 12:37:59 +01:00
|
|
|
onEditAction?: (data: LensSortAction['data']) => void;
|
2020-04-30 23:44:16 +02:00
|
|
|
getType: (name: string) => IAggType;
|
2020-12-15 12:37:59 +01:00
|
|
|
renderMode: RenderMode;
|
|
|
|
onRowContextMenuClick?: (data: LensTableRowContextMenuEvent['data']) => void;
|
2020-12-14 13:28:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A boolean for each table row, which is true if the row active
|
|
|
|
* ROW_CLICK_TRIGGER actions attached to it, otherwise false.
|
|
|
|
*/
|
|
|
|
rowHasRowClickTriggerActions?: boolean[];
|
2020-04-30 23:44:16 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
export interface DatatableRender {
|
|
|
|
type: 'render';
|
|
|
|
as: 'lens_datatable_renderer';
|
|
|
|
value: DatatableProps;
|
|
|
|
}
|
|
|
|
|
2021-01-11 16:51:00 +01:00
|
|
|
function isRange(meta: { params?: { id?: string } } | undefined) {
|
|
|
|
return meta?.params?.id === 'range';
|
|
|
|
}
|
|
|
|
|
2020-12-15 12:37:59 +01:00
|
|
|
export const getDatatable = ({
|
|
|
|
formatFactory,
|
|
|
|
}: {
|
|
|
|
formatFactory: FormatFactory;
|
|
|
|
}): ExpressionFunctionDefinition<'lens_datatable', LensMultiTable, Args, DatatableRender> => ({
|
2019-09-17 20:57:53 +02:00
|
|
|
name: 'lens_datatable',
|
|
|
|
type: 'render',
|
2020-02-11 19:47:36 +01:00
|
|
|
inputTypes: ['lens_multitable'],
|
2019-09-17 20:57:53 +02:00
|
|
|
help: i18n.translate('xpack.lens.datatable.expressionHelpLabel', {
|
|
|
|
defaultMessage: 'Datatable renderer',
|
|
|
|
}),
|
|
|
|
args: {
|
|
|
|
title: {
|
|
|
|
types: ['string'],
|
|
|
|
help: i18n.translate('xpack.lens.datatable.titleLabel', {
|
|
|
|
defaultMessage: 'Title',
|
|
|
|
}),
|
|
|
|
},
|
2020-10-01 18:02:37 +02:00
|
|
|
description: {
|
|
|
|
types: ['string'],
|
|
|
|
help: '',
|
|
|
|
},
|
2019-09-17 20:57:53 +02:00
|
|
|
columns: {
|
|
|
|
types: ['lens_datatable_columns'],
|
|
|
|
help: '',
|
|
|
|
},
|
|
|
|
},
|
2020-12-15 12:37:59 +01:00
|
|
|
fn(data, args, context) {
|
|
|
|
// do the sorting at this level to propagate it also at CSV download
|
|
|
|
const [firstTable] = Object.values(data.tables);
|
|
|
|
const [layerId] = Object.keys(context.inspectorAdapters.tables || {});
|
|
|
|
const formatters: Record<string, ReturnType<FormatFactory>> = {};
|
|
|
|
|
|
|
|
firstTable.columns.forEach((column) => {
|
|
|
|
formatters[column.id] = formatFactory(column.meta?.params);
|
|
|
|
});
|
|
|
|
const { sortBy, sortDirection } = args.columns;
|
|
|
|
|
|
|
|
const columnsReverseLookup = firstTable.columns.reduce<
|
|
|
|
Record<string, { name: string; index: number; meta?: DatatableColumnMeta }>
|
|
|
|
>((memo, { id, name, meta }, i) => {
|
|
|
|
memo[id] = { name, index: i, meta };
|
|
|
|
return memo;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
if (sortBy && sortDirection !== 'none') {
|
|
|
|
// Sort on raw values for these types, while use the formatted value for the rest
|
2021-01-11 16:51:00 +01:00
|
|
|
const sortingCriteria = getSortingCriteria(
|
|
|
|
isRange(columnsReverseLookup[sortBy]?.meta)
|
|
|
|
? 'range'
|
|
|
|
: columnsReverseLookup[sortBy]?.meta?.type,
|
|
|
|
sortBy,
|
|
|
|
formatters[sortBy],
|
|
|
|
sortDirection
|
2020-12-15 12:37:59 +01:00
|
|
|
);
|
2021-01-11 16:51:00 +01:00
|
|
|
// replace the table here
|
|
|
|
context.inspectorAdapters.tables[layerId].rows = (firstTable.rows || [])
|
|
|
|
.slice()
|
|
|
|
.sort(sortingCriteria);
|
2020-12-15 12:37:59 +01:00
|
|
|
// replace also the local copy
|
|
|
|
firstTable.rows = context.inspectorAdapters.tables[layerId].rows;
|
|
|
|
}
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
|
|
|
type: 'render',
|
|
|
|
as: 'lens_datatable_renderer',
|
|
|
|
value: {
|
|
|
|
data,
|
|
|
|
args,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
2020-12-15 12:37:59 +01:00
|
|
|
});
|
2019-09-17 20:57:53 +02:00
|
|
|
|
|
|
|
type DatatableColumnsResult = DatatableColumns & { type: 'lens_datatable_columns' };
|
|
|
|
|
2020-02-11 19:47:36 +01:00
|
|
|
export const datatableColumns: ExpressionFunctionDefinition<
|
2019-09-17 20:57:53 +02:00
|
|
|
'lens_datatable_columns',
|
|
|
|
null,
|
|
|
|
DatatableColumns,
|
|
|
|
DatatableColumnsResult
|
|
|
|
> = {
|
|
|
|
name: 'lens_datatable_columns',
|
|
|
|
aliases: [],
|
|
|
|
type: 'lens_datatable_columns',
|
|
|
|
help: '',
|
2020-02-11 19:47:36 +01:00
|
|
|
inputTypes: ['null'],
|
2019-09-17 20:57:53 +02:00
|
|
|
args: {
|
2020-12-15 12:37:59 +01:00
|
|
|
sortBy: { types: ['string'], help: '' },
|
|
|
|
sortDirection: { types: ['string'], help: '' },
|
2019-09-17 20:57:53 +02:00
|
|
|
columnIds: {
|
|
|
|
types: ['string'],
|
|
|
|
multi: true,
|
|
|
|
help: '',
|
|
|
|
},
|
|
|
|
},
|
2020-02-11 19:47:36 +01:00
|
|
|
fn: function fn(input: unknown, args: DatatableColumns) {
|
2019-09-17 20:57:53 +02:00
|
|
|
return {
|
|
|
|
type: 'lens_datatable_columns',
|
|
|
|
...args,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-04-30 23:44:16 +02:00
|
|
|
export const getDatatableRenderer = (dependencies: {
|
2020-12-15 12:37:59 +01:00
|
|
|
formatFactory: FormatFactory;
|
2020-04-30 23:44:16 +02:00
|
|
|
getType: Promise<(name: string) => IAggType>;
|
|
|
|
}): ExpressionRenderDefinition<DatatableProps> => ({
|
2019-09-17 20:57:53 +02:00
|
|
|
name: 'lens_datatable_renderer',
|
|
|
|
displayName: i18n.translate('xpack.lens.datatable.visualizationName', {
|
|
|
|
defaultMessage: 'Datatable',
|
|
|
|
}),
|
|
|
|
help: '',
|
2020-02-11 19:47:36 +01:00
|
|
|
validate: () => undefined,
|
2019-09-17 20:57:53 +02:00
|
|
|
reuseDomNode: true,
|
2019-10-25 22:32:59 +02:00
|
|
|
render: async (
|
|
|
|
domNode: Element,
|
|
|
|
config: DatatableProps,
|
2020-05-15 12:01:27 +02:00
|
|
|
handlers: ILensInterpreterRenderHandlers
|
2019-10-25 22:32:59 +02:00
|
|
|
) => {
|
2020-04-30 23:44:16 +02:00
|
|
|
const resolvedGetType = await dependencies.getType;
|
2020-05-15 12:01:27 +02:00
|
|
|
const onClickValue = (data: LensFilterEvent['data']) => {
|
|
|
|
handlers.event({ name: 'filter', data });
|
|
|
|
};
|
2020-12-15 12:37:59 +01:00
|
|
|
|
|
|
|
const onEditAction = (data: LensSortAction['data']) => {
|
|
|
|
if (handlers.getRenderMode() === 'edit') {
|
|
|
|
handlers.event({ name: 'edit', data });
|
|
|
|
}
|
|
|
|
};
|
2020-12-14 13:28:23 +01:00
|
|
|
const onRowContextMenuClick = (data: LensTableRowContextMenuEvent['data']) => {
|
|
|
|
handlers.event({ name: 'tableRowContextMenuClick', data });
|
|
|
|
};
|
|
|
|
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.columnIds,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return hasActions;
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 22:32:59 +02:00
|
|
|
ReactDOM.render(
|
2020-04-30 23:44:16 +02:00
|
|
|
<I18nProvider>
|
|
|
|
<DatatableComponent
|
|
|
|
{...config}
|
2020-12-15 12:37:59 +01:00
|
|
|
formatFactory={dependencies.formatFactory}
|
2020-05-15 12:01:27 +02:00
|
|
|
onClickValue={onClickValue}
|
2020-12-15 12:37:59 +01:00
|
|
|
onEditAction={onEditAction}
|
|
|
|
renderMode={handlers.getRenderMode()}
|
2020-12-14 13:28:23 +01:00
|
|
|
onRowContextMenuClick={onRowContextMenuClick}
|
2020-04-30 23:44:16 +02:00
|
|
|
getType={resolvedGetType}
|
2020-12-14 13:28:23 +01:00
|
|
|
rowHasRowClickTriggerActions={rowHasRowClickTriggerActions}
|
2020-04-30 23:44:16 +02:00
|
|
|
/>
|
|
|
|
</I18nProvider>,
|
2019-10-25 22:32:59 +02:00
|
|
|
domNode,
|
|
|
|
() => {
|
|
|
|
handlers.done();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
handlers.onDestroy(() => ReactDOM.unmountComponentAtNode(domNode));
|
2019-09-17 20:57:53 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-15 12:37:59 +01:00
|
|
|
function getNextOrderValue(currentValue: LensSortAction['data']['direction']) {
|
|
|
|
const states: Array<LensSortAction['data']['direction']> = ['asc', 'desc', 'none'];
|
|
|
|
const newStateIndex = (1 + states.findIndex((state) => state === currentValue)) % states.length;
|
|
|
|
return states[newStateIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDirectionLongLabel(sortDirection: LensSortAction['data']['direction']) {
|
|
|
|
if (sortDirection === 'none') {
|
|
|
|
return sortDirection;
|
|
|
|
}
|
|
|
|
return sortDirection === 'asc' ? 'ascending' : 'descending';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeaderSortingCell(
|
|
|
|
name: string,
|
|
|
|
columnId: string,
|
|
|
|
sorting: Omit<LensSortAction['data'], 'action'>,
|
|
|
|
sortingLabel: string
|
|
|
|
) {
|
|
|
|
if (columnId !== sorting.columnId || sorting.direction === 'none') {
|
|
|
|
return name || '';
|
|
|
|
}
|
|
|
|
// This is a workaround to hijack the title value of the header cell
|
|
|
|
return (
|
|
|
|
<span aria-sort={getDirectionLongLabel(sorting.direction)}>
|
|
|
|
{name || ''}
|
|
|
|
<EuiScreenReaderOnly>
|
|
|
|
<span>{sortingLabel}</span>
|
|
|
|
</EuiScreenReaderOnly>
|
|
|
|
<EuiIcon
|
|
|
|
className="euiTableSortIcon"
|
|
|
|
type={sorting.direction === 'asc' ? 'sortUp' : 'sortDown'}
|
|
|
|
size="m"
|
|
|
|
aria-label={sortingLabel}
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-30 23:44:16 +02:00
|
|
|
export function DatatableComponent(props: DatatableRenderProps) {
|
2019-09-17 20:57:53 +02:00
|
|
|
const [firstTable] = Object.values(props.data.tables);
|
|
|
|
const formatters: Record<string, ReturnType<FormatFactory>> = {};
|
|
|
|
|
2020-05-22 09:08:58 +02:00
|
|
|
firstTable.columns.forEach((column) => {
|
2020-10-16 18:46:51 +02:00
|
|
|
formatters[column.id] = props.formatFactory(column.meta?.params);
|
2019-09-17 20:57:53 +02:00
|
|
|
});
|
|
|
|
|
2020-12-15 12:37:59 +01:00
|
|
|
const { onClickValue, onEditAction, onRowContextMenuClick } = props;
|
2020-05-19 11:14:26 +02:00
|
|
|
const handleFilterClick = useMemo(
|
|
|
|
() => (field: string, value: unknown, colIndex: number, negate: boolean = false) => {
|
|
|
|
const col = firstTable.columns[colIndex];
|
2020-10-16 18:46:51 +02:00
|
|
|
const isDate = col.meta?.type === 'date';
|
|
|
|
const timeFieldName = negate && isDate ? undefined : col?.meta?.field;
|
2020-05-22 09:08:58 +02:00
|
|
|
const rowIndex = firstTable.rows.findIndex((row) => row[field] === value);
|
2020-05-19 11:14:26 +02:00
|
|
|
|
|
|
|
const data: LensFilterEvent['data'] = {
|
|
|
|
negate,
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
row: rowIndex,
|
|
|
|
column: colIndex,
|
|
|
|
value,
|
|
|
|
table: firstTable,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
timeFieldName,
|
|
|
|
};
|
2020-08-06 10:10:09 +02:00
|
|
|
onClickValue(desanitizeFilterContext(data));
|
2020-05-19 11:14:26 +02:00
|
|
|
},
|
2020-08-06 10:10:09 +02:00
|
|
|
[firstTable, onClickValue]
|
2020-05-19 11:14:26 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const bucketColumns = firstTable.columns
|
2020-05-22 09:08:58 +02:00
|
|
|
.filter((col) => {
|
2020-10-16 18:46:51 +02:00
|
|
|
return (
|
|
|
|
col?.meta?.sourceParams?.type &&
|
|
|
|
props.getType(col.meta.sourceParams.type as string)?.type === 'buckets'
|
|
|
|
);
|
2020-05-19 11:14:26 +02:00
|
|
|
})
|
2020-05-22 09:08:58 +02:00
|
|
|
.map((col) => col.id);
|
2020-05-19 11:14:26 +02:00
|
|
|
|
|
|
|
const isEmpty =
|
|
|
|
firstTable.rows.length === 0 ||
|
|
|
|
(bucketColumns.length &&
|
2020-05-22 09:08:58 +02:00
|
|
|
firstTable.rows.every((row) =>
|
|
|
|
bucketColumns.every((col) => typeof row[col] === 'undefined')
|
|
|
|
));
|
2020-05-19 11:14:26 +02:00
|
|
|
|
|
|
|
if (isEmpty) {
|
2020-09-15 11:24:02 +02:00
|
|
|
return <EmptyPlaceholder icon={LensIconChartDatatable} />;
|
2020-05-19 11:14:26 +02:00
|
|
|
}
|
2020-04-30 23:44:16 +02:00
|
|
|
|
2020-12-15 12:37:59 +01:00
|
|
|
const visibleColumns = props.args.columns.columnIds.filter((field) => !!field);
|
|
|
|
const columnsReverseLookup = firstTable.columns.reduce<
|
|
|
|
Record<string, { name: string; index: number; meta?: DatatableColumnMeta }>
|
|
|
|
>((memo, { id, name, meta }, i) => {
|
|
|
|
memo[id] = { name, index: i, meta };
|
|
|
|
return memo;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const { sortBy, sortDirection } = props.args.columns;
|
|
|
|
|
|
|
|
const sortedRows: TableRowField[] =
|
|
|
|
firstTable?.rows.map((row, rowIndex) => ({ ...row, rowIndex })) || [];
|
|
|
|
const isReadOnlySorted = props.renderMode !== 'edit';
|
|
|
|
|
|
|
|
const sortedInLabel = i18n.translate('xpack.lens.datatableSortedInReadOnlyMode', {
|
|
|
|
defaultMessage: 'Sorted in {sortValue} order',
|
|
|
|
values: {
|
|
|
|
sortValue: sortDirection === 'asc' ? 'ascending' : 'descending',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const tableColumns: Array<EuiBasicTableColumn<TableRowField>> = visibleColumns.map((field) => {
|
|
|
|
const filterable = bucketColumns.includes(field);
|
|
|
|
const { name, index: colIndex, meta } = columnsReverseLookup[field];
|
|
|
|
const fieldName = meta?.field;
|
|
|
|
const nameContent = !isReadOnlySorted
|
|
|
|
? name
|
|
|
|
: getHeaderSortingCell(
|
|
|
|
name,
|
|
|
|
field,
|
|
|
|
{
|
|
|
|
columnId: sortBy,
|
|
|
|
direction: sortDirection as LensSortAction['data']['direction'],
|
|
|
|
},
|
|
|
|
sortedInLabel
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
field,
|
|
|
|
name: nameContent,
|
|
|
|
sortable: !isReadOnlySorted,
|
|
|
|
render: (value: unknown) => {
|
|
|
|
const formattedValue = formatters[field]?.convert(value);
|
|
|
|
|
|
|
|
if (filterable) {
|
|
|
|
return (
|
|
|
|
<EuiFlexGroup
|
|
|
|
className="lnsDataTable__cell"
|
|
|
|
data-test-subj="lnsDataTableCellValueFilterable"
|
|
|
|
gutterSize="xs"
|
|
|
|
>
|
|
|
|
<EuiFlexItem grow={false}>{formattedValue}</EuiFlexItem>
|
|
|
|
<EuiFlexItem grow={false}>
|
|
|
|
<EuiFlexGroup
|
|
|
|
responsive={false}
|
|
|
|
gutterSize="none"
|
|
|
|
alignItems="center"
|
|
|
|
className="lnsDataTable__filter"
|
|
|
|
>
|
|
|
|
<EuiToolTip
|
|
|
|
position="bottom"
|
|
|
|
content={i18n.translate('xpack.lens.includeValueButtonTooltip', {
|
|
|
|
defaultMessage: 'Include value',
|
|
|
|
})}
|
2020-12-14 13:28:23 +01:00
|
|
|
>
|
2020-12-15 12:37:59 +01:00
|
|
|
<EuiButtonIcon
|
|
|
|
iconType="plusInCircle"
|
|
|
|
color="text"
|
|
|
|
aria-label={i18n.translate('xpack.lens.includeValueButtonAriaLabel', {
|
|
|
|
defaultMessage: `Include {value}`,
|
|
|
|
values: {
|
|
|
|
value: `${fieldName ? `${fieldName}: ` : ''}${formattedValue}`,
|
|
|
|
},
|
|
|
|
})}
|
|
|
|
data-test-subj="lensDatatableFilterFor"
|
|
|
|
onClick={() => handleFilterClick(field, value, colIndex)}
|
|
|
|
/>
|
|
|
|
</EuiToolTip>
|
|
|
|
<EuiFlexItem grow={false}>
|
2020-12-14 13:28:23 +01:00
|
|
|
<EuiToolTip
|
|
|
|
position="bottom"
|
2020-12-15 12:37:59 +01:00
|
|
|
content={i18n.translate('xpack.lens.excludeValueButtonTooltip', {
|
|
|
|
defaultMessage: 'Exclude value',
|
2020-12-14 13:28:23 +01:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<EuiButtonIcon
|
2020-12-15 12:37:59 +01:00
|
|
|
iconType="minusInCircle"
|
2020-12-14 13:28:23 +01:00
|
|
|
color="text"
|
2020-12-15 12:37:59 +01:00
|
|
|
aria-label={i18n.translate('xpack.lens.excludeValueButtonAriaLabel', {
|
|
|
|
defaultMessage: `Exclude {value}`,
|
2020-12-14 13:28:23 +01:00
|
|
|
values: {
|
|
|
|
value: `${fieldName ? `${fieldName}: ` : ''}${formattedValue}`,
|
|
|
|
},
|
|
|
|
})}
|
2020-12-15 12:37:59 +01:00
|
|
|
data-test-subj="lensDatatableFilterOut"
|
|
|
|
onClick={() => handleFilterClick(field, value, colIndex, true)}
|
2020-12-14 13:28:23 +01:00
|
|
|
/>
|
|
|
|
</EuiToolTip>
|
2020-12-15 12:37:59 +01:00
|
|
|
</EuiFlexItem>
|
|
|
|
</EuiFlexGroup>
|
|
|
|
</EuiFlexItem>
|
|
|
|
</EuiFlexGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <span data-test-subj="lnsDataTableCellValue">{formattedValue}</span>;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2020-12-14 13:28:23 +01:00
|
|
|
|
|
|
|
if (!!props.rowHasRowClickTriggerActions && !!onRowContextMenuClick) {
|
|
|
|
const hasAtLeastOneRowClickAction = props.rowHasRowClickTriggerActions.find((x) => x);
|
|
|
|
if (hasAtLeastOneRowClickAction) {
|
2020-12-15 12:37:59 +01:00
|
|
|
const actions: EuiTableActionsColumnType<TableRowField> = {
|
2020-12-14 13:28:23 +01:00
|
|
|
name: i18n.translate('xpack.lens.datatable.actionsColumnName', {
|
|
|
|
defaultMessage: 'Actions',
|
|
|
|
}),
|
|
|
|
actions: [
|
|
|
|
{
|
|
|
|
name: i18n.translate('xpack.lens.tableRowMore', {
|
|
|
|
defaultMessage: 'More',
|
|
|
|
}),
|
|
|
|
description: i18n.translate('xpack.lens.tableRowMoreDescription', {
|
|
|
|
defaultMessage: 'Table row context menu',
|
|
|
|
}),
|
|
|
|
type: 'icon',
|
|
|
|
icon: ({ rowIndex }: { rowIndex: number }) => {
|
|
|
|
if (
|
|
|
|
!!props.rowHasRowClickTriggerActions &&
|
|
|
|
!props.rowHasRowClickTriggerActions[rowIndex]
|
|
|
|
)
|
|
|
|
return 'empty';
|
|
|
|
return 'boxesVertical';
|
|
|
|
},
|
|
|
|
onClick: ({ rowIndex }) => {
|
|
|
|
onRowContextMenuClick({
|
|
|
|
rowIndex,
|
|
|
|
table: firstTable,
|
|
|
|
columns: props.args.columns.columnIds,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
tableColumns.push(actions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 20:57:53 +02:00
|
|
|
return (
|
2020-10-01 18:02:37 +02:00
|
|
|
<VisualizationContainer
|
|
|
|
reportTitle={props.args.title}
|
|
|
|
reportDescription={props.args.description}
|
|
|
|
>
|
2019-09-25 17:11:13 +02:00
|
|
|
<EuiBasicTable
|
|
|
|
className="lnsDataTable"
|
|
|
|
data-test-subj="lnsDataTable"
|
2020-04-17 23:03:33 +02:00
|
|
|
tableLayout="auto"
|
2020-12-15 12:37:59 +01:00
|
|
|
sorting={{
|
|
|
|
sort:
|
|
|
|
!sortBy || sortDirection === 'none' || isReadOnlySorted
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
field: sortBy,
|
|
|
|
direction: sortDirection as Direction,
|
|
|
|
},
|
|
|
|
allowNeutralSort: true, // this flag enables the 3rd Neutral state on the column header
|
|
|
|
}}
|
|
|
|
onChange={(event: { sort?: { field: string } }) => {
|
|
|
|
if (event.sort && onEditAction) {
|
|
|
|
const isNewColumn = sortBy !== event.sort.field;
|
|
|
|
// unfortunately the neutral state is not propagated and we need to manually handle it
|
|
|
|
const nextDirection = getNextOrderValue(
|
|
|
|
(isNewColumn ? 'none' : sortDirection) as LensSortAction['data']['direction']
|
|
|
|
);
|
|
|
|
return onEditAction({
|
|
|
|
action: 'sort',
|
|
|
|
columnId: nextDirection !== 'none' || isNewColumn ? event.sort.field : undefined,
|
|
|
|
direction: nextDirection,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
2020-12-14 13:28:23 +01:00
|
|
|
columns={tableColumns}
|
2020-12-15 12:37:59 +01:00
|
|
|
items={sortedRows}
|
2019-09-25 17:11:13 +02:00
|
|
|
/>
|
|
|
|
</VisualizationContainer>
|
2019-09-17 20:57:53 +02:00
|
|
|
);
|
|
|
|
}
|