/* ============================================================
KODILA — DataGrid (AG Grid wrapper) + orodna vrstica
Privzeto vedenje za VSE gride v sistemu:
- filter je del glave kolone (klik na meni glave), ni ločene tipke
- "Kolone" omogoča shranjevanje/ponastavitev postavitve (na uporabnika)
- klik z desno tipko na vrstico odpre meni: kopiraj vrstico/celico, rekapitulacija
============================================================ */
/* Build a print window for PDF export */
function exportPdf(title, columns, rows) {
const head = columns.map(c => `
${c.header}
`).join('');
const body = rows.map(r =>
'
' + columns.map(c => `
${(r[c.field] ?? '')}
`).join('') + '
'
).join('');
const w = window.open('', '_blank');
if (!w) return;
w.document.write(`${title}
K
${title}
KODILA · Poslovno-informacijski sistem · ${new Date().toLocaleString('sl-SI')}
${head}
${body}
Izvoženo iz sistema KODILA — ${rows.length} zapisov
`);
w.document.close();
setTimeout(() => { w.focus(); w.print(); }, 350);
}
/* --- shranjevanje postavitve kolon (na uporabnika, na grid) --- */
function gridPrefsKey(gridId) {
const uid = (window.__KODILA_USER__ && window.__KODILA_USER__.id) || 'anon';
return `kodila_grid_layout:${gridId}:${uid}`;
}
/* --- formatiranje vrednosti celice za kopiranje/rekapitulacijo (upošteva valueFormatter) --- */
function formatCellForCopy(colDef, data) {
const raw = data ? data[colDef.field] : undefined;
if (colDef.valueFormatter) {
try { return colDef.valueFormatter({ value: raw, data, colDef }); } catch (e) { /* ignore */ }
}
return raw === null || raw === undefined ? '' : String(raw);
}
async function copyToClipboard(text, toast) {
try {
await navigator.clipboard.writeText(text);
toast('Kopirano v odložišče', 'copy');
} catch (e) {
toast('Kopiranje ni uspelo', 'alert');
}
}
/* --- Meni z desnim klikom na vrstico --- */
function RowContextMenu({ x, y, columnDefs, data, field, onClose, onRecap, toast }) {
const ref = useRef(null);
useEffect(() => {
const close = () => onClose();
const onKey = (e) => { if (e.key === 'Escape') onClose(); };
document.addEventListener('mousedown', close);
document.addEventListener('scroll', close, true);
document.addEventListener('keydown', onKey);
return () => {
document.removeEventListener('mousedown', close);
document.removeEventListener('scroll', close, true);
document.removeEventListener('keydown', onKey);
};
}, [onClose]);
const dataCols = columnDefs.filter(c => c.colId !== '__sel' && c.field);
const clickedCol = dataCols.find(c => c.field === field);
const copyRow = () => {
const line = dataCols.map(c => formatCellForCopy(c, data)).join('\t');
copyToClipboard(line, toast);
onClose();
};
const copyCell = () => {
if (!clickedCol) { onClose(); return; }
copyToClipboard(formatCellForCopy(clickedCol, data), toast);
onClose();
};
const vw = window.innerWidth, vh = window.innerHeight;
const left = Math.min(x, vw - 220);
const top = Math.min(y, vh - 160);
return (