Files
Assetmanager/app/static/js/software-inventory-selection.js

45 lines
2.6 KiB
JavaScript

(()=>{
function init(root=document){
root.querySelectorAll('[data-software-selection]').forEach(container=>{
if(container.dataset.selectionReady==='1') return;
container.dataset.selectionReady='1';
const table=container.querySelector('table');
const form=container.querySelector('form[data-software-delete-form]');
const master=container.querySelector('[data-software-select-all]');
const button=container.querySelector('[data-software-delete-button]');
const countNode=container.querySelector('[data-software-selection-count]');
if(!table||!form) return;
const boxes=()=>[...table.querySelectorAll('tbody input[data-software-entry-checkbox]')];
const visibleBoxes=()=>boxes().filter(box=>!box.closest('tr')?.hidden);
const update=()=>{
const selected=boxes().filter(box=>box.checked);
if(button) button.disabled=selected.length===0;
if(countNode) countNode.textContent=String(selected.length);
if(master){
const visible=visibleBoxes();
const selectedVisible=visible.filter(box=>box.checked).length;
master.checked=visible.length>0&&selectedVisible===visible.length;
master.indeterminate=selectedVisible>0&&selectedVisible<visible.length;
master.disabled=visible.length===0;
}
};
master?.addEventListener('change',()=>{ visibleBoxes().forEach(box=>box.checked=master.checked); update(); });
table.addEventListener('change',event=>{ if(event.target.matches('[data-software-entry-checkbox]')) update(); });
form.addEventListener('submit',event=>{
const selected=boxes().filter(box=>box.checked);
if(!selected.length){ event.preventDefault(); return; }
form.querySelectorAll('input[data-generated-entry-id]').forEach(node=>node.remove());
selected.forEach(box=>{ const input=document.createElement('input'); input.type='hidden'; input.name='entry_ids'; input.value=box.value; input.dataset.generatedEntryId='1'; form.appendChild(input); });
const template=form.dataset.confirmTemplate||'Delete {count} selected software inventory entries?';
const warning=form.dataset.inventoryOnlyWarning||'';
const message=template.replace('{count}',String(selected.length))+(warning?'\n\n'+warning:'');
if(!window.confirm(message)) event.preventDefault();
});
window.addEventListener('asset-table-filter-changed',event=>{ if(event.detail?.table===table) update(); });
update();
});
}
document.addEventListener('DOMContentLoaded',()=>init());
window.AssetManagerSoftwareSelection={init};
})();