-
Notifications
You must be signed in to change notification settings - Fork 337
/
variable_form.tsx
420 lines (405 loc) · 16.5 KB
/
variable_form.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import React from "react";
import { Row, Col, FBSelect, Color, BlurableInput, Help } from "../../ui";
import {
variableFormList, NO_VALUE_SELECTED_DDI, sortVariables, heading, sequences2Ddi,
LOCATION_PLACEHOLDER_DDI,
peripherals2Ddi,
sensors2Ddi,
} from "./variable_form_list";
import { convertDDItoVariable } from "../locals_list/handle_select";
import {
VariableFormProps, AllowedVariableNodes, VariableNode, OnChange, VariableType,
} from "../locals_list/locals_list_support";
import {
determineVector, determineDropdown, SequenceMeta, determineVarDDILabel,
maybeFindVariable,
} from "../../resources/sequence_meta";
import { ResourceIndex, UUID } from "../../resources/interfaces";
import { DefaultValueForm } from "./default_value_form";
import { t } from "../../i18next_wrapper";
import { CoordinateInputBoxes } from "./location_form_coordinate_input_boxes";
import { generateNewVariableLabel } from "./locals_list";
import { error } from "../../toast/toast";
import { cloneDeep } from "lodash";
import { defensiveClone } from "../../util";
import { Numeric, Resource, Text } from "farmbot";
import { ToolTips } from "../../constants";
import { Position } from "@blueprintjs/core";
import {
determineVariableType, newVariableLabel, VariableIcon,
} from "./new_variable";
import { jsonReplacer } from "../step_tiles";
import {
selectAllPeripherals, selectAllSensors, selectAllSequences,
} from "../../resources/selectors_by_kind";
import { PERIPHERAL_HEADING, SENSOR_HEADING } from "../step_tiles/pin_support";
/**
* If a variable with a matching label exists in local parameter applications
* (step body, etc.), use it instead of the one in scope declarations.
*/
const maybeUseStepData = ({ resources, bodyVariables, variable, uuid }: {
resources: ResourceIndex,
bodyVariables: VariableNode[] | undefined,
variable: SequenceMeta,
uuid: UUID,
}): SequenceMeta => {
const executeStepData = bodyVariables?.filter(v =>
v.args.label === variable.celeryNode.args.label)[0];
if (executeStepData) {
return {
celeryNode: executeStepData,
vector: determineVector(executeStepData, resources, uuid),
dropdown: determineDropdown(executeStepData, resources, uuid),
};
}
return variable;
};
/**
* Form with an "import from" dropdown and coordinate input boxes.
* Can be used to set a specific value, import a value, or declare a variable.
*/
export const VariableForm =
// eslint-disable-next-line complexity
(props: VariableFormProps) => {
const { sequenceUuid, resources, bodyVariables, variable, variableType,
allowedVariableNodes, hideGroups, removeVariable, onChange } = props;
const { celeryNode, dropdown, vector, isDefault } = maybeUseStepData({
resources, bodyVariables, variable, uuid: sequenceUuid
});
const variableListItems = generateVariableListItems({
allowedVariableNodes, resources, sequenceUuid, variableType,
});
const displayGroups = !hideGroups;
const list = variableFormList(resources, [], variableListItems,
displayGroups, variableType);
/** Variable name. */
const { label } = celeryNode.args;
const headerForm = allowedVariableNodes === AllowedVariableNodes.parameter;
if (headerForm) {
list.unshift({
value: label,
label: determineVarDDILabel({
label, resources, uuid: sequenceUuid, forceExternal: true,
}),
headingId: "Variable",
});
}
if (variable.isDefault && variableType != VariableType.Resource) {
const defaultDDI = determineDropdown(variable.celeryNode, resources);
defaultDDI.label = addDefaultTextToLabel(defaultDDI.label);
list.unshift(defaultDDI);
}
const metaVariable =
maybeFindVariable(celeryNode.args.label, resources, sequenceUuid);
const usingDefaultValue = celeryNode.kind == "parameter_application" &&
metaVariable?.celeryNode.kind == "parameter_declaration" &&
JSON.stringify(celeryNode.args.data_value, jsonReplacer) ==
JSON.stringify(metaVariable.celeryNode.args.default_value, jsonReplacer);
const isDefaultValueForm =
!!props.locationDropdownKey?.endsWith("default_value");
if (variableType == VariableType.Resource) {
if (isDefaultValueForm) {
[
{ label: t("Sequence"), value: "Sequence", headingId: "Resource" },
{ label: t("Peripheral"), value: "Peripheral", headingId: "Resource" },
{ label: t("Sensor"), value: "Sensor", headingId: "Resource" },
].map(item => list.unshift(item));
} else if (celeryNode.kind != "parameter_declaration") {
if (variable.celeryNode.kind == "parameter_application") {
const resourceType = (variable.celeryNode.args.data_value as Resource)
.args.resource_type;
resourceType == "Sequence" && heading("Sequence")
.concat(sequences2Ddi(selectAllSequences(resources)))
.map(item => list.push(item));
resourceType == "Peripheral" && [PERIPHERAL_HEADING()]
.concat(peripherals2Ddi(selectAllPeripherals(resources)))
.map(item => list.push(item));
resourceType == "Sensor" && [SENSOR_HEADING()]
.concat(sensors2Ddi(selectAllSensors(resources)))
.map(item => list.push(item));
}
}
}
if (variableType == VariableType.Location && isDefaultValueForm) {
list.unshift(LOCATION_PLACEHOLDER_DDI());
}
const narrowLabel = !!removeVariable;
return <div className={"location-form"}>
<div className={"location-form-content"}>
<Row>
{!props.hideWrapper &&
<Col xs={1}>
{!isDefaultValueForm &&
<VariableIcon variableType={variableType} />}
</Col>}
{!props.hideWrapper &&
<Col xs={narrowLabel ? 4 : 5}>
{isDefaultValueForm
? <p>{t("Default value")}</p>
: <Label label={label} inUse={props.inUse || !removeVariable}
allowedVariableNodes={allowedVariableNodes}
labelOnly={props.labelOnly}
variable={variable} onChange={onChange} />}
{isDefaultValueForm &&
<Help text={ToolTips.DEFAULT_VALUE} position={Position.TOP_LEFT} />}
{isDefaultValueForm && isDefault &&
<Help text={ToolTips.USING_DEFAULT_VARIABLE_VALUE}
customIcon={"exclamation-triangle"} onHover={true} />}
</Col>}
{([VariableType.Location, VariableType.Resource]
.includes(variableType)
|| !isDefaultValueForm) &&
<Col xs={props.hideWrapper ? 12 : 6}>
<FBSelect
key={props.locationDropdownKey}
list={list}
selectedItem={dropdown}
customNullLabel={isDefaultValueForm
? LOCATION_PLACEHOLDER_DDI().label
: NO_VALUE_SELECTED_DDI().label}
onChange={ddi => {
onChange(convertDDItoVariable({
identifierLabel: label,
allowedVariableNodes,
dropdown: ddi,
variableType,
}), label);
}} />
</Col>}
{variableType == VariableType.Number && isDefaultValueForm &&
<NumericInput label={label} variableNode={variable.celeryNode}
onChange={onChange} isDefaultValueForm={isDefaultValueForm} />}
{variableType == VariableType.Text && isDefaultValueForm &&
<TextInput label={label} variableNode={variable.celeryNode}
onChange={onChange} isDefaultValueForm={isDefaultValueForm} />}
{removeVariable && !isDefaultValueForm &&
<Col xs={1} className={"trash"}>
<i className={"fa fa-trash"}
style={props.inUse ? { color: Color.gray } : {}}
onClick={() => removeVariable(label)} />
</Col>}
</Row>
{!isDefaultValueForm && variableType == VariableType.Number &&
celeryNode.kind != "parameter_declaration" &&
!usingDefaultValue && celeryNode.args.data_value.kind != "identifier" &&
<Row>
<Col xs={narrowLabel ? 5 : 6} />
<NumericInput label={label} variableNode={celeryNode}
onChange={onChange} isDefaultValueForm={isDefaultValueForm} />
</Row>}
{!isDefaultValueForm && variableType == VariableType.Text &&
celeryNode.kind != "parameter_declaration" &&
!usingDefaultValue && celeryNode.args.data_value.kind != "identifier" &&
<Row>
<Col xs={narrowLabel ? 5 : 6} />
<TextInput label={label} variableNode={celeryNode}
onChange={onChange} isDefaultValueForm={isDefaultValueForm} />
</Row>}
<CoordinateInputBoxes
variableNode={celeryNode}
vector={vector}
hideWrapper={!!props.hideWrapper}
narrowLabel={narrowLabel}
onChange={onChange} />
<DefaultValueForm
key={props.locationDropdownKey}
variableNode={celeryNode}
resources={resources}
removeVariable={removeVariable}
onChange={onChange} />
</div>
</div>;
};
const addDefaultTextToLabel = (label: string) => `${t("Default value")} - ${label}`;
export interface NumericInputProps {
variableNode: VariableNode;
onChange: OnChange;
label: string;
isDefaultValueForm: boolean;
}
export const NumericInput = (props: NumericInputProps) => {
const { variableNode } = props;
const isPlaceholder = variableNode.kind == "parameter_application"
&& variableNode.args.data_value.kind == "number_placeholder";
const argsValue = variableNode.kind == "parameter_declaration"
? (variableNode.args.default_value as Numeric).args.number
: (variableNode.args.data_value as Numeric).args.number;
return <Col xs={6} className={"numeric-variable-input"}>
<BlurableInput type={isPlaceholder ? "text" : "number"}
className={"number-input"}
clearBtn={props.isDefaultValueForm}
disabled={isPlaceholder}
keyCallback={(key, _buffer) => {
if (key || !props.isDefaultValueForm) { return; }
const editableVariable = defensiveClone(variableNode);
if (editableVariable.kind == "parameter_declaration") {
if (editableVariable.args.default_value.kind == "numeric") {
editableVariable.args.default_value =
{ kind: "number_placeholder", args: {} };
} else {
editableVariable.args.default_value =
{ kind: "numeric", args: { number: 0 } };
}
} else {
if (editableVariable.args.data_value.kind == "numeric") {
editableVariable.args.data_value =
{ kind: "number_placeholder", args: {} };
} else {
editableVariable.args.data_value =
{ kind: "numeric", args: { number: 0 } };
}
}
props.onChange(editableVariable, props.label);
}}
onCommit={e => {
if (isPlaceholder) { return; }
const editableVariable = defensiveClone(variableNode);
const value = parseFloat(e.currentTarget.value);
if (editableVariable.kind == "parameter_declaration") {
(editableVariable.args.default_value as Numeric).args.number = value;
} else {
(editableVariable.args.data_value as Numeric).args.number = value;
}
props.onChange(editableVariable, props.label);
}}
value={isPlaceholder ? t("None") : argsValue} />
</Col>;
};
export interface TextInputProps {
variableNode: VariableNode;
onChange: OnChange;
label: string;
isDefaultValueForm: boolean;
}
export const TextInput = (props: TextInputProps) => {
const { variableNode } = props;
const isPlaceholder = variableNode.kind == "parameter_application"
&& variableNode.args.data_value.kind == "text_placeholder";
const argsValue = variableNode.kind == "parameter_declaration"
? (variableNode.args.default_value as Text).args.string
: (variableNode.args.data_value as Text).args.string;
return <Col xs={6} className={"text-variable-input"}>
<BlurableInput type={"text"}
className={"string-input"}
clearBtn={props.isDefaultValueForm}
disabled={isPlaceholder}
keyCallback={(key, _buffer) => {
if (key || !props.isDefaultValueForm) { return; }
const editableVariable = defensiveClone(variableNode);
if (editableVariable.kind == "parameter_declaration") {
if (editableVariable.args.default_value.kind == "text") {
editableVariable.args.default_value =
{ kind: "text_placeholder", args: {} };
} else {
editableVariable.args.default_value =
{ kind: "text", args: { string: "" } };
}
} else {
if (editableVariable.args.data_value.kind == "text") {
editableVariable.args.data_value =
{ kind: "text_placeholder", args: {} };
} else {
editableVariable.args.data_value =
{ kind: "text", args: { string: "" } };
}
}
props.onChange(editableVariable, props.label);
}}
onCommit={e => {
if (isPlaceholder) { return; }
const editableVariable = defensiveClone(variableNode);
const value = e.currentTarget.value;
if (editableVariable.kind == "parameter_declaration") {
(editableVariable.args.default_value as Text).args.string = value;
} else {
(editableVariable.args.data_value as Text).args.string = value;
}
props.onChange(editableVariable, props.label);
}}
value={isPlaceholder ? t("None") : argsValue} />
</Col>;
};
export interface LabelProps {
label: string;
inUse: boolean | undefined;
variable: SequenceMeta;
onChange: OnChange;
allowedVariableNodes: AllowedVariableNodes;
labelOnly?: boolean;
}
interface LabelState {
labelValue: string;
}
export class Label extends React.Component<LabelProps, LabelState> {
state: LabelState = { labelValue: this.props.label };
setLabelValue = (e: React.FormEvent<HTMLInputElement>) =>
this.setState({ labelValue: e.currentTarget.value });
UneditableLabel = () => {
const { labelValue } = this.state;
const { allowedVariableNodes } = this.props;
const value = labelValue == "parent" ? t("Location") : labelValue;
return (allowedVariableNodes == AllowedVariableNodes.parameter
&& !this.props.labelOnly)
? <input
style={{ background: Color.lightGray }}
value={value}
readOnly={true}
onClick={() => error(t("Can't edit variable name while in use."))} />
: <p className={"variable-label"}>{value}</p>;
};
render() {
const { labelValue } = this.state;
const { label, inUse, variable, onChange } = this.props;
return !inUse
? <input value={labelValue}
onBlur={() => {
const editableVariable = cloneDeep(variable.celeryNode);
if (editableVariable.args.label != labelValue) {
editableVariable.args.label = labelValue;
onChange(editableVariable, label);
}
}}
onChange={this.setLabelValue} />
: <this.UneditableLabel />;
}
}
interface GenerateVariableListProps {
allowedVariableNodes: AllowedVariableNodes;
resources: ResourceIndex;
sequenceUuid: UUID;
headingId?: string;
variableType: VariableType;
}
export const generateVariableListItems = (props: GenerateVariableListProps) => {
const { allowedVariableNodes, resources, sequenceUuid } = props;
const headingId = props.headingId || "Variable";
const variables = sortVariables(Object.values(
resources.sequenceMetas[sequenceUuid] || [])).map(v => v.celeryNode);
const displayVariables = allowedVariableNodes !== AllowedVariableNodes.variable;
if (!displayVariables) { return []; }
const headerForm = allowedVariableNodes === AllowedVariableNodes.parameter;
if (headerForm) { return []; }
const oldVariables = variables
.filter(v => determineVariableType(v) == props.variableType)
.map(variable_ => ({
value: variable_.args.label,
label: determineVarDDILabel({
label: variable_.args.label,
resources,
uuid: sequenceUuid,
}),
headingId,
}));
const newVarLabel = generateNewVariableLabel(variables,
newVariableLabel(props.variableType));
const newVariable = [{
value: newVarLabel,
label: determineVarDDILabel({
label: newVarLabel,
resources,
uuid: sequenceUuid,
}),
headingId,
}];
return oldVariables.concat(newVariable);
};