-
Notifications
You must be signed in to change notification settings - Fork 11.7k
/
layerEditor.tsx
133 lines (120 loc) · 4.17 KB
/
layerEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { get as lodashGet, isEqual } from 'lodash';
import { FrameGeometrySourceMode, getFrameMatchers, MapLayerOptions } from '@grafana/data';
import { NestedPanelOptions, NestedValueAccess } from '@grafana/data/src/utils/OptionsUIBuilders';
import { setOptionImmutably } from 'app/features/dashboard/components/PanelEditor/utils';
import { addLocationFields } from 'app/features/geo/editor/locationEditor';
import { defaultMarkersConfig } from '../layers/data/markersLayer';
import { DEFAULT_BASEMAP_CONFIG, geomapLayerRegistry, getLayersOptions } from '../layers/registry';
import { MapLayerState } from '../types';
import { FrameSelectionEditor } from './FrameSelectionEditor';
export interface LayerEditorOptions {
state: MapLayerState;
category: string[];
basemaps: boolean; // only basemaps
}
export function getLayerEditor(opts: LayerEditorOptions): NestedPanelOptions<MapLayerOptions> {
return {
category: opts.category,
path: '--', // Not used
defaultValue: opts.basemaps ? DEFAULT_BASEMAP_CONFIG : defaultMarkersConfig,
values: (parent: NestedValueAccess) => ({
getContext: (parent) => {
return { ...parent, options: opts.state.options, instanceState: opts.state };
},
getValue: (path: string) => lodashGet(opts.state.options, path),
onChange: (path: string, value: string) => {
const { state } = opts;
const { options } = state;
if (path === 'type' && value) {
const layer = geomapLayerRegistry.getIfExists(value);
if (layer) {
const opts = {
...options, // keep current shared options
type: layer.id,
config: { ...layer.defaultOptions }, // clone?
};
if (layer.showLocation) {
if (!opts.location?.mode) {
opts.location = { mode: FrameGeometrySourceMode.Auto };
} else {
delete opts.location;
}
}
state.onChange(opts);
return;
}
}
state.onChange(setOptionImmutably(options, path, value));
},
}),
build: (builder, context) => {
if (!opts.state) {
return;
}
const { handler, options } = opts.state;
const layer = geomapLayerRegistry.getIfExists(options?.type);
const layerTypes = getLayersOptions(
opts.basemaps,
options?.type // the selected value
? options.type
: DEFAULT_BASEMAP_CONFIG.type
);
builder.addSelect({
path: 'type',
name: 'Layer type', // required, but hide space
settings: {
options: layerTypes.options,
},
});
// Show data filter if the layer type can do something with the data query results
if (handler.update) {
builder.addCustomEditor({
id: 'filterData',
path: 'filterData',
name: 'Data',
editor: FrameSelectionEditor,
defaultValue: undefined,
});
}
if (!layer) {
return; // unknown layer type
}
// Don't show UI for default configuration
if (options.type === DEFAULT_BASEMAP_CONFIG.type) {
return;
}
if (layer.showLocation) {
let data = context.data;
// If `filterData` exists filter data feeding into location editor
if (options.filterData) {
const matcherFunc = getFrameMatchers(options.filterData);
data = data.filter(matcherFunc);
}
addLocationFields('Location', 'location.', builder, options.location, data);
}
if (handler.registerOptionsUI) {
handler.registerOptionsUI(builder);
}
if (!isEqual(opts.category, ['Base layer'])) {
if (!layer.hideOpacity) {
builder.addSliderInput({
path: 'opacity',
name: 'Opacity',
defaultValue: 1,
settings: {
min: 0,
max: 1,
step: 0.1,
},
});
}
builder.addBooleanSwitch({
path: 'tooltip',
name: 'Display tooltip',
description: 'Show the tooltip for layer',
defaultValue: true,
});
}
},
};
}