125 lines
2.8 KiB
JavaScript
125 lines
2.8 KiB
JavaScript
export const SELF_SERVE_TASK_BUTTON_RESET = "reset";
|
|
export const SELF_SERVE_TASK_BUTTON_START = "start";
|
|
export const SELF_SERVE_TASK_BUTTON_PROGRAM_PICKER = "program_picker";
|
|
|
|
export const SELF_SERVE_TASK_BUTTON_OPTIONS = [
|
|
{
|
|
id: SELF_SERVE_TASK_BUTTON_RESET,
|
|
name: "Reset",
|
|
description: "Reset button",
|
|
},
|
|
{
|
|
id: SELF_SERVE_TASK_BUTTON_PROGRAM_PICKER,
|
|
name: "Program picker",
|
|
description: "Program picker wheel",
|
|
},
|
|
...Array.from({ length: 12 }, (_, index) => ({
|
|
id: index,
|
|
name: `Program ${index + 1}`,
|
|
description: `Machine button ${index}`,
|
|
})),
|
|
{
|
|
id: SELF_SERVE_TASK_BUTTON_START,
|
|
name: "Start",
|
|
description: "Start button",
|
|
},
|
|
];
|
|
|
|
const KNOWN_SPECIAL_BUTTONS = new Set([
|
|
SELF_SERVE_TASK_BUTTON_RESET,
|
|
SELF_SERVE_TASK_BUTTON_START,
|
|
SELF_SERVE_TASK_BUTTON_PROGRAM_PICKER,
|
|
]);
|
|
|
|
const BUTTON_LABELS = new Map(
|
|
SELF_SERVE_TASK_BUTTON_OPTIONS.map((option) => [String(option.id), option.name])
|
|
);
|
|
|
|
const normalizeListInput = (value) => {
|
|
if (Array.isArray(value)) {
|
|
return value;
|
|
}
|
|
|
|
if (typeof value === "string") {
|
|
const trimmed = value.trim();
|
|
if (!trimmed) {
|
|
return [];
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(trimmed);
|
|
if (Array.isArray(parsed)) {
|
|
return normalizeListInput(parsed);
|
|
}
|
|
} catch {
|
|
// Comma-separated values are accepted for quick manual edits.
|
|
}
|
|
|
|
return trimmed.split(",").map((entry) => entry.trim()).filter(Boolean);
|
|
}
|
|
|
|
return [];
|
|
};
|
|
|
|
export const normalizeSelfServeTaskButton = (value) => {
|
|
if (value === null || value === undefined) {
|
|
return null;
|
|
}
|
|
|
|
const text = String(value).trim();
|
|
if (!text) {
|
|
return null;
|
|
}
|
|
|
|
const lowerText = text.toLowerCase();
|
|
if (KNOWN_SPECIAL_BUTTONS.has(lowerText)) {
|
|
return lowerText;
|
|
}
|
|
|
|
const parsed = Number(text);
|
|
if (!Number.isInteger(parsed) || parsed < 0 || parsed > 11) {
|
|
return null;
|
|
}
|
|
|
|
return parsed;
|
|
};
|
|
|
|
export const normalizeSelfServeTaskButtons = (value) => {
|
|
const normalized = [];
|
|
const seen = new Set();
|
|
|
|
for (const entry of normalizeListInput(value)) {
|
|
const button = normalizeSelfServeTaskButton(entry);
|
|
if (button === null) {
|
|
continue;
|
|
}
|
|
|
|
const key = `${typeof button}:${button}`;
|
|
if (seen.has(key)) {
|
|
continue;
|
|
}
|
|
|
|
seen.add(key);
|
|
normalized.push(button);
|
|
}
|
|
|
|
return normalized;
|
|
};
|
|
|
|
export const formatSelfServeTaskButton = (button) => {
|
|
const normalized = normalizeSelfServeTaskButton(button);
|
|
if (normalized === null) {
|
|
return null;
|
|
}
|
|
|
|
return BUTTON_LABELS.get(String(normalized)) || `Button ${normalized}`;
|
|
};
|
|
|
|
export const formatSelfServeTaskButtons = (buttons, emptyLabel = "Ingen") => {
|
|
const labels = normalizeSelfServeTaskButtons(buttons)
|
|
.map(formatSelfServeTaskButton)
|
|
.filter(Boolean);
|
|
|
|
return labels.length > 0 ? labels.join(", ") : emptyLabel;
|
|
};
|