2021-09-22 08:29:07 -05:00
|
|
|
import {
|
|
|
|
FieldOverrideContext,
|
|
|
|
FieldType,
|
|
|
|
getFieldDisplayName,
|
|
|
|
PanelPlugin,
|
|
|
|
ReducerID,
|
|
|
|
standardEditorsRegistry,
|
2023-01-12 05:42:57 -06:00
|
|
|
identityOverrideProcessor,
|
2021-09-22 08:29:07 -05:00
|
|
|
} from '@grafana/data';
|
2023-01-12 05:42:57 -06:00
|
|
|
import { TableFieldOptions, TableCellOptions, TableCellDisplayMode } from '@grafana/schema';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
2022-03-31 02:36:04 -05:00
|
|
|
import { PaginationEditor } from './PaginationEditor';
|
2023-01-12 05:42:57 -06:00
|
|
|
import { TableCellOptionEditor } from './TableCellOptionEditor';
|
2022-04-22 08:33:13 -05:00
|
|
|
import { TablePanel } from './TablePanel';
|
|
|
|
import { tableMigrationHandler, tablePanelChangedHandler } from './migrations';
|
|
|
|
import { PanelOptions, defaultPanelOptions, defaultPanelFieldConfig } from './models.gen';
|
|
|
|
import { TableSuggestionsSupplier } from './suggestions';
|
2019-03-05 18:07:46 -06:00
|
|
|
|
2022-06-01 06:11:19 -05:00
|
|
|
const footerCategory = 'Table footer';
|
2023-01-12 05:42:57 -06:00
|
|
|
const cellCategory = ['Cell Options'];
|
2022-06-01 06:11:19 -05:00
|
|
|
|
2021-11-09 10:10:21 -06:00
|
|
|
export const plugin = new PanelPlugin<PanelOptions, TableFieldOptions>(TablePanel)
|
2020-04-14 14:14:06 -05:00
|
|
|
.setPanelChangeHandler(tablePanelChangedHandler)
|
|
|
|
.setMigrationHandler(tableMigrationHandler)
|
2020-04-06 09:24:41 -05:00
|
|
|
.useFieldConfig({
|
2021-01-20 00:59:48 -06:00
|
|
|
useCustomConfig: (builder) => {
|
2020-04-06 09:24:41 -05:00
|
|
|
builder
|
2021-07-12 02:46:23 -05:00
|
|
|
.addNumberInput({
|
|
|
|
path: 'minWidth',
|
|
|
|
name: 'Minimum column width',
|
|
|
|
description: 'The minimum width for column auto resizing',
|
|
|
|
settings: {
|
|
|
|
placeholder: '150',
|
|
|
|
min: 50,
|
|
|
|
max: 500,
|
|
|
|
},
|
|
|
|
shouldApply: () => true,
|
|
|
|
defaultValue: defaultPanelFieldConfig.minWidth,
|
|
|
|
})
|
2020-04-06 09:24:41 -05:00
|
|
|
.addNumberInput({
|
|
|
|
path: 'width',
|
|
|
|
name: 'Column width',
|
|
|
|
settings: {
|
|
|
|
placeholder: 'auto',
|
|
|
|
min: 20,
|
|
|
|
max: 300,
|
|
|
|
},
|
2020-04-20 02:27:40 -05:00
|
|
|
shouldApply: () => true,
|
2021-04-08 03:11:11 -05:00
|
|
|
defaultValue: defaultPanelFieldConfig.width,
|
2020-04-06 09:24:41 -05:00
|
|
|
})
|
2020-04-07 07:02:18 -05:00
|
|
|
.addRadio({
|
|
|
|
path: 'align',
|
|
|
|
name: 'Column alignment',
|
|
|
|
settings: {
|
|
|
|
options: [
|
2021-05-03 08:45:48 -05:00
|
|
|
{ label: 'auto', value: 'auto' },
|
2020-04-07 07:02:18 -05:00
|
|
|
{ label: 'left', value: 'left' },
|
|
|
|
{ label: 'center', value: 'center' },
|
|
|
|
{ label: 'right', value: 'right' },
|
|
|
|
],
|
|
|
|
},
|
2021-04-08 03:11:11 -05:00
|
|
|
defaultValue: defaultPanelFieldConfig.align,
|
2020-04-07 07:02:18 -05:00
|
|
|
})
|
2023-01-12 05:42:57 -06:00
|
|
|
.addCustomEditor<void, TableCellOptions>({
|
|
|
|
id: 'cellOptions',
|
|
|
|
path: 'cellOptions',
|
|
|
|
name: 'Cell Type',
|
|
|
|
editor: TableCellOptionEditor,
|
|
|
|
override: TableCellOptionEditor,
|
|
|
|
defaultValue: defaultPanelFieldConfig.cellOptions,
|
|
|
|
process: identityOverrideProcessor,
|
|
|
|
category: cellCategory,
|
|
|
|
shouldApply: () => true,
|
2020-09-01 10:06:35 -05:00
|
|
|
})
|
2022-02-28 08:35:05 -06:00
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'inspect',
|
|
|
|
name: 'Cell value inspect',
|
|
|
|
description: 'Enable cell value inspection in a modal window',
|
|
|
|
defaultValue: false,
|
2023-01-12 05:42:57 -06:00
|
|
|
category: cellCategory,
|
2022-02-28 08:35:05 -06:00
|
|
|
showIf: (cfg) => {
|
|
|
|
return (
|
2023-01-12 05:42:57 -06:00
|
|
|
cfg.cellOptions.type === TableCellDisplayMode.Auto ||
|
|
|
|
cfg.cellOptions.type === TableCellDisplayMode.JSONView ||
|
|
|
|
cfg.cellOptions.type === TableCellDisplayMode.ColorText ||
|
|
|
|
cfg.cellOptions.type === TableCellDisplayMode.ColorBackground
|
2022-02-28 08:35:05 -06:00
|
|
|
);
|
|
|
|
},
|
|
|
|
})
|
2020-09-01 10:06:35 -05:00
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'filterable',
|
|
|
|
name: 'Column filter',
|
|
|
|
description: 'Enables/disables field filters in table',
|
2021-04-08 03:11:11 -05:00
|
|
|
defaultValue: defaultPanelFieldConfig.filterable,
|
2021-11-09 10:10:21 -06:00
|
|
|
})
|
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'hidden',
|
|
|
|
name: 'Hide in table',
|
|
|
|
defaultValue: undefined,
|
|
|
|
hideFromDefaults: true,
|
2020-04-06 09:24:41 -05:00
|
|
|
});
|
|
|
|
},
|
2020-03-26 15:48:46 -05:00
|
|
|
})
|
2021-01-20 00:59:48 -06:00
|
|
|
.setPanelOptions((builder) => {
|
2021-09-22 08:29:07 -05:00
|
|
|
builder
|
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'showHeader',
|
2022-06-01 06:11:19 -05:00
|
|
|
name: 'Show table header',
|
2021-09-22 08:29:07 -05:00
|
|
|
defaultValue: defaultPanelOptions.showHeader,
|
|
|
|
})
|
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'footer.show',
|
2022-06-01 06:11:19 -05:00
|
|
|
category: [footerCategory],
|
|
|
|
name: 'Show table footer',
|
2021-09-22 08:29:07 -05:00
|
|
|
defaultValue: defaultPanelOptions.footer?.show,
|
|
|
|
})
|
|
|
|
.addCustomEditor({
|
|
|
|
id: 'footer.reducer',
|
2022-06-01 06:11:19 -05:00
|
|
|
category: [footerCategory],
|
2021-09-22 08:29:07 -05:00
|
|
|
path: 'footer.reducer',
|
|
|
|
name: 'Calculation',
|
|
|
|
description: 'Choose a reducer function / calculation',
|
|
|
|
editor: standardEditorsRegistry.get('stats-picker').editor as any,
|
|
|
|
defaultValue: [ReducerID.sum],
|
|
|
|
showIf: (cfg) => cfg.footer?.show,
|
|
|
|
})
|
2022-11-28 02:16:35 -06:00
|
|
|
.addBooleanSwitch({
|
|
|
|
path: 'footer.countRows',
|
|
|
|
category: [footerCategory],
|
|
|
|
name: 'Count rows',
|
|
|
|
description: 'Display a single count for all data rows',
|
|
|
|
defaultValue: defaultPanelOptions.footer?.countRows,
|
|
|
|
showIf: (cfg) => cfg.footer?.reducer?.length === 1 && cfg.footer?.reducer[0] === ReducerID.count,
|
|
|
|
})
|
2021-09-22 08:29:07 -05:00
|
|
|
.addMultiSelect({
|
|
|
|
path: 'footer.fields',
|
2022-06-01 06:11:19 -05:00
|
|
|
category: [footerCategory],
|
2021-09-22 08:29:07 -05:00
|
|
|
name: 'Fields',
|
|
|
|
description: 'Select the fields that should be calculated',
|
|
|
|
settings: {
|
|
|
|
allowCustomValue: false,
|
|
|
|
options: [],
|
|
|
|
placeholder: 'All Numeric Fields',
|
|
|
|
getOptions: async (context: FieldOverrideContext) => {
|
|
|
|
const options = [];
|
|
|
|
if (context && context.data && context.data.length > 0) {
|
|
|
|
const frame = context.data[0];
|
|
|
|
for (const field of frame.fields) {
|
|
|
|
if (field.type === FieldType.number) {
|
|
|
|
const name = getFieldDisplayName(field, frame, context.data);
|
|
|
|
const value = field.name;
|
|
|
|
options.push({ value, label: name } as any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
defaultValue: '',
|
2022-11-28 02:16:35 -06:00
|
|
|
showIf: (cfg) =>
|
|
|
|
(cfg.footer?.show && !cfg.footer?.countRows) ||
|
|
|
|
(cfg.footer?.reducer?.length === 1 && cfg.footer?.reducer[0] !== ReducerID.count),
|
2022-03-31 02:36:04 -05:00
|
|
|
})
|
|
|
|
.addCustomEditor({
|
|
|
|
id: 'footer.enablePagination',
|
|
|
|
path: 'footer.enablePagination',
|
|
|
|
name: 'Enable pagination',
|
|
|
|
editor: PaginationEditor,
|
2021-09-22 08:29:07 -05:00
|
|
|
});
|
2021-10-25 06:55:06 -05:00
|
|
|
})
|
|
|
|
.setSuggestionsSupplier(new TableSuggestionsSupplier());
|