diff --git a/static/js/pages/interfaces.js b/static/js/pages/interfaces.js new file mode 100644 index 0000000..4ecf1e7 --- /dev/null +++ b/static/js/pages/interfaces.js @@ -0,0 +1,113 @@ +function filterTable(searchInputId,tableId){ + const input=document.getElementById(searchInputId); + const filter=input.value.toUpperCase(); + const table=document.getElementById(tableId); + const tr=table.getElementsByTagName("tr"); + + for(let i=1;i-1){ + tr[i].style.display=""; + break; + } + } + } + } +} + +function sortTable(tableId,columnIndex,th){ + const table=document.getElementById(tableId); + const tbody=table.tBodies[0]; + const rows=Array.from(tbody.rows); + const headers=Array.from(th.parentNode.children); + + headers.forEach(header=>{ + if(header!==th){ + header.classList.remove('asc','desc'); + header.setAttribute('data-sort-state','none'); + } + }); + + let currentState=th.getAttribute('data-sort-state')||'none'; + let newState=currentState==='none'?'asc':currentState==='asc'?'desc':'none'; + + th.classList.remove('asc','desc'); + if(newState!=='none')th.classList.add(newState); + th.setAttribute('data-sort-state',newState); + + if(newState==='none'){ + rows.sort((a,b)=>parseInt(a.getAttribute('data-index'))-parseInt(b.getAttribute('data-index'))); + }else{ + rows.sort((a,b)=>{ + const aText=a.cells[columnIndex].textContent.trim(); + const bText=b.cells[columnIndex].textContent.trim(); + const aNum=parseFloat(aText.replace(/[^0-9.-]/g,'')); + const bNum=parseFloat(bText.replace(/[^0-9.-]/g,'')); + const isNumeric=!isNaN(aNum)&&!isNaN(bNum); + + return newState==='asc' + ?(isNumeric?aNum-bNum:aText.localeCompare(bText)) + :(isNumeric?bNum-aNum:bText.localeCompare(aText)); + }); + } + + rows.forEach(row=>tbody.appendChild(row)); +} + +async function loadInterfaceTable(){ + const tableBody=document.getElementById("interfaceTableBody"); + const loadingRow=document.getElementById("interface-loading-row"); + + try{ + const response=await fetch("/interfaces/json"); + const data=await response.json(); + + tableBody.innerHTML=""; + + if(!data.interface_table||data.interface_table.length===0){ + tableBody.innerHTML="Geen data beschikbaar."; + return; + } + + data.interface_table.forEach((entry,index)=>{ + const row=document.createElement("tr"); + row.setAttribute("data-index",index); + row.innerHTML=` + ${entry.interface} + ${entry.ip_address} + ${entry.mac_address} + ${entry.vrf} + ${entry.mtu} + ${entry.status} + ${entry.description} + `; + tableBody.appendChild(row); + }); + }catch(err){ + tableBody.innerHTML="Fout bij ophalen van data."; + } +} + +window.addEventListener("DOMContentLoaded",()=>{ + loadInterfaceTable(); +}); + +function refreshTable(){ + const searchInput=document.getElementById("interfaceSearch"); + const refreshIcon=document.getElementById("refreshIcon"); + const refreshSpinner=document.getElementById("refreshSpinner"); + + searchInput.value=""; + + refreshIcon.classList.add("d-none"); + refreshSpinner.classList.remove("d-none"); + + loadInterfaceTable().then(()=>{ + refreshIcon.classList.remove("d-none"); + refreshSpinner.classList.add("d-none"); + }); +}