// ── documenti.js — core namespace, refresh, pagination, settings ── var _Doc = {}; _Doc.blocks = []; _Doc.PAGE_SIZE = 10; _Doc.currentPage = 1; _Doc.autorefreshTimer = null; _Doc.rebuildBlocks = function() { _Doc.blocks = Array.prototype.slice.call(document.querySelectorAll('.sess-block')); }; _Doc.resetPage = function() { _Doc.currentPage = 1; }; _Doc.saveFilterState = function() { var tipo = {}, cartella = {}; document.querySelectorAll('.tipo-chk').forEach(function(c){ tipo[c.value] = c.checked; }); document.querySelectorAll('.cartella-chk').forEach(function(c){ cartella[c.value] = c.checked; }); return { tipo: tipo, cartella: cartella }; }; _Doc.doRefresh = function() { var btn = document.getElementById('btn-refresh'); if (btn) { btn.disabled = true; btn.textContent = '\u27F3 Aggiornamento\u2026'; } var state = _Doc.saveFilterState(); fetch('/api/documenti') .then(function(r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); }) .then(function(data) { document.getElementById('doc-content').innerHTML = data.html; _Doc.rebuildBlocks(); _Doc.buildDynamicFilters(state.tipo, state.cartella); _Doc.applyFilters(); var tsEl = document.getElementById('doc-refresh-ts'); if (tsEl) tsEl.textContent = data.ts; }) .catch(function(err) { console.error('Refresh error:', err); }) .finally(function() { if (btn) { btn.disabled = false; btn.textContent = '\u21BB Aggiorna dati'; } }); }; // ── Pagination ───────────────────────────────────────────────────────────── // Pagination uses .pag-hidden class (not inline style) so export still works on all filtered data. _Doc.applyPagination = function() { // Remove any previous pagination-hiding first _Doc.blocks.forEach(function(b) { b.classList.remove('pag-hidden'); }); var visible = _Doc.blocks.filter(function(b) { return b.style.display !== 'none'; }); var total = Math.max(1, Math.ceil(visible.length / _Doc.PAGE_SIZE)); var page = _Doc.currentPage; if (page > total) page = _Doc.currentPage = total; if (page < 1) page = _Doc.currentPage = 1; var countEl = document.getElementById('doc-visible-count'); if (total <= 1) { var pag = document.getElementById('doc-pagination'); if (pag) pag.innerHTML = ''; if (countEl) countEl.textContent = visible.length + ' sessioni'; return; } var start = (page - 1) * _Doc.PAGE_SIZE; visible.forEach(function(b, i) { b.classList.toggle('pag-hidden', i < start || i >= start + _Doc.PAGE_SIZE); }); if (countEl) countEl.textContent = visible.length + ' sessioni \u2014 pag. ' + page + '/' + total; _Doc.renderPagination(page, total); }; _Doc.renderPagination = function(page, total) { var pag = document.getElementById('doc-pagination'); if (!pag) return; var lo = Math.max(1, page - 3); var hi = Math.min(total, lo + 6); lo = Math.max(1, hi - 6); var html = ''; if (lo > 1) { html += ''; if (lo > 2) html += '…'; } for (var i = lo; i <= hi; i++) { html += ''; } if (hi < total) { if (hi < total - 1) html += '…'; html += ''; } html += ''; pag.innerHTML = html; pag.querySelectorAll('.doc-pag-btn:not([disabled])').forEach(function(btn) { btn.addEventListener('click', function() { _Doc.currentPage = parseInt(this.getAttribute('data-page'), 10); _Doc.applyPagination(); document.getElementById('doc-content').scrollIntoView({ behavior: 'smooth', block: 'start' }); }); }); }; // ── Event listeners ──────────────────────────────────────────────────────── document.getElementById('btn-refresh').addEventListener('click', _Doc.doRefresh); document.getElementById('btn-expand-all').addEventListener('click', function() { _Doc.blocks.forEach(function(b) { if (b.style.display !== 'none' && !b.classList.contains('pag-hidden')) b.open = true; }); }); document.getElementById('btn-collapse-all').addEventListener('click', function() { _Doc.blocks.forEach(function(b) { if (!b.classList.contains('pag-hidden')) b.open = false; }); }); // ── Settings modal ───────────────────────────────────────────────────────── (function() { var wrenchDrop = document.querySelector('.wrench-drop'); if (wrenchDrop) { var link = document.createElement('a'); link.id = 'btn-page-settings'; link.href = '#'; link.innerHTML = '⚙ Impostazioni…'; wrenchDrop.appendChild(link); } var modal = document.createElement('div'); modal.id = 'page-settings-modal'; modal.className = 'page-settings-overlay'; modal.innerHTML = '