2021-03-25 02:33:13 -05:00
|
|
|
import React from 'react';
|
|
|
|
import { StandardEditorContext, VariableSuggestionsScope } from '@grafana/data';
|
2021-07-07 01:58:07 -05:00
|
|
|
import { get as lodashGet } from 'lodash';
|
2021-04-28 02:58:53 -05:00
|
|
|
import { getDataLinksVariableSuggestions } from 'app/features/panel/panellinks/link_srv';
|
2021-03-25 02:33:13 -05:00
|
|
|
import { OptionPaneRenderProps } from './types';
|
2021-07-07 01:58:07 -05:00
|
|
|
import { updateDefaultFieldConfigValue, setOptionImmutably } from './utils';
|
2021-03-25 02:33:13 -05:00
|
|
|
import { OptionsPaneItemDescriptor } from './OptionsPaneItemDescriptor';
|
|
|
|
import { OptionsPaneCategoryDescriptor } from './OptionsPaneCategoryDescriptor';
|
|
|
|
|
|
|
|
export function getVizualizationOptions(props: OptionPaneRenderProps): OptionsPaneCategoryDescriptor[] {
|
|
|
|
const { plugin, panel, onPanelOptionsChanged, onFieldConfigsChange, data, dashboard } = props;
|
|
|
|
const currentOptions = panel.getOptions();
|
|
|
|
const currentFieldConfig = panel.fieldConfig;
|
|
|
|
const categoryIndex: Record<string, OptionsPaneCategoryDescriptor> = {};
|
|
|
|
|
|
|
|
const context: StandardEditorContext<any> = {
|
|
|
|
data: data?.series || [],
|
|
|
|
replaceVariables: panel.replaceVariables,
|
|
|
|
options: currentOptions,
|
|
|
|
eventBus: dashboard.events,
|
|
|
|
getSuggestions: (scope?: VariableSuggestionsScope) => {
|
2021-04-28 02:58:53 -05:00
|
|
|
return data ? getDataLinksVariableSuggestions(data.series, scope) : [];
|
2021-03-25 02:33:13 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const getOptionsPaneCategory = (categoryNames?: string[]): OptionsPaneCategoryDescriptor => {
|
|
|
|
const categoryName = (categoryNames && categoryNames[0]) ?? `${plugin.meta.name}`;
|
|
|
|
const category = categoryIndex[categoryName];
|
|
|
|
|
|
|
|
if (category) {
|
|
|
|
return category;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (categoryIndex[categoryName] = new OptionsPaneCategoryDescriptor({
|
|
|
|
title: categoryName,
|
|
|
|
id: categoryName,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Panel options
|
|
|
|
*/
|
|
|
|
for (const pluginOption of plugin.optionEditors.list()) {
|
2021-04-20 14:22:58 -05:00
|
|
|
if (pluginOption.showIf && !pluginOption.showIf(currentOptions, data?.series)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-25 02:33:13 -05:00
|
|
|
const category = getOptionsPaneCategory(pluginOption.category);
|
|
|
|
const Editor = pluginOption.editor;
|
|
|
|
|
|
|
|
category.addItem(
|
|
|
|
new OptionsPaneItemDescriptor({
|
|
|
|
title: pluginOption.name,
|
|
|
|
description: pluginOption.description,
|
2021-04-13 01:46:07 -05:00
|
|
|
render: function renderEditor() {
|
2021-03-25 02:33:13 -05:00
|
|
|
const onChange = (value: any) => {
|
2021-07-07 01:58:07 -05:00
|
|
|
const newOptions = setOptionImmutably(currentOptions, pluginOption.path, value);
|
2021-03-25 02:33:13 -05:00
|
|
|
onPanelOptionsChanged(newOptions);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Editor
|
|
|
|
value={lodashGet(currentOptions, pluginOption.path)}
|
|
|
|
onChange={onChange}
|
|
|
|
item={pluginOption}
|
|
|
|
context={context}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Field options
|
|
|
|
*/
|
|
|
|
for (const fieldOption of plugin.fieldConfigRegistry.list()) {
|
|
|
|
if (
|
|
|
|
fieldOption.isCustom &&
|
|
|
|
fieldOption.showIf &&
|
|
|
|
!fieldOption.showIf(currentFieldConfig.defaults.custom, data?.series)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fieldOption.hideFromDefaults) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const category = getOptionsPaneCategory(fieldOption.category);
|
|
|
|
const Editor = fieldOption.editor;
|
|
|
|
|
|
|
|
const defaults = currentFieldConfig.defaults;
|
|
|
|
const value = fieldOption.isCustom
|
|
|
|
? defaults.custom
|
|
|
|
? lodashGet(defaults.custom, fieldOption.path)
|
|
|
|
: undefined
|
|
|
|
: lodashGet(defaults, fieldOption.path);
|
|
|
|
|
|
|
|
if (fieldOption.getItemsCount) {
|
|
|
|
category.props.itemsCount = fieldOption.getItemsCount(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
category.addItem(
|
|
|
|
new OptionsPaneItemDescriptor({
|
|
|
|
title: fieldOption.name,
|
|
|
|
description: fieldOption.description,
|
2021-04-13 01:46:07 -05:00
|
|
|
render: function renderEditor() {
|
2021-03-25 02:33:13 -05:00
|
|
|
const onChange = (v: any) => {
|
|
|
|
onFieldConfigsChange(
|
|
|
|
updateDefaultFieldConfigValue(currentFieldConfig, fieldOption.path, v, fieldOption.isCustom)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return <Editor value={value} onChange={onChange} item={fieldOption} context={context} />;
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Object.values(categoryIndex);
|
|
|
|
}
|