add default value to dropdown

pull/1242/head
gabrielburnworth 2019-06-21 14:46:11 -07:00
parent 4c552a1a7e
commit 38ddb3db6c
8 changed files with 48 additions and 13 deletions

View File

@ -41,11 +41,11 @@ describe("determineDropdown", () => {
kind: "parameter_application",
args: {
label: "x",
data_value: { kind: "coordinate", args: { x: 0, y: 1, z: 2 } }
data_value: { kind: "coordinate", args: { x: 0, y: 1.1, z: 2 } }
}
}, buildResourceIndex([]).index);
expect(r.label).toBe("Coordinate (0, 1, 2)");
expect(r.value).toBe("?");
expect(r.label).toBe("Coordinate (0, 1.1, 2)");
expect(r.value).toBe("{\"x\":0,\"y\":1.1,\"z\":2}");
});
it("Returns a label for `identifier`", () => {

View File

@ -9,7 +9,8 @@ import { findPointerByTypeAndId } from "./selectors";
import { findSlotByToolId, findToolById } from "./selectors_by_id";
import {
formatPoint, safeEveryPointType, everyPointDDI, NO_VALUE_SELECTED_DDI,
formatTool
formatTool,
COORDINATE_DDI
} from "../sequences/locals_list/location_form_list";
import { VariableNode } from "../sequences/locals_list/locals_list_support";
import { EveryPointShape } from "../sequences/locals_list/handle_select";
@ -19,6 +20,7 @@ export interface SequenceMeta {
celeryNode: VariableNode;
dropdown: DropDownItem;
vector: Vector3 | undefined;
default?: boolean;
}
/** Converts a "scope declaration body item" (AKA a CeleryScript variable) into
@ -95,8 +97,7 @@ export const determineDropdown =
const { data_value } = node.args;
switch (data_value.kind) {
case "coordinate":
const { x, y, z } = data_value.args;
return { label: `Coordinate (${x}, ${y}, ${z})`, value: "?" };
return COORDINATE_DDI(data_value.args);
case "identifier":
const { label } = data_value.args;
const varName = determineVarDDILabel({ label, resources, uuid });

View File

@ -45,7 +45,25 @@ describe("convertDDItoDeclaration()", () => {
expect(variable).toEqual(expectedVariable(NOTHING_SELECTED));
});
it("returns variable declaration: default", () => {
const expected = expectedVariable(NOTHING_SELECTED);
expected.kind = "variable_declaration";
const variable = convertDDItoVariable({
label, allowedVariableNodes: AllowedVariableNodes.parameter
})(NO_VALUE_SELECTED_DDI());
expect(variable).toEqual(expected);
});
it("returns location data: coordinate", () => {
const variable = convertDDItoVariable({
label, allowedVariableNodes
})(COORDINATE_DDI({ x: 1, y: 2, z: 3 }));
expect(variable).toEqual(expectedVariable({
kind: "coordinate", args: { x: 1, y: 2, z: 3 }
}));
});
it("returns location data: new coordinate", () => {
const variable = convertDDItoVariable({
label, allowedVariableNodes
})(COORDINATE_DDI());

View File

@ -120,7 +120,11 @@ describe("getRegimenVariableData()", () => {
},
parent2: {
celeryNode: varDeclaration,
dropdown: { label: "Coordinate (1, 2, 3)", value: "?" },
dropdown: {
label: "Coordinate (1, 2, 3)",
value: expect.any(String),
headingId: "Coordinate",
},
vector: COORDINATE.args
}
});

View File

@ -92,10 +92,11 @@ const everyPointVar = (value: string | number) =>
args: { every_point_type: "" + value as PointType }
});
const manualEntry =
const manualEntry = (value: string | number) =>
({ label, allowedVariableNodes }: NewVarProps): VariableWithAValue =>
createVariableNode(allowedVariableNodes)(label, {
kind: "coordinate", args: { x: 0, y: 0, z: 0 }
kind: "coordinate",
args: value ? JSON.parse("" + value) : { x: 0, y: 0, z: 0 }
});
interface NewVariableProps extends NewVarProps {
@ -128,7 +129,7 @@ const newVariableCreator = (ddi: DropDownItem):
case "Tool": return toolVar(ddi.value);
case "parameter": return newParameter; // Caller decides X/Y/Z
case "every_point": return everyPointVar(ddi.value);
case "Coordinate": return manualEntry;
case "Coordinate": return manualEntry(ddi.value);
}
return () => undefined;
};

View File

@ -78,6 +78,7 @@ const convertFormVariable = (variable: SequenceMeta, resources: ResourceIndex):
celeryNode: converted,
dropdown: determineDropdown(converted, resources),
vector: determineVector(converted, resources),
default: true,
};
}
};

View File

@ -59,6 +59,11 @@ export const LocationForm =
const list = locationFormList(resources, variableListItems, displayGroups);
/** Variable name. */
const { label } = celeryNode.args;
if (variable.default) {
const defaultDDI = determineDropdown(variable.celeryNode, resources);
defaultDDI.label = `${t("Default value")} - ${defaultDDI.label}`;
list.unshift(defaultDDI);
}
const formTitleWithType =
props.hideVariableLabel ? t("Location") : `${label} (${t("Location")})`;
const formTitle = props.hideTypeLabel ? label : formTitleWithType;

View File

@ -6,7 +6,7 @@ import {
} from "../../resources/selectors";
import { betterCompact } from "../../util";
import {
TaggedTool, TaggedPoint, TaggedToolSlotPointer, Xyz
TaggedTool, TaggedPoint, TaggedToolSlotPointer, Xyz, Vector3
} from "farmbot";
import { DropDownItem } from "../../ui";
import { capitalize, isNumber } from "lodash";
@ -138,8 +138,13 @@ export const safeEveryPointType = (x: string): EveryPointType => {
export const everyPointDDI = (value: EveryPointType): DropDownItem =>
({ value, label: t(EVERY_POINT_LABEL[value]), headingId: "every_point" });
export const COORDINATE_DDI = (): DropDownItem =>
({ value: "", label: t("Custom Coordinates"), headingId: "Coordinate" });
export const COORDINATE_DDI = (vector?: Vector3): DropDownItem => ({
label: vector
? `${t("Coordinate")} (${vector.x}, ${vector.y}, ${vector.z})`
: t("Custom Coordinates"),
value: vector ? JSON.stringify(vector) : "",
headingId: "Coordinate"
});
export const NO_VALUE_SELECTED_DDI = (): DropDownItem =>
({ label: t("Select a location"), value: "", isNull: true });