You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.7 KiB

import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker';
import { GrafanaTheme, StandardEditorProps } from '@grafana/data';
3 years ago
import {
Button,
ColorPicker,
HorizontalGroup,
IconButton,
InlineField,
InlineSwitch,
Input,
stylesFactory,
3 years ago
ThemeContext,
} from '@grafana/ui';
import React from 'react';
import { css } from 'emotion';
import * as _ from 'lodash';
interface Props {
3 years ago
defaultColor: string;
arcBackground: string;
3 years ago
thresholds: ThresholdConfig[];
}
type ThresholdConfig = {
color: string;
useMetric: boolean;
value?: number;
metricName?: string;
3 years ago
};
const DEFAULT_THRESHOLD: ThresholdConfig = {
color: '#378372d',
useMetric: false,
3 years ago
value: 0,
};
const fieldNamePickerSettings = {
settings: { width: 24 },
} as any;
export function ThresholdsEditor({ onChange, value, context }: StandardEditorProps<Props>) {
const config = value;
const addThreshold = () => {
config.thresholds.push(_.cloneDeep(DEFAULT_THRESHOLD));
onChange(config);
3 years ago
};
const removeThreshold = (idx: number) => {
config.thresholds.splice(idx, 1);
onChange(config);
3 years ago
};
const onFieldChange = (field: keyof Props, value: any) => {
config[field] = value;
onChange(config);
3 years ago
};
const onThresholdFieldChange = (thresholdIdx: number, field: keyof ThresholdConfig, value: any) => {
// @ts-ignore
config.thresholds[thresholdIdx][field] = value;
onChange(config);
};
return (
<ThemeContext.Consumer>
{(theme) => {
const styles = getStyles(theme.v1);
3 years ago
return (
<div>
<Input
className={styles.row}
3 years ago
type="text"
value={'Default Gauge Color'}
disabled
prefix={
<div>
<ColorPicker
color={config.defaultColor}
onChange={(val) => onFieldChange('defaultColor', val)}
enableNamedColors={true}
/>
</div>
}
/>
<Input
className={styles.row}
3 years ago
type="text"
value={'Arc Background'}
disabled
prefix={
<div>
<ColorPicker
color={config.arcBackground}
onChange={(val) => onFieldChange('arcBackground', val)}
enableNamedColors={true}
/>
</div>
}
/>
{config.thresholds.map((threshold, thresholdIdx) => {
return (
3 years ago
<HorizontalGroup key={thresholdIdx}>
3 years ago
<Input
type="text"
value={'Color'}
disabled
prefix={
<div>
<ColorPicker
color={threshold.color}
onChange={(newVal) => onThresholdFieldChange(thresholdIdx, 'color', newVal)}
enableNamedColors={true}
/>
</div>
}
/>
<InlineField label="Use metric">
<InlineSwitch
value={threshold.useMetric}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', (evt.target as any).checked)}
/>
3 years ago
</InlineField>
{threshold.useMetric ? (
<FieldNamePicker
context={context}
value={threshold.metricName as string}
onChange={(newVal: any) => onThresholdFieldChange(thresholdIdx, 'metricName', newVal)}
item={fieldNamePickerSettings}
/>
3 years ago
) : (
<Input
placeholder="value"
value={threshold.value}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'value', (evt.target as any).value)}
/>
)}
3 years ago
<IconButton
name="trash-alt"
onClick={() => removeThreshold(thresholdIdx)}
tooltip="Delete Threshold"
></IconButton>
3 years ago
</HorizontalGroup>
);
})}
<Button variant="secondary" onClick={addThreshold}>
Add Threshold
</Button>
</div>
);
}}
</ThemeContext.Consumer>
3 years ago
);
}
interface ThresholdsEditorStyles {
row: string;
}
const getStyles = stylesFactory((theme: GrafanaTheme): ThresholdsEditorStyles => {
return {
row: css`
margin-bottom: ${theme.spacing.sm};
`,
};
});