2020-02-09 01:29:09 +08:00
|
|
|
import React from 'react';
|
2020-02-09 21:34:42 +08:00
|
|
|
import cloneDeep from 'lodash/cloneDeep';
|
|
|
|
import {
|
|
|
|
FieldConfigSource,
|
|
|
|
DataFrame,
|
|
|
|
FieldPropertyEditorItem,
|
2020-02-11 20:48:36 +08:00
|
|
|
VariableSuggestionsScope,
|
2020-02-14 04:37:24 +08:00
|
|
|
standardFieldConfigEditorRegistry,
|
2020-02-17 14:27:21 +08:00
|
|
|
PanelPlugin,
|
2020-02-14 20:46:08 +08:00
|
|
|
SelectableValue,
|
2020-02-09 21:34:42 +08:00
|
|
|
} from '@grafana/data';
|
2020-02-14 04:37:24 +08:00
|
|
|
import { Forms, fieldMatchersUI, ValuePicker } from '@grafana/ui';
|
2020-02-11 20:48:36 +08:00
|
|
|
import { getDataLinksVariableSuggestions } from '../../../panel/panellinks/link_srv';
|
2020-02-12 17:42:57 +08:00
|
|
|
import { OptionsGroup } from './OptionsGroup';
|
2020-02-14 20:46:08 +08:00
|
|
|
import { OverrideEditor } from './OverrideEditor';
|
2020-02-12 17:42:57 +08:00
|
|
|
|
2020-02-09 01:29:09 +08:00
|
|
|
interface Props {
|
2020-02-17 14:27:21 +08:00
|
|
|
plugin: PanelPlugin;
|
2020-02-09 01:29:09 +08:00
|
|
|
config: FieldConfigSource;
|
|
|
|
include?: string[]; // Ordered list of which fields should be shown/included
|
|
|
|
onChange: (config: FieldConfigSource) => void;
|
2020-02-12 17:42:57 +08:00
|
|
|
/* Helpful for IntelliSense */
|
2020-02-09 01:29:09 +08:00
|
|
|
data: DataFrame[];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expects the container div to have size set and will fill it 100%
|
|
|
|
*/
|
|
|
|
export class FieldConfigEditor extends React.PureComponent<Props> {
|
|
|
|
private setDefaultValue = (name: string, value: any, custom: boolean) => {
|
|
|
|
const defaults = { ...this.props.config.defaults };
|
|
|
|
const remove = value === undefined || value === null || '';
|
2020-02-12 17:42:57 +08:00
|
|
|
|
2020-02-09 01:29:09 +08:00
|
|
|
if (custom) {
|
|
|
|
if (defaults.custom) {
|
|
|
|
if (remove) {
|
|
|
|
defaults.custom = { ...defaults.custom };
|
|
|
|
delete defaults.custom[name];
|
|
|
|
} else {
|
|
|
|
defaults.custom = { ...defaults.custom, [name]: value };
|
|
|
|
}
|
|
|
|
} else if (!remove) {
|
|
|
|
defaults.custom = { [name]: value };
|
|
|
|
}
|
|
|
|
} else if (remove) {
|
|
|
|
delete (defaults as any)[name];
|
|
|
|
} else {
|
|
|
|
(defaults as any)[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onChange({
|
|
|
|
...this.props.config,
|
|
|
|
defaults,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-02-14 20:46:08 +08:00
|
|
|
onOverrideChange = (index: number, override: any) => {
|
2020-02-09 21:34:42 +08:00
|
|
|
const { config } = this.props;
|
|
|
|
let overrides = cloneDeep(config.overrides);
|
2020-02-14 20:46:08 +08:00
|
|
|
overrides[index] = override;
|
2020-02-09 21:34:42 +08:00
|
|
|
this.props.onChange({ ...config, overrides });
|
|
|
|
};
|
|
|
|
|
2020-02-14 20:46:08 +08:00
|
|
|
onOverrideRemove = (overrideIndex: number) => {
|
2020-02-09 21:34:42 +08:00
|
|
|
const { config } = this.props;
|
|
|
|
let overrides = cloneDeep(config.overrides);
|
2020-02-14 20:46:08 +08:00
|
|
|
overrides.splice(overrideIndex, 1);
|
2020-02-09 21:34:42 +08:00
|
|
|
this.props.onChange({ ...config, overrides });
|
|
|
|
};
|
|
|
|
|
2020-02-14 20:46:08 +08:00
|
|
|
onOverrideAdd = (value: SelectableValue<string>) => {
|
|
|
|
const { onChange, config } = this.props;
|
|
|
|
onChange({
|
|
|
|
...config,
|
|
|
|
overrides: [
|
|
|
|
...config.overrides,
|
|
|
|
{
|
|
|
|
matcher: {
|
|
|
|
id: value.value!,
|
|
|
|
},
|
|
|
|
properties: [],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2020-02-09 21:34:42 +08:00
|
|
|
};
|
|
|
|
|
2020-02-09 01:29:09 +08:00
|
|
|
renderEditor(item: FieldPropertyEditorItem, custom: boolean) {
|
2020-02-11 20:48:36 +08:00
|
|
|
const { data } = this.props;
|
2020-02-09 01:29:09 +08:00
|
|
|
const config = this.props.config.defaults;
|
|
|
|
const value = custom ? (config.custom ? config.custom[item.id] : undefined) : (config as any)[item.id];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Forms.Field label={item.name} description={item.description} key={`${item.id}/${custom}`}>
|
2020-02-11 20:48:36 +08:00
|
|
|
<item.editor
|
|
|
|
item={item}
|
|
|
|
value={value}
|
|
|
|
onChange={v => this.setDefaultValue(item.id, v, custom)}
|
|
|
|
context={{
|
|
|
|
data,
|
|
|
|
getSuggestions: (scope?: VariableSuggestionsScope) => getDataLinksVariableSuggestions(data, scope),
|
|
|
|
}}
|
|
|
|
/>
|
2020-02-09 01:29:09 +08:00
|
|
|
</Forms.Field>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderStandardConfigs() {
|
|
|
|
const { include } = this.props;
|
|
|
|
if (include) {
|
|
|
|
return include.map(f => this.renderEditor(standardFieldConfigEditorRegistry.get(f), false));
|
|
|
|
}
|
|
|
|
return standardFieldConfigEditorRegistry.list().map(f => this.renderEditor(f, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCustomConfigs() {
|
2020-02-17 14:27:21 +08:00
|
|
|
const { plugin } = this.props;
|
|
|
|
|
|
|
|
if (!plugin.customFieldConfigs) {
|
2020-02-09 01:29:09 +08:00
|
|
|
return null;
|
|
|
|
}
|
2020-02-17 14:27:21 +08:00
|
|
|
|
|
|
|
return plugin.customFieldConfigs.list().map(f => this.renderEditor(f, true));
|
2020-02-09 01:29:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderOverrides() {
|
2020-02-17 14:27:21 +08:00
|
|
|
const { config, data, plugin } = this.props;
|
|
|
|
const { customFieldConfigs } = plugin;
|
|
|
|
|
2020-02-10 01:47:48 +08:00
|
|
|
if (config.overrides.length === 0) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-02-09 01:29:09 +08:00
|
|
|
|
2020-02-09 21:34:42 +08:00
|
|
|
let configPropertiesOptions = standardFieldConfigEditorRegistry.list().map(i => ({
|
|
|
|
label: i.name,
|
|
|
|
value: i.id,
|
|
|
|
description: i.description,
|
|
|
|
custom: false,
|
|
|
|
}));
|
|
|
|
|
2020-02-17 14:27:21 +08:00
|
|
|
if (customFieldConfigs) {
|
2020-02-09 21:34:42 +08:00
|
|
|
configPropertiesOptions = configPropertiesOptions.concat(
|
2020-02-17 14:27:21 +08:00
|
|
|
customFieldConfigs.list().map(i => ({
|
2020-02-09 21:34:42 +08:00
|
|
|
label: i.name,
|
|
|
|
value: i.id,
|
|
|
|
description: i.description,
|
|
|
|
custom: true,
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-10 01:47:48 +08:00
|
|
|
<div>
|
2020-02-09 21:34:42 +08:00
|
|
|
{config.overrides.map((o, i) => {
|
2020-02-14 04:37:24 +08:00
|
|
|
// TODO: apply matcher to retrieve fields
|
2020-02-09 21:34:42 +08:00
|
|
|
return (
|
2020-02-14 20:46:08 +08:00
|
|
|
<OverrideEditor
|
|
|
|
key={`${o.matcher.id}/${i}`}
|
|
|
|
data={data}
|
|
|
|
override={o}
|
|
|
|
onChange={value => this.onOverrideChange(i, value)}
|
|
|
|
onRemove={() => this.onOverrideRemove(i)}
|
|
|
|
configPropertiesOptions={configPropertiesOptions}
|
2020-02-17 14:27:21 +08:00
|
|
|
customPropertiesRegistry={customFieldConfigs}
|
2020-02-14 20:46:08 +08:00
|
|
|
/>
|
2020-02-09 21:34:42 +08:00
|
|
|
);
|
|
|
|
})}
|
2020-02-10 01:47:48 +08:00
|
|
|
</div>
|
2020-02-09 21:34:42 +08:00
|
|
|
);
|
2020-02-09 01:29:09 +08:00
|
|
|
}
|
|
|
|
|
2020-02-09 21:34:42 +08:00
|
|
|
renderAddOverride = () => {
|
|
|
|
return (
|
2020-02-10 01:47:48 +08:00
|
|
|
<ValuePicker
|
2020-02-09 21:34:42 +08:00
|
|
|
icon="plus"
|
2020-02-10 01:47:48 +08:00
|
|
|
label="Add override"
|
2020-02-14 20:46:08 +08:00
|
|
|
options={fieldMatchersUI
|
|
|
|
.list()
|
|
|
|
.map<SelectableValue<string>>(i => ({ label: i.name, value: i.id, description: i.description }))}
|
|
|
|
onChange={value => this.onOverrideAdd(value)}
|
2020-02-09 21:34:42 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-02-09 01:29:09 +08:00
|
|
|
render() {
|
2020-02-17 14:27:21 +08:00
|
|
|
const { plugin } = this.props;
|
|
|
|
|
2020-02-09 01:29:09 +08:00
|
|
|
return (
|
|
|
|
<div>
|
2020-02-17 14:27:21 +08:00
|
|
|
{plugin.customFieldConfigs && (
|
|
|
|
<OptionsGroup title={`${plugin.meta.name} options`}>{this.renderCustomConfigs()}</OptionsGroup>
|
|
|
|
)}
|
2020-02-12 17:42:57 +08:00
|
|
|
|
2020-02-17 14:27:21 +08:00
|
|
|
<OptionsGroup title="Field defaults">{this.renderStandardConfigs()}</OptionsGroup>
|
2020-02-12 17:42:57 +08:00
|
|
|
|
2020-02-17 14:27:21 +08:00
|
|
|
<OptionsGroup title="Field overrides">
|
2020-02-10 00:39:26 +08:00
|
|
|
{this.renderOverrides()}
|
2020-02-10 01:47:48 +08:00
|
|
|
{this.renderAddOverride()}
|
2020-02-12 17:42:57 +08:00
|
|
|
</OptionsGroup>
|
2020-02-09 01:29:09 +08:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FieldConfigEditor;
|