Farmbot-Web-App/frontend/sequences/locals_list/location_form.tsx

104 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-01-13 17:16:22 -07:00
import * as React from "react";
2019-06-06 17:54:48 -06:00
import { Row, Col, FBSelect, DropDownItem } from "../../ui";
2019-01-16 18:51:25 -07:00
import { locationFormList, NO_VALUE_SELECTED_DDI } from "./location_form_list";
2019-02-22 19:09:40 -07:00
import { convertDDItoVariable } from "../locals_list/handle_select";
2019-01-13 17:16:22 -07:00
import {
2019-02-22 19:09:40 -07:00
LocationFormProps, PARENT, AllowedVariableNodes, VariableNode,
2019-01-13 17:16:22 -07:00
} from "../locals_list/locals_list_support";
import {
2019-06-06 17:54:48 -06:00
determineVector, determineDropdown, SequenceMeta, determineVarDDILabel,
2019-01-13 17:16:22 -07:00
} from "../../resources/sequence_meta";
import { ResourceIndex, UUID } from "../../resources/interfaces";
import { Feature } from "../../devices/interfaces";
2019-02-22 19:09:40 -07:00
import { DefaultValueForm } from "./default_value_form";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2019-06-06 17:54:48 -06:00
import { CoordinateInputBoxes } from "./location_form_coordinate_input_boxes";
2019-01-13 17:16:22 -07:00
/**
2019-02-22 19:09:40 -07:00
* If a variable with a matching label exists in local parameter applications
2019-01-13 17:16:22 -07:00
* (step body, etc.), use it instead of the one in scope declarations.
*/
2019-02-22 19:09:40 -07:00
const maybeUseStepData = ({ resources, bodyVariables, variable, uuid }: {
2019-01-13 17:16:22 -07:00
resources: ResourceIndex,
2019-02-22 19:09:40 -07:00
bodyVariables: VariableNode[] | undefined,
2019-01-13 17:16:22 -07:00
variable: SequenceMeta,
uuid: UUID,
}): SequenceMeta => {
2019-02-22 19:09:40 -07:00
if (bodyVariables) {
const executeStepData = bodyVariables
2019-01-13 17:16:22 -07:00
.filter(v => v.args.label === variable.celeryNode.args.label)[0];
if (executeStepData) {
return {
celeryNode: executeStepData,
vector: determineVector(executeStepData, resources, uuid),
2019-06-04 16:07:52 -06:00
dropdown: determineDropdown(executeStepData, resources, uuid),
2019-01-13 17:16:22 -07:00
};
}
}
return variable;
};
2019-06-06 17:54:48 -06:00
/** Determine DropdownItem for the current LocationForm selection. */
2019-06-04 16:07:52 -06:00
const selectedLabelDDI = (ddi: DropDownItem, override?: string) => {
const newDDI = Object.assign({}, ddi);
newDDI.label = (ddi.value === "parameter_declaration" && override)
? override : newDDI.label;
return newDDI;
};
2019-01-13 17:16:22 -07:00
/**
2019-06-06 17:54:48 -06:00
* Form with an "import from" dropdown and coordinate input boxes.
2019-01-13 17:16:22 -07:00
* Can be used to set a specific value, import a value, or declare a variable.
*/
export const LocationForm =
(props: LocationFormProps) => {
2019-06-06 17:54:48 -06:00
const { sequenceUuid, resources, bodyVariables, variable,
allowedVariableNodes, disallowGroups } = props;
const { celeryNode, dropdown, vector } = maybeUseStepData({
resources, bodyVariables, variable, uuid: sequenceUuid
});
const displayVariables = props.shouldDisplay(Feature.variables) &&
allowedVariableNodes !== AllowedVariableNodes.variable;
const variableListItems = displayVariables ? [PARENT(props.listVarLabel ||
determineVarDDILabel("parent", resources, sequenceUuid))] : [];
2019-03-13 13:05:54 -06:00
const displayGroups = props.shouldDisplay(Feature.loops) && !disallowGroups;
const list = locationFormList(resources, variableListItems, displayGroups);
2019-01-13 17:16:22 -07:00
/** Variable name. */
const { label } = celeryNode.args;
2019-02-22 19:09:40 -07:00
const formTitleWithType =
props.hideVariableLabel ? t("Location") : `${label} (${t("Location")})`;
const formTitle = props.hideTypeLabel ? label : formTitleWithType;
2019-01-13 17:16:22 -07:00
return <div className="location-form">
2019-04-09 19:32:18 -06:00
<div className="location-form-header">
<label>{formTitle}</label>
{props.collapsible &&
<i className={`fa fa-caret-${props.collapsed ? "down" : "up"}`}
onClick={props.toggleVarShow} />}
</div>
{!props.collapsed &&
<div className="location-form-content">
<Row>
<Col xs={12}>
<FBSelect
2019-06-06 17:54:48 -06:00
key={props.locationDropdownKey}
2019-04-09 19:32:18 -06:00
list={list}
2019-06-06 17:54:48 -06:00
selectedItem={selectedLabelDDI(dropdown, props.listVarLabel)}
2019-04-09 19:32:18 -06:00
customNullLabel={NO_VALUE_SELECTED_DDI().label}
2019-06-06 17:54:48 -06:00
onChange={ddi => props.onChange(convertDDItoVariable({
2019-04-09 19:32:18 -06:00
label, allowedVariableNodes
})(ddi))} />
</Col>
</Row>
2019-06-06 17:54:48 -06:00
<CoordinateInputBoxes
variableNode={celeryNode}
vector={vector}
width={props.width}
onChange={props.onChange} />
2019-04-09 19:32:18 -06:00
<DefaultValueForm
variableNode={celeryNode}
resources={resources}
2019-06-06 17:54:48 -06:00
onChange={props.onChange} />
2019-04-09 19:32:18 -06:00
</div>}
2019-01-13 17:16:22 -07:00
</div>;
};