|
|
|
@ -1,9 +1,11 @@ |
|
|
|
|
import React, { ChangeEvent, FC, useCallback } from 'react'; |
|
|
|
|
import { GrafanaTheme2, SelectableValue, StandardEditorProps } from '@grafana/data'; |
|
|
|
|
import { ComparisonOperation, FeatureStyleConfig } from '../types'; |
|
|
|
|
import { Button, ColorPicker, InlineField, InlineFieldRow, Input, Select, useStyles2 } from '@grafana/ui'; |
|
|
|
|
import { Button, InlineField, InlineFieldRow, Input, Select, useStyles2 } from '@grafana/ui'; |
|
|
|
|
import { css } from '@emotion/css'; |
|
|
|
|
import { NumberInput } from 'app/features/dimensions/editors/NumberInput'; |
|
|
|
|
import { StyleEditor } from '../layers/data/StyleEditor'; |
|
|
|
|
import { defaultStyleConfig, StyleConfig } from '../style/types'; |
|
|
|
|
import { DEFAULT_STYLE_RULE } from '../layers/data/geojsonLayer'; |
|
|
|
|
|
|
|
|
|
export interface StyleRuleEditorSettings { |
|
|
|
|
options: SelectableValue[]; |
|
|
|
@ -12,7 +14,7 @@ export interface StyleRuleEditorSettings { |
|
|
|
|
export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, any, StyleRuleEditorSettings>> = ( |
|
|
|
|
props |
|
|
|
|
) => { |
|
|
|
|
const { value, onChange, item } = props; |
|
|
|
|
const { value, onChange, item, context } = props; |
|
|
|
|
const settings: StyleRuleEditorSettings = item.settings; |
|
|
|
|
|
|
|
|
|
const styles = useStyles2(getStyles); |
|
|
|
@ -23,11 +25,11 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
(e: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
onChange({ |
|
|
|
|
...value, |
|
|
|
|
rule: { |
|
|
|
|
...value.rule, |
|
|
|
|
check: { |
|
|
|
|
...value.check, |
|
|
|
|
property: e.currentTarget.value, |
|
|
|
|
operation: value.rule?.operation ?? ComparisonOperation.EQ, |
|
|
|
|
value: value.rule?.value ?? '', |
|
|
|
|
operation: value.check?.operation ?? ComparisonOperation.EQ, |
|
|
|
|
value: value.check?.value ?? '', |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
@ -38,11 +40,11 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
(selection: SelectableValue) => { |
|
|
|
|
onChange({ |
|
|
|
|
...value, |
|
|
|
|
rule: { |
|
|
|
|
...value.rule, |
|
|
|
|
check: { |
|
|
|
|
...value.check, |
|
|
|
|
operation: selection.value ?? ComparisonOperation.EQ, |
|
|
|
|
property: value.rule?.property ?? '', |
|
|
|
|
value: value.rule?.value ?? '', |
|
|
|
|
property: value.check?.property ?? '', |
|
|
|
|
value: value.check?.value ?? '', |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
@ -53,27 +55,20 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
(e: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
onChange({ |
|
|
|
|
...value, |
|
|
|
|
rule: { |
|
|
|
|
...value.rule, |
|
|
|
|
check: { |
|
|
|
|
...value.check, |
|
|
|
|
value: e.currentTarget.value, |
|
|
|
|
operation: value.rule?.operation ?? ComparisonOperation.EQ, |
|
|
|
|
property: value.rule?.property ?? '', |
|
|
|
|
operation: value.check?.operation ?? ComparisonOperation.EQ, |
|
|
|
|
property: value.check?.property ?? '', |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
}, |
|
|
|
|
[onChange, value] |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const onChangeColor = useCallback( |
|
|
|
|
(c: string) => { |
|
|
|
|
onChange({ ...value, fillColor: c }); |
|
|
|
|
}, |
|
|
|
|
[onChange, value] |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const onChangeStrokeWidth = useCallback( |
|
|
|
|
(num: number | undefined) => { |
|
|
|
|
onChange({ ...value, strokeWidth: num ?? value.strokeWidth ?? 1 }); |
|
|
|
|
const onChangeStyle = useCallback( |
|
|
|
|
(style?: StyleConfig) => { |
|
|
|
|
onChange({ ...value, style }); |
|
|
|
|
}, |
|
|
|
|
[onChange, value] |
|
|
|
|
); |
|
|
|
@ -82,6 +77,8 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
onChange(undefined); |
|
|
|
|
}, [onChange]); |
|
|
|
|
|
|
|
|
|
const check = value.check ?? DEFAULT_STYLE_RULE.check!; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div className={styles.rule}> |
|
|
|
|
<InlineFieldRow className={styles.row}> |
|
|
|
@ -89,7 +86,7 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
<Input |
|
|
|
|
type="text" |
|
|
|
|
placeholder={'Feature property'} |
|
|
|
|
value={`${value?.rule?.property}`} |
|
|
|
|
value={check.property ?? ''} |
|
|
|
|
onChange={onChangeComparisonProperty} |
|
|
|
|
aria-label={'Feature property'} |
|
|
|
|
/> |
|
|
|
@ -97,7 +94,7 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
<InlineField className={styles.inline} grow={true}> |
|
|
|
|
<Select |
|
|
|
|
menuShouldPortal |
|
|
|
|
value={`${value?.rule?.operation}` ?? ComparisonOperation.EQ} |
|
|
|
|
value={check.operation ?? ComparisonOperation.EQ} |
|
|
|
|
options={settings.options} |
|
|
|
|
onChange={onChangeComparison} |
|
|
|
|
aria-label={'Comparison operator'} |
|
|
|
@ -107,26 +104,11 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
<Input |
|
|
|
|
type="text" |
|
|
|
|
placeholder={'value'} |
|
|
|
|
value={`${value?.rule?.value}`} |
|
|
|
|
value={`${check.value}` ?? ''} |
|
|
|
|
onChange={onChangeComparisonValue} |
|
|
|
|
aria-label={'Comparison value'} |
|
|
|
|
/> |
|
|
|
|
</InlineField> |
|
|
|
|
</InlineFieldRow> |
|
|
|
|
<InlineFieldRow className={styles.row}> |
|
|
|
|
<InlineField label="Style" labelWidth={LABEL_WIDTH} className={styles.color}> |
|
|
|
|
<ColorPicker color={value?.fillColor} onChange={onChangeColor} /> |
|
|
|
|
</InlineField> |
|
|
|
|
<InlineField label="Stroke" className={styles.inline} grow={true}> |
|
|
|
|
<NumberInput |
|
|
|
|
value={value?.strokeWidth ?? 1} |
|
|
|
|
min={1} |
|
|
|
|
max={20} |
|
|
|
|
step={0.5} |
|
|
|
|
aria-label={'Stroke width'} |
|
|
|
|
onChange={onChangeStrokeWidth} |
|
|
|
|
/> |
|
|
|
|
</InlineField> |
|
|
|
|
<Button |
|
|
|
|
size="md" |
|
|
|
|
icon="trash-alt" |
|
|
|
@ -136,6 +118,20 @@ export const StyleRuleEditor: FC<StandardEditorProps<FeatureStyleConfig, any, an |
|
|
|
|
className={styles.button} |
|
|
|
|
></Button> |
|
|
|
|
</InlineFieldRow> |
|
|
|
|
<div> |
|
|
|
|
<StyleEditor |
|
|
|
|
value={value.style ?? defaultStyleConfig} |
|
|
|
|
context={context} |
|
|
|
|
onChange={onChangeStyle} |
|
|
|
|
item={ |
|
|
|
|
{ |
|
|
|
|
settings: { |
|
|
|
|
simpleFixedValues: true, |
|
|
|
|
}, |
|
|
|
|
} as any |
|
|
|
|
} |
|
|
|
|
/> |
|
|
|
|
</div> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
@ -152,11 +148,6 @@ const getStyles = (theme: GrafanaTheme2) => ({ |
|
|
|
|
margin-bottom: 0; |
|
|
|
|
margin-left: 4px; |
|
|
|
|
`,
|
|
|
|
|
color: css` |
|
|
|
|
align-items: center; |
|
|
|
|
margin-bottom: 0; |
|
|
|
|
margin-right: 4px; |
|
|
|
|
`,
|
|
|
|
|
button: css` |
|
|
|
|
margin-left: 4px; |
|
|
|
|
`,
|
|
|
|
|