grafana/packages/grafana-ui/src/components/ValueMappingsEditor/LegacyValueMappingsEditor.tsx

109 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-12-07 00:09:32 +08:00
import React, { PureComponent } from 'react';
FieldOverrides: Move FieldConfigSource from fieldOptions to PanelModel.fieldConfig (#22600) * Apply field overrides in PanelChrome * Move applyFieldOverrides to panel query runner * Review updates * Make sure overrides are applied back on souce panel when exiting the new edit mode * TS ignores in est * Make field display work in viz repeater * Review updates * Review and test updates * Change the way overrides and trransformations are retrieved in PQR * Add fieldConfig property to PanelModel * Dashboard migration v1 * Use field config when exiting new panel edit mode * Gauge - use fieldConfig from panel model * FieldDisplayOptions - don's extend FieldConfigSource * Fix fieldDisplay ts * StatPanel updated * Stat panel defaults applied * Table2 panel options update * React graph updates * BarGauge updated * PieChart, Gauge, BarGauge and Stat updates * PieChart - remove field config defaults from options * FieldDisplayEditor - remove unused methos * PanelModel - remove debugger * Remove fieldConfig from field options when migrating dashboard * Update data links migrations * Update fieldDisaplay tests to respect new fieldConfig * Update dashboard schema version in snapshots * Fix BarGaugePanel test * Rebase fixes * Add onFieldConfigChange to PanelProps type * Update shared single stat migration * Pass PanelModel instead of options only for panel type change handler [breaking] * Renames * Don't mutate panel options * Migrations update * Remove obsolete snap * Minor updates after review * Fix null checks * Temporarily (until we decide to switch to new pane edit) bring back old aditors * Temporarily rename ValueMappingEditor and MappingRow to Legacy* * Migrations update * Updae setFieldConfigDefaults API * Update the way field config defaults are applied * Use standard field config for gauge, bar gauge and stat panels * refactoring * Revert dashboard fieldOptions migrations as those are handled by single stat migrator * Fix ts in tests * Strict null fix and some minor fixes Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-03-19 18:50:31 +08:00
import LegacyMappingRow from './LegacyMappingRow';
import { MappingType, ValueMapping } from '@grafana/data';
import { Button } from '../Button/Button';
2019-02-21 17:59:13 +08:00
import { PanelOptionsGroup } from '../PanelOptionsGroup/PanelOptionsGroup';
2018-12-07 00:09:32 +08:00
2019-01-16 19:14:43 +08:00
export interface Props {
valueMappings?: ValueMapping[];
2019-01-16 19:14:43 +08:00
onChange: (valueMappings: ValueMapping[]) => void;
}
2018-12-07 00:09:32 +08:00
interface State {
2019-01-16 19:14:43 +08:00
valueMappings: ValueMapping[];
2018-12-11 21:17:58 +08:00
nextIdToAdd: number;
2018-12-07 00:09:32 +08:00
}
FieldOverrides: Move FieldConfigSource from fieldOptions to PanelModel.fieldConfig (#22600) * Apply field overrides in PanelChrome * Move applyFieldOverrides to panel query runner * Review updates * Make sure overrides are applied back on souce panel when exiting the new edit mode * TS ignores in est * Make field display work in viz repeater * Review updates * Review and test updates * Change the way overrides and trransformations are retrieved in PQR * Add fieldConfig property to PanelModel * Dashboard migration v1 * Use field config when exiting new panel edit mode * Gauge - use fieldConfig from panel model * FieldDisplayOptions - don's extend FieldConfigSource * Fix fieldDisplay ts * StatPanel updated * Stat panel defaults applied * Table2 panel options update * React graph updates * BarGauge updated * PieChart, Gauge, BarGauge and Stat updates * PieChart - remove field config defaults from options * FieldDisplayEditor - remove unused methos * PanelModel - remove debugger * Remove fieldConfig from field options when migrating dashboard * Update data links migrations * Update fieldDisaplay tests to respect new fieldConfig * Update dashboard schema version in snapshots * Fix BarGaugePanel test * Rebase fixes * Add onFieldConfigChange to PanelProps type * Update shared single stat migration * Pass PanelModel instead of options only for panel type change handler [breaking] * Renames * Don't mutate panel options * Migrations update * Remove obsolete snap * Minor updates after review * Fix null checks * Temporarily (until we decide to switch to new pane edit) bring back old aditors * Temporarily rename ValueMappingEditor and MappingRow to Legacy* * Migrations update * Updae setFieldConfigDefaults API * Update the way field config defaults are applied * Use standard field config for gauge, bar gauge and stat panels * refactoring * Revert dashboard fieldOptions migrations as those are handled by single stat migrator * Fix ts in tests * Strict null fix and some minor fixes Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-03-19 18:50:31 +08:00
export class LegacyValueMappingsEditor extends PureComponent<Props, State> {
2019-01-16 19:14:43 +08:00
constructor(props: Props) {
2018-12-07 00:09:32 +08:00
super(props);
const mappings = props.valueMappings || [];
2018-12-11 21:17:58 +08:00
2018-12-07 00:09:32 +08:00
this.state = {
2019-01-16 19:14:43 +08:00
valueMappings: mappings,
nextIdToAdd: mappings.length > 0 ? this.getMaxIdFromValueMappings(mappings) : 1,
2018-12-07 00:09:32 +08:00
};
}
2019-01-16 19:14:43 +08:00
getMaxIdFromValueMappings(mappings: ValueMapping[]) {
return (
Math.max.apply(
null,
mappings.map(mapping => mapping.id).map(m => m)
) + 1
);
2018-12-11 21:17:58 +08:00
}
onAddMapping = () =>
2018-12-07 00:09:32 +08:00
this.setState(prevState => ({
2019-01-16 19:14:43 +08:00
valueMappings: [
...prevState.valueMappings,
2018-12-11 21:17:58 +08:00
{
id: prevState.nextIdToAdd,
operator: '',
value: '',
text: '',
type: MappingType.ValueToText,
from: '',
to: '',
},
2018-12-10 23:57:34 +08:00
],
2018-12-11 21:17:58 +08:00
nextIdToAdd: prevState.nextIdToAdd + 1,
2018-12-07 00:09:32 +08:00
}));
2019-01-16 19:14:43 +08:00
onRemoveMapping = (id: number) => {
2018-12-11 21:17:58 +08:00
this.setState(
prevState => ({
2019-01-16 19:14:43 +08:00
valueMappings: prevState.valueMappings.filter(m => {
2018-12-11 21:17:58 +08:00
return m.id !== id;
}),
}),
() => {
2019-01-16 19:14:43 +08:00
this.props.onChange(this.state.valueMappings);
2018-12-11 21:17:58 +08:00
}
);
};
2018-12-10 23:57:34 +08:00
2019-01-16 19:14:43 +08:00
updateGauge = (mapping: ValueMapping) => {
2018-12-10 23:57:34 +08:00
this.setState(
prevState => ({
2019-01-16 19:14:43 +08:00
valueMappings: prevState.valueMappings.map(m => {
2018-12-11 21:17:58 +08:00
if (m.id === mapping.id) {
2018-12-10 23:57:34 +08:00
return { ...mapping };
}
2018-12-07 00:09:32 +08:00
2018-12-10 23:57:34 +08:00
return m;
}),
2018-12-07 23:55:58 +08:00
}),
2018-12-10 23:57:34 +08:00
() => {
2019-01-16 19:14:43 +08:00
this.props.onChange(this.state.valueMappings);
2018-12-10 23:57:34 +08:00
}
);
2018-12-07 23:55:58 +08:00
};
2018-12-07 00:09:32 +08:00
render() {
2019-01-16 19:14:43 +08:00
const { valueMappings } = this.state;
2018-12-07 00:09:32 +08:00
return (
<PanelOptionsGroup title="Value mappings">
<div>
{valueMappings.length > 0 &&
valueMappings.map((valueMapping, index) => (
FieldOverrides: Move FieldConfigSource from fieldOptions to PanelModel.fieldConfig (#22600) * Apply field overrides in PanelChrome * Move applyFieldOverrides to panel query runner * Review updates * Make sure overrides are applied back on souce panel when exiting the new edit mode * TS ignores in est * Make field display work in viz repeater * Review updates * Review and test updates * Change the way overrides and trransformations are retrieved in PQR * Add fieldConfig property to PanelModel * Dashboard migration v1 * Use field config when exiting new panel edit mode * Gauge - use fieldConfig from panel model * FieldDisplayOptions - don's extend FieldConfigSource * Fix fieldDisplay ts * StatPanel updated * Stat panel defaults applied * Table2 panel options update * React graph updates * BarGauge updated * PieChart, Gauge, BarGauge and Stat updates * PieChart - remove field config defaults from options * FieldDisplayEditor - remove unused methos * PanelModel - remove debugger * Remove fieldConfig from field options when migrating dashboard * Update data links migrations * Update fieldDisaplay tests to respect new fieldConfig * Update dashboard schema version in snapshots * Fix BarGaugePanel test * Rebase fixes * Add onFieldConfigChange to PanelProps type * Update shared single stat migration * Pass PanelModel instead of options only for panel type change handler [breaking] * Renames * Don't mutate panel options * Migrations update * Remove obsolete snap * Minor updates after review * Fix null checks * Temporarily (until we decide to switch to new pane edit) bring back old aditors * Temporarily rename ValueMappingEditor and MappingRow to Legacy* * Migrations update * Updae setFieldConfigDefaults API * Update the way field config defaults are applied * Use standard field config for gauge, bar gauge and stat panels * refactoring * Revert dashboard fieldOptions migrations as those are handled by single stat migrator * Fix ts in tests * Strict null fix and some minor fixes Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-03-19 18:50:31 +08:00
<LegacyMappingRow
key={`${valueMapping.text}-${index}`}
valueMapping={valueMapping}
updateValueMapping={this.updateGauge}
removeValueMapping={() => this.onRemoveMapping(valueMapping.id)}
/>
))}
<Button variant="inverse" icon="fa fa-plus" onClick={this.onAddMapping}>
Add mapping
</Button>
</div>
2019-01-13 19:42:21 +08:00
</PanelOptionsGroup>
2018-12-07 00:09:32 +08:00
);
}
}