(function () {
function escapeHtml(str) {
return String(str === null || str === undefined ? "" : str)
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function isEmpty(v) {
return v === null || v === undefined || String(v).trim() === "";
}
function valueOrDash(v) {
return isEmpty(v) ? "—" : escapeHtml(v);
}
// Traduce il valore salvato (es. "every_2h") nell'etichetta del menu a tendina,
// usando le etichette di ws_lookup_options restituite dall'endpoint.
// Se l'etichetta non c'è, mostra il valore così com'è.
function lookupLabel(lookups, category, value) {
if (isEmpty(value)) return "";
const map = lookups && lookups[category];
if (map && map[String(value)] !== undefined) return map[String(value)];
return value;
}
// Campo in sola lettura: etichetta piccola sopra, valore in evidenza sotto
function wsdField(label, value, extraClass) {
const empty = isEmpty(value);
return `
${escapeHtml(label)}
${valueOrDash(value)}
`;
}
// Blocco di testo lungo (note, misure di controllo)
function wsdText(label, value, emptyText) {
const empty = isEmpty(value);
return `
',
);
});
};
// ================================================================
// Tab allegati della matrice: disegni tecnici, strumenti, cicli
// ================================================================
const WS_ATT_KINDS = {
disegni: {
flag: "is_disegno",
label: "Disegni tecnici",
icon: "📐",
itemLabel: "Disegno",
empty:
"Nessun disegno tecnico per questo profilo. " +
"Si possono segnare dalla gestione matrici (graffetta → pulsante compasso).",
},
strumenti: {
flag: "is_strumento",
label: "Strumenti di lavoro",
icon: "🔧",
itemLabel: "Strumento",
empty:
"Nessuno strumento di lavoro per questo profilo. " +
"Si possono segnare dalla gestione matrici (graffetta → pulsante chiave inglese).",
},
cicli: {
flag: "is_ciclo",
label: "Cicli di lavoro",
icon: "🔁",
itemLabel: "Ciclo",
empty:
"Nessun ciclo di lavoro per questo profilo. " +
"Si possono segnare dalla gestione matrici (graffetta → pulsante frecce).",
},
};
let wsAttLists = { disegni: [], strumenti: [], cicli: [] };
function wsFileExt(name) {
const p = String(name || "").split(".");
return p.length > 1 ? p.pop().toLowerCase() : "";
}
function wsIsImage(ext) {
return ["jpg", "jpeg", "png", "gif", "bmp", "webp"].includes(ext);
}
function showWorksheetAttachment(kind, index) {
const list = wsAttLists[kind] || [];
const f = list[index];
if (!f) return;
$(`.ws-disegno-item[data-kind="${kind}"]`).removeClass("active");
$(
`.ws-disegno-item[data-kind="${kind}"][data-index="${index}"]`,
).addClass("active");
const name = f.file_name || "";
const path = f.file_url || f.file_path || "";
const ext = wsFileExt(name);
let viewer;
if (ext === "pdf") {
viewer = ``;
} else if (wsIsImage(ext)) {
viewer = `
`;
} else {
viewer = `
Anteprima non disponibile per questo formato: usa "Apri in nuova scheda"
`;
}
$(`#wsToolbar_${kind}`).html(`
${escapeHtml(name)}
${f.description ? `
${escapeHtml(f.description)}
` : ""}
Apri in nuova scheda
`);
$(`#wsViewer_${kind}`).html(viewer);
}
function renderWorksheetAttachmentKind(kind) {
const cfg = WS_ATT_KINDS[kind];
const list = wsAttLists[kind];
const $c = $(`#wsContainer_${kind}`);
$(`#wsCount_${kind}`).text(list.length);
if (!list.length) {
$c.html(
`
${escapeHtml(cfg.empty)}
`,
);
return;
}
let listHtml = "";
if (list.length > 1) {
listHtml =
'
' +
list
.map(
(f, i) =>
``,
)
.join("") +
"
";
}
$c.html(`
${listHtml}
`);
// Se l'utente ha già aperto il tab mentre caricava, mostro subito il primo
if ($(`.ws-tab[data-tab="${kind}"]`).hasClass("active")) {
showWorksheetAttachment(kind, 0);
}
}
function loadWorksheetAttachments(idmatrice) {
wsAttLists = { disegni: [], strumenti: [], cicli: [] };
const kinds = Object.keys(WS_ATT_KINDS);
if (!idmatrice) {
kinds.forEach((k) => {
$(`#wsCount_${k}`).text("0");
$(`#wsContainer_${k}`).html(
'
${wsdField("Metri per confezione", ws.meters_per_package)}
${wsdField("Tolleranza metri/conf.", ws.meters_per_package_tolerance)}
${wsdField("Conf./pezzi per scatola", ws.packages_or_pieces_per_box)}
${wsdField("Metri per scatola", ws.meters_per_box)}
${wsdField("Scatole/conf. per bancale", ws.boxes_or_packages_per_pallet)}