diff --git a/public/userarea/matrici.php b/public/userarea/matrici.php index 5d1cdc44..550de76f 100644 --- a/public/userarea/matrici.php +++ b/public/userarea/matrici.php @@ -1271,7 +1271,12 @@ function revisionLabel($rev) } function loadExistingAttachments(idmatrice) { - $("#existingAttachmentsList").html('
'); + const $attList = $("#existingAttachmentsList"); + const keepScroll = $attList.scrollTop(); + // mostro "Caricamento" solo se la lista è vuota, altrimenti lascio i file visibili finché arrivano i nuovi + if (!$attList.find('.attachment-existing-row').length) { + $attList.html(''); + } fetch("get_matrice_attachments.php?id=" + encodeURIComponent(idmatrice)) .then(r => r.json()) @@ -1385,12 +1390,61 @@ function revisionLabel($rev) `; container.append(html); }); + + $attList.scrollTop(keepScroll); }) .catch(() => { $("#existingAttachmentsList").html(''); }); } + // Aggiorna solo la riga dell'allegato dopo un cambio di flag, senza ricaricare la lista + const ATT_FLAGS = { + is_disegno: { + rowClass: 'is-disegno', + badge: ' Disegno tecnico', + titleOn: 'Togli da disegni tecnici', + titleOff: 'Segna come disegno tecnico' + }, + is_strumento: { + rowClass: 'is-strumento', + badge: ' Strumento di lavoro', + titleOn: 'Togli da strumenti di lavoro', + titleOff: 'Segna come strumento di lavoro' + }, + is_ciclo: { + rowClass: 'is-ciclo', + badge: ' Ciclo di lavoro', + titleOn: 'Togli da cicli di lavoro', + titleOff: 'Segna come ciclo di lavoro' + } + }; + + function applyAttachmentFlag($btn, field, value) { + const cfg = ATT_FLAGS[field]; + if (!cfg) return; + + const on = value === 1; + const $row = $btn.closest('.attachment-existing-row'); + + // pulsante + $btn.data('value', value).attr('data-value', value) + .toggleClass('active', on) + .attr('title', on ? cfg.titleOn : cfg.titleOff); + + // bordo/colore della riga + $row.toggleClass(cfg.rowClass, on); + + // badge accanto al nome, sempre nello stesso ordine + const $title = $row.find('.attachment-meta-title'); + $title.find('.badge-disegno, .badge-strumento, .badge-ciclo').remove(); + ['is_disegno', 'is_strumento', 'is_ciclo'].forEach(f => { + if ($row.hasClass(ATT_FLAGS[f].rowClass)) { + $title.append(' ' + ATT_FLAGS[f].badge); + } + }); + } + function openFilePreview(filePath, fileName) { const ext = getFileExt(fileName); $("#filePreviewTitle").text(fileName || 'Anteprima file'); @@ -2024,7 +2078,6 @@ function revisionLabel($rev) const $btn = $(this); const id = $btn.data("id"); const newValue = parseInt($btn.data("value"), 10) === 1 ? 0 : 1; - const idmatrice = $("#mf_idmatrice").val(); $btn.prop("disabled", true); @@ -2037,16 +2090,19 @@ function revisionLabel($rev) }) .then(r => r.json()) .then(data => { + $btn.prop("disabled", false); + if (!data.success) { Swal.fire({ icon: "error", title: "Errore", text: data.message || "" }); - $btn.prop("disabled", false); return; } + applyAttachmentFlag($btn, 'is_disegno', newValue); + Swal.fire({ toast: true, position: "top-end", @@ -2055,16 +2111,14 @@ function revisionLabel($rev) showConfirmButton: false, timer: 1500 }); - - loadExistingAttachments(idmatrice); }) .catch(() => { + $btn.prop("disabled", false); Swal.fire({ icon: "error", title: "Errore", text: "Errore di comunicazione con il server" }); - $btn.prop("disabled", false); }); }); @@ -2073,7 +2127,6 @@ function revisionLabel($rev) const id = $btn.data("id"); const field = $btn.data("field"); const newValue = parseInt($btn.data("value"), 10) === 1 ? 0 : 1; - const idmatrice = $("#mf_idmatrice").val(); $btn.prop("disabled", true); @@ -2088,15 +2141,19 @@ function revisionLabel($rev) }) .then(r => r.json()) .then(data => { + $btn.prop("disabled", false); + if (!data.success) { Swal.fire({ icon: "error", title: "Errore", text: data.message || "" }); - $btn.prop("disabled", false); return; } + + applyAttachmentFlag($btn, field, newValue); + Swal.fire({ toast: true, position: "top-end", @@ -2105,15 +2162,14 @@ function revisionLabel($rev) showConfirmButton: false, timer: 1500 }); - loadExistingAttachments(idmatrice); }) .catch(() => { + $btn.prop("disabled", false); Swal.fire({ icon: "error", title: "Errore", text: "Errore di comunicazione con il server" }); - $btn.prop("disabled", false); }); });