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.

164 lines
4.8 KiB

import { FieldNamePicker } from '../../grafana/MatchersUI/FieldNamePicker';
import { isNumber } from '../../utils';
import { GrafanaTheme, StandardEditorProps } from '@grafana/data';
2 years ago
import {
Button,
ColorPicker,
HorizontalGroup,
IconButton,
InlineField,
2 years ago
InlineLabel,
2 years ago
InlineSwitch,
Input,
stylesFactory,
2 years ago
ThemeContext,
} from '@grafana/ui';
import React from 'react';
import { css } from 'emotion';
import * as _ from 'lodash';
interface Props {
2 years ago
defaultColor: string;
arcBackground: string;
2 years ago
thresholds: ThresholdConfig[];
}
type ThresholdConfig = {
color: string;
useMetric: boolean;
value?: number;
metricName?: string;
2 years ago
};
const DEFAULT_THRESHOLD: ThresholdConfig = {
color: '#378372d',
useMetric: false,
2 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);
2 years ago
};
const removeThreshold = (idx: number) => {
config.thresholds.splice(idx, 1);
onChange(config);
2 years ago
};
const onFieldChange = (field: keyof Props, value: any) => {
config[field] = value;
onChange(config);
2 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);
2 years ago
return (
<div>
2 years ago
<InlineField label="Default Gauge Color" labelWidth={20}>
2 years ago
<InlineLabel>
<ColorPicker
color={config.defaultColor}
onChange={(val) => onFieldChange('defaultColor', val)}
enableNamedColors={false}
2 years ago
/>
</InlineLabel>
</InlineField>
2 years ago
<InlineField label="Arc Background" labelWidth={20}>
2 years ago
<InlineLabel>
<ColorPicker
color={config.arcBackground}
onChange={(val) => onFieldChange('arcBackground', val)}
enableNamedColors={false}
2 years ago
/>
</InlineLabel>
</InlineField>
2 years ago
{config.thresholds.map((threshold, thresholdIdx) => {
return (
2 years ago
<HorizontalGroup key={thresholdIdx}>
2 years ago
<InlineField label="Use metric">
<InlineSwitch
value={threshold.useMetric}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'useMetric', evt.currentTarget.checked)}
/>
2 years ago
</InlineField>
2 years ago
<InlineField>
{threshold.useMetric ? (
<FieldNamePicker
context={context}
value={threshold.metricName as string}
onChange={(newVal: any) => onThresholdFieldChange(thresholdIdx, 'metricName', newVal)}
item={fieldNamePickerSettings}
/>
) : (
<Input
placeholder="value"
2 years ago
width={24}
2 years ago
value={threshold.value}
onChange={(evt) => onThresholdFieldChange(thresholdIdx, 'value', isNumber(evt.currentTarget.value) ? +evt.currentTarget.value : undefined)}
2 years ago
/>
)}
</InlineField>
<InlineField label="Color">
<InlineLabel>
<ColorPicker
color={threshold.color}
onChange={(newVal) => onThresholdFieldChange(thresholdIdx, 'color', newVal)}
enableNamedColors={false}
2 years ago
/>
</InlineLabel>
</InlineField>
<InlineField>
<InlineLabel width={0} className={styles.deleteButton}>
<IconButton
name="trash-alt"
onClick={() => removeThreshold(thresholdIdx)}
tooltip="Delete Threshold"
></IconButton>
</InlineLabel>
</InlineField>
2 years ago
</HorizontalGroup>
);
})}
<Button variant="secondary" onClick={addThreshold}>
Add Threshold
</Button>
</div>
);
}}
</ThemeContext.Consumer>
2 years ago
);
}
interface ThresholdsEditorStyles {
2 years ago
deleteButton: string;
}
const getStyles = stylesFactory((theme: GrafanaTheme): ThresholdsEditorStyles => {
return {
2 years ago
deleteButton: css`
2 years ago
margin-right: 0px !important;
`,
};
});