-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
controlUtils.js
240 lines (221 loc) · 6.97 KB
/
controlUtils.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import memoizeOne from 'memoize-one';
import { getChartControlPanelRegistry } from '@superset-ui/chart';
import { expandControlConfig } from '@superset-ui/chart-controls';
import { controls as SHARED_CONTROLS } from './controls';
import * as SECTIONS from './controlPanels/sections';
export function getFormDataFromControls(controlsState) {
const formData = { queryFields: {} };
Object.keys(controlsState).forEach(controlName => {
const control = controlsState[controlName];
formData[controlName] = control.value;
if (control.hasOwnProperty('queryField')) {
formData.queryFields[controlName] = control.queryField;
}
});
return formData;
}
export function validateControl(control, processedState) {
const validators = control.validators;
if (validators && validators.length > 0) {
const validatedControl = { ...control };
const validationErrors = [];
validators.forEach(f => {
const v = f.call(control, control.value, processedState);
if (v) {
validationErrors.push(v);
}
});
return { ...validatedControl, validationErrors };
}
return control;
}
/**
* Find control item from control panel config.
*/
function findControlItem(controlPanelSections, controlKey) {
for (const section of controlPanelSections) {
for (const controlArr of section.controlSetRows) {
for (const control of controlArr) {
if (controlKey === control) return control;
if (
control !== null &&
typeof control === 'object' &&
control.name === controlKey
) {
return control;
}
}
}
}
return null;
}
export const getControlConfig = memoizeOne(function getControlConfig(
controlKey,
vizType,
) {
const controlPanelConfig = getChartControlPanelRegistry().get(vizType) || {};
const {
controlOverrides = {},
controlPanelSections = [],
} = controlPanelConfig;
const control = expandControlConfig(
findControlItem(controlPanelSections, controlKey),
controlOverrides,
);
return control?.config || control;
});
export function applyMapStateToPropsToControl(controlState, controlPanelState) {
const { mapStateToProps } = controlState;
if (mapStateToProps && controlPanelState) {
return {
...controlState,
...mapStateToProps(controlPanelState, controlState),
};
}
return controlState;
}
function handleMissingChoice(control) {
// If the value is not valid anymore based on choices, clear it
const value = control.value;
if (
control.type === 'SelectControl' &&
!control.freeForm &&
control.choices &&
value
) {
const alteredControl = { ...control };
const choiceValues = control.choices.map(c => c[0]);
if (control.multi && value.length > 0) {
alteredControl.value = value.filter(el => choiceValues.indexOf(el) > -1);
return alteredControl;
} else if (!control.multi && choiceValues.indexOf(value) < 0) {
alteredControl.value = null;
return alteredControl;
}
}
return control;
}
export function getControlStateFromControlConfig(
controlConfig,
controlPanelState,
value,
) {
// skip invalid config values
if (!controlConfig) {
return null;
}
let controlState = { ...controlConfig };
// only apply mapStateToProps when control states have been initialized
if (
controlPanelState &&
(controlPanelState.controls || !controlPanelState.isInitializing)
) {
controlState = applyMapStateToPropsToControl(
controlState,
controlPanelState,
);
}
// If default is a function, evaluate it
if (typeof controlState.default === 'function') {
controlState.default = controlState.default(
controlState,
controlPanelState,
);
// if default is still a function, discard
if (typeof controlState.default === 'function') {
delete controlState.default;
}
}
// If a choice control went from multi=false to true, wrap value in array
const controlValue =
controlConfig.multi && value && !Array.isArray(value) ? [value] : value;
controlState.value =
typeof controlValue === 'undefined' ? controlState.default : controlValue;
return validateControl(handleMissingChoice(controlState), controlState);
}
export function getControlState(controlKey, vizType, state, value) {
return getControlStateFromControlConfig(
getControlConfig(controlKey, vizType),
state,
value,
);
}
/**
* Get the clean and processed control panel sections
*/
export const sectionsToRender = memoizeOne(function sectionsToRender(
vizType,
datasourceType,
) {
const controlPanelConfig = getChartControlPanelRegistry().get(vizType) || {};
const {
sectionOverrides = {},
controlOverrides,
controlPanelSections = [],
} = controlPanelConfig;
// default control panel sections
const sections = { ...SECTIONS };
// apply section overrides
Object.entries(sectionOverrides).forEach(([section, overrides]) => {
if (typeof overrides === 'object' && overrides.constructor === Object) {
sections[section] = {
...sections[section],
...overrides,
};
} else {
sections[section] = overrides;
}
});
const { datasourceAndVizType, sqlaTimeSeries, druidTimeSeries } = sections;
const timeSection =
datasourceType === 'table' ? sqlaTimeSeries : druidTimeSeries;
return []
.concat(datasourceAndVizType, timeSection, controlPanelSections)
.filter(section => !!section)
.map(section => {
const { controlSetRows } = section;
return {
...section,
controlSetRows:
controlSetRows?.map(row =>
row.map(item => expandControlConfig(item, controlOverrides)),
) || [],
};
});
});
export function getAllControlsState(vizType, datasourceType, state, formData) {
const controlsState = {};
sectionsToRender(vizType, datasourceType).forEach(section =>
section.controlSetRows.forEach(fieldsetRow =>
fieldsetRow.forEach(field => {
if (field && field.config && field.name) {
const { config, name } = field;
controlsState[name] = getControlStateFromControlConfig(
config,
state,
formData[name],
);
}
}),
),
);
return controlsState;
}