178 lines
7.5 KiB
JavaScript
178 lines
7.5 KiB
JavaScript
// ── 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 = '<button class="doc-pag-btn" data-page="' + (page - 1) + '"' + (page === 1 ? ' disabled' : '') + '>«</button>';
|
|
if (lo > 1) {
|
|
html += '<button class="doc-pag-btn" data-page="1">1</button>';
|
|
if (lo > 2) html += '<span class="doc-pag-ellipsis">…</span>';
|
|
}
|
|
for (var i = lo; i <= hi; i++) {
|
|
html += '<button class="doc-pag-btn' + (i === page ? ' active' : '') + '" data-page="' + i + '">' + i + '</button>';
|
|
}
|
|
if (hi < total) {
|
|
if (hi < total - 1) html += '<span class="doc-pag-ellipsis">…</span>';
|
|
html += '<button class="doc-pag-btn" data-page="' + total + '">' + total + '</button>';
|
|
}
|
|
html += '<button class="doc-pag-btn" data-page="' + (page + 1) + '"' + (page === total ? ' disabled' : '') + '>»</button>';
|
|
|
|
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 =
|
|
'<div class="page-settings-dialog">' +
|
|
'<div class="page-settings-hdr">' +
|
|
'<span>Impostazioni pagina</span>' +
|
|
'<button class="page-settings-close" id="btn-settings-close" title="Chiudi">×</button>' +
|
|
'</div>' +
|
|
'<div class="page-settings-body">' +
|
|
'<div class="page-settings-row"><label><input type="checkbox" id="autorefresh-toggle"> Auto-aggiornamento</label></div>' +
|
|
'<div class="page-settings-row"><span>Intervallo (sec)</span><input type="number" id="autorefresh-seconds" class="page-settings-input" value="60" min="5" max="3600"></div>' +
|
|
'</div>' +
|
|
'</div>';
|
|
document.body.appendChild(modal);
|
|
|
|
function openModal() { modal.classList.add('open'); }
|
|
function closeModal() { modal.classList.remove('open'); }
|
|
|
|
var settingsBtn = document.getElementById('btn-page-settings');
|
|
if (settingsBtn) {
|
|
settingsBtn.addEventListener('click', function(e) {
|
|
e.preventDefault(); e.stopPropagation();
|
|
var drop = document.querySelector('.wrench-drop');
|
|
if (drop) drop.classList.remove('open');
|
|
openModal();
|
|
});
|
|
}
|
|
document.getElementById('btn-settings-close').addEventListener('click', closeModal);
|
|
modal.addEventListener('click', function(e) { if (e.target === modal) closeModal(); });
|
|
document.addEventListener('keydown', function(e) { if (e.key === 'Escape') closeModal(); });
|
|
|
|
document.getElementById('autorefresh-toggle').addEventListener('change', function() {
|
|
if (this.checked) { startAutorefresh(); } else { stopAutorefresh(); }
|
|
});
|
|
document.getElementById('autorefresh-seconds').addEventListener('change', function() {
|
|
if (document.getElementById('autorefresh-toggle').checked) { startAutorefresh(); }
|
|
});
|
|
})();
|
|
|
|
function startAutorefresh() {
|
|
stopAutorefresh();
|
|
var secs = parseInt(document.getElementById('autorefresh-seconds').value, 10) || 60;
|
|
if (secs < 5) secs = 5;
|
|
_Doc.autorefreshTimer = setInterval(_Doc.doRefresh, secs * 1000);
|
|
}
|
|
function stopAutorefresh() {
|
|
if (_Doc.autorefreshTimer) { clearInterval(_Doc.autorefreshTimer); _Doc.autorefreshTimer = null; }
|
|
}
|