Packet-Fence/admin/js/sorting_popup.js
2025-06-28 06:23:17 +05:30

121 lines
4.5 KiB
JavaScript

// sorting.js
document.addEventListener("DOMContentLoaded", function() {
const getCellValue = (tr, idx) => tr.children[idx].innerText || tr.children[idx].textContent;
const comparer = (idx, asc, isMixed) => (a, b) => {
let v1 = getCellValue(asc ? a : b, idx);
let v2 = getCellValue(asc ? b : a, idx);
// Handle empty values
if (v1 === '') v1 = '0'; // Treat empty values as '0' or some comparable default
if (v2 === '') v2 = '0';
if (isMixed) {
const num1 = parseFloat(v1);
const num2 = parseFloat(v2);
// Check if both values are numbers
if (!isNaN(num1) && !isNaN(num2)) {
v1 = num1;
v2 = num2;
}
}
return v1.toString().localeCompare(v2.toString(), undefined, { numeric: true });
};
document.querySelectorAll('th.sortable img.sort').forEach(img => img.addEventListener('click', function() {
const th = this.closest('th'); // Find the closest th element
const table = th.closest('table');
const isMixed = th.dataset.column === 'pid'; // Check if sorting a mixed column like PID
const rows = Array.from(table.querySelectorAll('tbody tr')); // Get all rows including those in other pages
rows
.sort(comparer(Array.from(th.parentNode.children).indexOf(th), th.dataset.order === 'asc', isMixed))
.forEach(tr => table.querySelector('tbody').appendChild(tr));
th.dataset.order = th.dataset.order === 'asc' ? 'desc' : 'asc';
}));
// Default sorting (ascending)
document.querySelectorAll('th.sortable').forEach(th => {
th.dataset.order = 'asc'; // Ensure default order is ascending
const img = th.querySelector('img.sort');
if (th.dataset.column === 'pid') { // If you want PID to be the default sorted column
img.click(); // Trigger sort on the desired column
}
});
});
// userlist.js
document.addEventListener('DOMContentLoaded', function() {
const table = document.getElementById('user_table');
const form = document.getElementById('pid_form');
const pidInput = document.getElementById('pid_input');
const popup = document.getElementById("popup");
const popupMessage = document.getElementById("popup-message");
const confirmButton = document.getElementById("confirm-button");
const cancelButton = document.getElementById("cancel-button");
const searchBtn = document.getElementById("search_btn");
const clearButton = document.getElementById("clear_btn");
const searchInput = document.getElementById("search");
let currentPid = null;
table.addEventListener('click', function(event) {
const target = event.target;
if (target.classList.contains('btn_delete')) {
// Handle delete button click
currentPid = target.getAttribute('data-pid');
if (currentPid) {
popupMessage.textContent = "Do you really want to delete the user: " + currentPid + "?";
popup.style.display = "block";
}
event.stopPropagation();
}
else if (target.classList.contains('btn_modify')) {
// Handle modify button click
const pid = target.getAttribute('data-pid');
if (pid) {
pidInput.value = pid;
form.action = '../user/user_edit.php';
form.submit();
}
}
});
confirmButton.addEventListener('click', function() {
const deleteForm = document.createElement('form');
deleteForm.action = '../user/user_delete.php';
deleteForm.method = 'POST';
const pidInputField = document.createElement('input');
pidInputField.type = 'hidden';
pidInputField.name = 'pid';
pidInputField.value = currentPid;
deleteForm.appendChild(pidInputField);
document.body.appendChild(deleteForm);
deleteForm.submit();
popup.style.display = "none";
});
cancelButton.addEventListener('click', function() {
popup.style.display = "none";
});
// Close popup when clicking outside of it
window.addEventListener('click', function(event) {
if (event.target == popup) {
popup.style.display = "none";
}
});
// Clear search input
clearButton.addEventListener('click', function(event) {
event.preventDefault();
searchInput.value = '';
});
});