Add interactive label calibration UI
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
/* Shared label field calibration — derived from
|
||||
* ETICHETTE PER INKS(...).xlsx.pdf (fit-to-page A4 print).
|
||||
* Positions are mm from the top-left of each slip quadrant.
|
||||
* Font sizes are effective printed sizes (Excel 15/13pt × ~0.46 scale).
|
||||
/* Shared label field layout — positions from CSS vars
|
||||
* (defaults here; calibrate UI / data/calibration.json override via :root).
|
||||
*/
|
||||
.var-field {
|
||||
position: absolute;
|
||||
@@ -11,27 +9,27 @@
|
||||
}
|
||||
|
||||
.val-batch {
|
||||
font-size: 7pt;
|
||||
top: 63mm; /* +2mm vs prior top-row calibration */
|
||||
left: 30.2mm;
|
||||
font-size: var(--batch-font, 7pt);
|
||||
top: var(--batch-top, 63mm);
|
||||
left: var(--batch-left, 30.2mm);
|
||||
}
|
||||
|
||||
.val-date {
|
||||
font-size: 6pt;
|
||||
top: 63mm; /* +2mm vs prior top-row calibration */
|
||||
left: 67.4mm;
|
||||
font-size: var(--date-font, 6pt);
|
||||
top: var(--date-top, 63mm);
|
||||
left: var(--date-left, 67.4mm);
|
||||
}
|
||||
|
||||
.val-next {
|
||||
font-size: 7pt;
|
||||
top: 68.5mm; /* +2mm vs prior top-row calibration */
|
||||
left: 84.2mm;
|
||||
font-size: var(--next-font, 7pt);
|
||||
top: var(--next-top, 68.5mm);
|
||||
left: var(--next-left, 84.2mm);
|
||||
}
|
||||
|
||||
.val-weight {
|
||||
font-size: 7pt;
|
||||
top: 68.5mm; /* +2mm vs prior top-row calibration */
|
||||
left: 37.3mm;
|
||||
font-size: var(--weight-font, 7pt);
|
||||
top: var(--weight-top, 68.5mm);
|
||||
left: var(--weight-left, 37.3mm);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -39,29 +37,29 @@
|
||||
* matching fields is ~118.8mm (not 148.5mm). Shift bottom slips up.
|
||||
* Quadrant order in the 2×2 grid: 1=TL, 2=TR, 3=BL, 4=BR.
|
||||
*/
|
||||
.slip-quadrant:nth-child(n + 3) .val-batch,
|
||||
.slip-quadrant:nth-child(n + 3) .val-date {
|
||||
top: 43.3mm; /* +12mm vs prior bottom-row calibration */
|
||||
}
|
||||
|
||||
.slip-quadrant:nth-child(n + 3) .val-next,
|
||||
.slip-quadrant:nth-child(n + 3) .val-weight {
|
||||
top: 48.8mm; /* +12mm vs prior bottom-row calibration */
|
||||
.slip-quadrant:nth-child(n + 3) .val-batch {
|
||||
top: var(--batch-bottom-top, 43.3mm);
|
||||
}
|
||||
|
||||
.slip-quadrant:nth-child(n + 3) .val-date {
|
||||
left: 70.4mm; /* +3mm vs top-row date */
|
||||
top: var(--date-bottom-top, 43.3mm);
|
||||
left: var(--date-bottom-left, 70.4mm);
|
||||
}
|
||||
|
||||
.slip-quadrant:nth-child(n + 3) .val-next {
|
||||
left: 87.2mm; /* +3mm vs top-row next control */
|
||||
top: var(--next-bottom-top, 48.8mm);
|
||||
left: var(--next-bottom-left, 87.2mm);
|
||||
}
|
||||
|
||||
.slip-quadrant:nth-child(n + 3) .val-weight {
|
||||
top: var(--weight-bottom-top, 48.8mm);
|
||||
}
|
||||
|
||||
/* Right column (TR, BR): Batch# and Net weight */
|
||||
.slip-quadrant:nth-child(2n) .val-batch {
|
||||
left: 22.2mm; /* -8mm vs left-column batch */
|
||||
left: var(--batch-right-left, 22.2mm);
|
||||
}
|
||||
|
||||
.slip-quadrant:nth-child(2n) .val-weight {
|
||||
left: 29.3mm; /* -8mm vs left-column weight */
|
||||
left: var(--weight-right-left, 29.3mm);
|
||||
}
|
||||
|
||||
292
slips/static/slips/js/calibrate.js
Normal file
292
slips/static/slips/js/calibrate.js
Normal file
@@ -0,0 +1,292 @@
|
||||
(() => {
|
||||
const FIELDS = ['batch', 'date', 'next', 'weight'];
|
||||
const LABELS = { batch: 'Batch', date: 'Date', next: 'Next control', weight: 'Weight' };
|
||||
|
||||
const TOP_KEYS = [
|
||||
{ key: 'top', label: 'top' },
|
||||
{ key: 'left', label: 'left' },
|
||||
{ key: 'font', label: 'font pt' },
|
||||
];
|
||||
const BOTTOM_KEYS = {
|
||||
batch: [{ key: 'bottom_top', label: 'top' }],
|
||||
date: [
|
||||
{ key: 'bottom_top', label: 'top' },
|
||||
{ key: 'bottom_left', label: 'left' },
|
||||
],
|
||||
next: [
|
||||
{ key: 'bottom_top', label: 'top' },
|
||||
{ key: 'bottom_left', label: 'left' },
|
||||
],
|
||||
weight: [{ key: 'bottom_top', label: 'top' }],
|
||||
};
|
||||
const RIGHT_KEYS = {
|
||||
batch: [{ key: 'right_left', label: 'left' }],
|
||||
weight: [{ key: 'right_left', label: 'left' }],
|
||||
};
|
||||
|
||||
const dataEl = document.getElementById('calibration-data');
|
||||
let state = JSON.parse(dataEl.textContent);
|
||||
let saved = structuredClone(state);
|
||||
let selected = { field: 'batch', section: 'top' };
|
||||
|
||||
const statusEl = document.getElementById('status');
|
||||
const stepEl = document.getElementById('step');
|
||||
const varsEl = document.getElementById('calibration-vars');
|
||||
|
||||
function round1(n) {
|
||||
return Math.round(Number(n) * 10) / 10;
|
||||
}
|
||||
|
||||
function isDirty() {
|
||||
return JSON.stringify(state) !== JSON.stringify(saved);
|
||||
}
|
||||
|
||||
function setStatus(msg, kind) {
|
||||
statusEl.textContent = msg;
|
||||
statusEl.className = 'status' + (kind ? ' ' + kind : '');
|
||||
}
|
||||
|
||||
function cssFromState(cal) {
|
||||
const lines = [':root {'];
|
||||
for (const field of FIELDS) {
|
||||
const v = cal[field];
|
||||
lines.push(` --${field}-top: ${v.top}mm;`);
|
||||
lines.push(` --${field}-left: ${v.left}mm;`);
|
||||
lines.push(` --${field}-font: ${v.font}pt;`);
|
||||
lines.push(` --${field}-bottom-top: ${v.bottom_top}mm;`);
|
||||
if (v.bottom_left != null) lines.push(` --${field}-bottom-left: ${v.bottom_left}mm;`);
|
||||
if (v.right_left != null) lines.push(` --${field}-right-left: ${v.right_left}mm;`);
|
||||
}
|
||||
lines.push('}');
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function applyLive() {
|
||||
varsEl.textContent = cssFromState(state);
|
||||
if (isDirty()) setStatus('Unsaved changes', 'dirty');
|
||||
else setStatus('Saved', 'ok');
|
||||
syncInputs();
|
||||
highlightSelected();
|
||||
}
|
||||
|
||||
function step() {
|
||||
const s = parseFloat(stepEl.value);
|
||||
return Number.isFinite(s) && s > 0 ? s : 0.5;
|
||||
}
|
||||
|
||||
function setValue(field, key, value) {
|
||||
if (!(key in state[field])) return;
|
||||
let v = round1(value);
|
||||
if (key === 'font') v = Math.max(4, Math.min(20, v));
|
||||
else v = Math.max(0, Math.min(150, v));
|
||||
state[field][key] = v;
|
||||
applyLive();
|
||||
}
|
||||
|
||||
function nudge(field, key, dir) {
|
||||
setValue(field, key, state[field][key] + dir * step());
|
||||
}
|
||||
|
||||
function makeNudgeRow(field, key, label) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'nudge';
|
||||
row.dataset.field = field;
|
||||
row.dataset.key = key;
|
||||
|
||||
const lab = document.createElement('label');
|
||||
lab.textContent = label;
|
||||
|
||||
const input = document.createElement('input');
|
||||
input.type = 'number';
|
||||
input.step = '0.1';
|
||||
input.value = state[field][key];
|
||||
input.addEventListener('change', () => setValue(field, key, input.value));
|
||||
|
||||
const minus = document.createElement('button');
|
||||
minus.type = 'button';
|
||||
minus.textContent = '−';
|
||||
minus.addEventListener('click', () => nudge(field, key, -1));
|
||||
|
||||
const plus = document.createElement('button');
|
||||
plus.type = 'button';
|
||||
plus.textContent = '+';
|
||||
plus.addEventListener('click', () => nudge(field, key, 1));
|
||||
|
||||
row.append(lab, input, minus, plus);
|
||||
return row;
|
||||
}
|
||||
|
||||
function makeFieldBlock(field, keys, section) {
|
||||
const block = document.createElement('div');
|
||||
block.className = 'field-block';
|
||||
block.dataset.field = field;
|
||||
block.dataset.section = section;
|
||||
|
||||
const h = document.createElement('h3');
|
||||
h.textContent = LABELS[field];
|
||||
block.appendChild(h);
|
||||
|
||||
for (const { key, label } of keys) {
|
||||
if (state[field][key] == null) continue;
|
||||
block.appendChild(makeNudgeRow(field, key, label));
|
||||
}
|
||||
|
||||
block.addEventListener('click', () => {
|
||||
selected = { field, section };
|
||||
highlightSelected();
|
||||
});
|
||||
return block;
|
||||
}
|
||||
|
||||
function buildPanel() {
|
||||
const top = document.getElementById('sec-top');
|
||||
const bottom = document.getElementById('sec-bottom');
|
||||
const right = document.getElementById('sec-right');
|
||||
|
||||
for (const field of FIELDS) {
|
||||
top.appendChild(makeFieldBlock(field, TOP_KEYS, 'top'));
|
||||
if (BOTTOM_KEYS[field]) {
|
||||
bottom.appendChild(makeFieldBlock(field, BOTTOM_KEYS[field], 'bottom'));
|
||||
}
|
||||
if (RIGHT_KEYS[field]) {
|
||||
right.appendChild(makeFieldBlock(field, RIGHT_KEYS[field], 'right'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function syncInputs() {
|
||||
document.querySelectorAll('.nudge').forEach((row) => {
|
||||
const field = row.dataset.field;
|
||||
const key = row.dataset.key;
|
||||
const input = row.querySelector('input');
|
||||
if (input && document.activeElement !== input) {
|
||||
input.value = state[field][key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function highlightSelected() {
|
||||
document.querySelectorAll('.field-block').forEach((el) => {
|
||||
el.classList.toggle(
|
||||
'active',
|
||||
el.dataset.field === selected.field && el.dataset.section === selected.section
|
||||
);
|
||||
});
|
||||
|
||||
document.querySelectorAll('.var-field').forEach((el) => {
|
||||
const q = el.dataset.scope;
|
||||
const field = el.dataset.field;
|
||||
let match = field === selected.field;
|
||||
if (selected.section === 'top') {
|
||||
match = match && (q === 'TL' || q === 'TR');
|
||||
} else if (selected.section === 'bottom') {
|
||||
match = match && (q === 'BL' || q === 'BR');
|
||||
} else if (selected.section === 'right') {
|
||||
match = match && (q === 'TR' || q === 'BR');
|
||||
}
|
||||
el.classList.toggle('selected', match);
|
||||
});
|
||||
}
|
||||
|
||||
function payloadFromState() {
|
||||
// API schema expects all keys; optional ones may be null
|
||||
const out = {};
|
||||
for (const field of FIELDS) {
|
||||
out[field] = {
|
||||
top: state[field].top,
|
||||
left: state[field].left,
|
||||
font: state[field].font,
|
||||
bottom_top: state[field].bottom_top,
|
||||
bottom_left: state[field].bottom_left ?? null,
|
||||
right_left: state[field].right_left ?? null,
|
||||
};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function save() {
|
||||
const btn = document.getElementById('btn-save');
|
||||
btn.disabled = true;
|
||||
try {
|
||||
const res = await fetch('/api/calibration', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payloadFromState()),
|
||||
});
|
||||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||
state = await res.json();
|
||||
saved = structuredClone(state);
|
||||
applyLive();
|
||||
setStatus('Saved to server', 'ok');
|
||||
} catch (e) {
|
||||
setStatus('Save failed: ' + e.message, 'err');
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function reset() {
|
||||
if (!confirm('Reset all positions to built-in defaults?')) return;
|
||||
try {
|
||||
const res = await fetch('/api/calibration/reset', { method: 'POST' });
|
||||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||
state = await res.json();
|
||||
saved = structuredClone(state);
|
||||
applyLive();
|
||||
setStatus('Defaults restored', 'ok');
|
||||
} catch (e) {
|
||||
setStatus('Reset failed: ' + e.message, 'err');
|
||||
}
|
||||
}
|
||||
|
||||
document.querySelectorAll('.var-field').forEach((el) => {
|
||||
el.addEventListener('click', () => {
|
||||
const q = el.dataset.scope;
|
||||
const field = el.dataset.field;
|
||||
let section = 'top';
|
||||
if ((q === 'TR' || q === 'BR') && (field === 'batch' || field === 'weight')) {
|
||||
section = 'right';
|
||||
} else if (q === 'BL' || q === 'BR') {
|
||||
section = 'bottom';
|
||||
}
|
||||
selected = { field, section };
|
||||
const block = document.querySelector(
|
||||
`.field-block[data-field="${field}"][data-section="${section}"]`
|
||||
);
|
||||
if (block) block.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
||||
highlightSelected();
|
||||
});
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.target.matches('input, textarea')) return;
|
||||
const map = {
|
||||
ArrowLeft: ['leftish', -1],
|
||||
ArrowRight: ['leftish', 1],
|
||||
ArrowUp: ['topish', -1],
|
||||
ArrowDown: ['topish', 1],
|
||||
};
|
||||
const hit = map[e.key];
|
||||
if (!hit) return;
|
||||
e.preventDefault();
|
||||
const [axis, dir] = hit;
|
||||
const { field, section } = selected;
|
||||
let key;
|
||||
if (section === 'top') {
|
||||
key = axis === 'topish' ? 'top' : 'left';
|
||||
} else if (section === 'bottom') {
|
||||
key = axis === 'topish' ? 'bottom_top' : 'bottom_left';
|
||||
} else {
|
||||
key = axis === 'topish' ? null : 'right_left';
|
||||
}
|
||||
if (!key || state[field][key] == null) return;
|
||||
nudge(field, key, dir);
|
||||
});
|
||||
|
||||
document.getElementById('btn-save').addEventListener('click', save);
|
||||
document.getElementById('btn-reset').addEventListener('click', reset);
|
||||
document.getElementById('btn-print').addEventListener('click', () => window.print());
|
||||
|
||||
buildPanel();
|
||||
applyLive();
|
||||
})();
|
||||
Reference in New Issue
Block a user