Farmbot-Web-App/frontend/sequences/locals_list/location_form_list.ts

143 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-01-13 17:16:22 -07:00
import { ResourceIndex } from "../../resources/interfaces";
2017-06-29 12:54:02 -06:00
import {
2018-09-20 13:59:13 -06:00
selectAllToolSlotPointers,
selectAllActivePoints
2019-01-13 17:16:22 -07:00
} from "../../resources/selectors";
import { betterCompact } from "../../util";
2019-03-13 13:05:54 -06:00
import { TaggedTool, TaggedPoint, Vector3 } from "farmbot";
2019-01-13 17:16:22 -07:00
import { DropDownItem } from "../../ui";
2019-04-02 13:59:37 -06:00
2019-02-06 18:36:11 -07:00
import { capitalize } from "lodash";
2019-01-13 17:16:22 -07:00
import { joinKindAndId } from "../../resources/reducer_support";
import { Point } from "farmbot/dist/resources/api_resources";
2019-04-02 13:59:37 -06:00
import { t } from "../../i18next_wrapper";
2019-01-13 17:16:22 -07:00
const TOOL: "Tool" = "Tool";
2018-12-20 20:18:10 -07:00
type ToolAndLocation = { tool: TaggedTool, location: Vector3 };
/** Return tool and location for all tools currently in tool slots. */
export function activeTools(resources: ResourceIndex): ToolAndLocation[] {
const Tool: TaggedTool["kind"] = "Tool";
const slots = selectAllToolSlotPointers(resources);
const { byKindAndId, references } = resources;
return betterCompact(slots
2018-12-20 20:18:10 -07:00
.map(x => ({
tool: references[byKindAndId[joinKindAndId(Tool, x.body.tool_id)] || ""],
location: { x: x.body.x, y: x.body.y, z: x.body.z }
}))
.map(({ tool, location }) => (tool && tool.kind === "Tool")
? { tool, location }
: undefined));
}
2017-06-29 12:54:02 -06:00
2019-01-13 17:16:22 -07:00
type PointerTypeName = Point["pointer_type"];
2019-01-13 16:43:12 -07:00
type DropdownHeadingId = PointerTypeName | typeof TOOL | "Other";
2018-12-20 20:18:10 -07:00
/** Location selection menu section names. */
export const NAME_MAP: Record<DropdownHeadingId, string> = {
"GenericPointer": "Map Points",
"Plant": "Plants",
"ToolSlot": "Tool Slots",
"Tool": "Tools",
2019-01-13 16:43:12 -07:00
"Other": "Other",
};
2019-02-06 18:36:11 -07:00
const heading = (name: DropdownHeadingId): DropDownItem[] => ([{
2019-01-13 16:43:12 -07:00
label: t(NAME_MAP[name]),
heading: true,
value: 0,
headingId: name
2019-02-06 18:36:11 -07:00
}]);
2019-01-13 16:43:12 -07:00
const ddiFrom = (points: TaggedPoint[], pointerType: PointerTypeName) => points
.filter(x => x.body.pointer_type === pointerType)
.map(formatPoint)
.filter(x => parseInt("" + x.value) > 0);
const maybeGroup = (display: boolean) =>
(groupDDI: DropDownItem): DropDownItem[] =>
display ? [groupDDI] : [];
2018-12-20 20:18:10 -07:00
/** Location selection menu items. */
2019-01-16 19:24:59 -07:00
export function locationFormList(resources: ResourceIndex,
2019-01-13 16:43:12 -07:00
additionalItems: DropDownItem[], displayGroups?: boolean): DropDownItem[] {
2019-01-16 19:24:59 -07:00
const points = selectAllActivePoints(resources)
2019-01-13 16:43:12 -07:00
.filter(x => x.body.pointer_type !== "ToolSlot");
const plantDDI = ddiFrom(points, "Plant");
const genericPointerDDI = ddiFrom(points, "GenericPointer");
2019-01-16 19:24:59 -07:00
const toolDDI: DropDownItem[] = activeTools(resources)
2019-01-13 16:43:12 -07:00
.map(({ tool, location }) => formatTools(tool, location))
.filter(x => parseInt("" + x.value) > 0);
const group = maybeGroup(!!displayGroups);
2019-02-06 18:36:11 -07:00
return heading("Tool")
2017-07-06 10:42:33 -06:00
.concat(toolDDI)
2019-01-13 16:43:12 -07:00
.concat(group(everyPointDDI("Tool")))
.concat(group(everyPointDDI("ToolSlot")))
.concat(heading("Plant"))
.concat(plantDDI)
.concat(group(everyPointDDI("Plant")))
.concat(heading("GenericPointer"))
.concat(genericPointerDDI)
.concat(group(everyPointDDI("GenericPointer")))
.concat(heading("Other"))
.concat(additionalItems)
2019-02-06 18:36:11 -07:00
.concat(COORDINATE_DDI());
2017-06-29 12:54:02 -06:00
}
2018-12-20 20:18:10 -07:00
/** Create drop down item with label; i.e., "Point/Plant (1, 2, 3)" */
export const formatPoint = (p: TaggedPoint): DropDownItem => {
const { id, name, pointer_type, x, y, z } = p.body;
return {
label: dropDownName(name, { x, y, z }),
value: "" + id,
2018-11-30 12:31:10 -07:00
headingId: pointer_type
};
};
2017-06-29 12:54:02 -06:00
2018-12-20 20:18:10 -07:00
/** Create drop down item with label; i.e., "Tool (1, 2, 3)" */
const formatTools = (tool: TaggedTool, v: Vector3): DropDownItem => {
const { id, name } = tool.body;
2017-06-29 12:54:02 -06:00
return {
2018-12-20 20:18:10 -07:00
label: dropDownName((name || "untitled"), v),
2017-06-29 12:54:02 -06:00
value: "" + id,
headingId: TOOL
};
};
2017-06-29 12:54:02 -06:00
/** Uniformly generate a label for things that have an X/Y/Z value. */
export function dropDownName(name: string, v?: Vector3) {
let label = name || "untitled";
if (v) { label += ` (${v.x}, ${v.y}, ${v.z})`; }
return capitalize(label);
2017-06-29 12:54:02 -06:00
}
2019-01-13 16:43:12 -07:00
export const EVERY_POINT_LABEL = {
"Plant": "All plants",
"GenericPointer": "All map points",
"Tool": "All tools",
"ToolSlot": "All tool slots",
};
export type EveryPointType = keyof typeof EVERY_POINT_LABEL;
const isEveryPointType = (x: string): x is EveryPointType =>
Object.keys(EVERY_POINT_LABEL).includes(x);
export const safeEveryPointType = (x: string): EveryPointType => {
if (isEveryPointType(x)) {
return x;
} else {
throw new Error(`'${x}' is not of type EveryPointType`);
}
};
2019-01-16 18:51:25 -07:00
export const everyPointDDI = (value: EveryPointType): DropDownItem =>
2019-01-16 19:24:59 -07:00
({ value, label: t(EVERY_POINT_LABEL[value]), headingId: "every_point" });
2019-01-16 18:51:25 -07:00
2019-02-22 19:09:40 -07:00
export const COORDINATE_DDI = (): DropDownItem =>
2019-01-16 18:51:25 -07:00
({ value: "", label: t("Coordinate"), headingId: "Coordinate" });
2019-01-16 19:24:59 -07:00
export const NO_VALUE_SELECTED_DDI = (): DropDownItem =>
({ label: t("Select a location"), value: "", isNull: true });