adds status indicators to files view

This commit is contained in:
Michael Beck
2025-02-06 22:37:39 +01:00
parent 417a47d6a7
commit a5c15f211e
3 changed files with 174 additions and 103 deletions

View File

@@ -1,27 +1,34 @@
function deleteFiles(filePaths) {
fetch('/delete_files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'file_paths': filePaths
})
})
.then(response => response.json())
.then(data => {
async function deleteFiles(filePaths) {
try {
const response = await fetch('/delete_files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ file_paths: filePaths })
});
const data = await response.json();
if (data.success) {
alert('Files deleted successfully');
location.reload();
} else {
alert('Error deleting files: ' + JSON.stringify(data.errors));
alert(`Error deleting files: ${JSON.stringify(data.errors)}`);
}
});
} catch (error) {
console.error('Error:', error);
alert('An error occurred while deleting files.');
}
}
function getSelectedFiles() {
return Array.from(document.querySelectorAll('input[name="fileCheckbox"]:checked'))
.map(checkbox => checkbox.value);
}
function deleteSelectedFiles() {
const selectedFiles = Array.from(document.querySelectorAll('input[name="fileCheckbox"]:checked'))
.map(checkbox => checkbox.value);
const selectedFiles = getSelectedFiles();
if (selectedFiles.length > 0) {
deleteFiles(selectedFiles);
} else {
@@ -29,39 +36,43 @@ function deleteSelectedFiles() {
}
}
async function downloadSelectedFiles() {
const selectedFiles = getSelectedFiles();
if (selectedFiles.length === 0) {
alert('No files selected');
return;
}
function downloadSelectedFiles() {
const selectedFiles = Array.from(document.querySelectorAll('input[name="fileCheckbox"]:checked'))
.map(checkbox => checkbox.value);
if (selectedFiles.length > 0) {
fetch('/download_files', {
try {
const response = await fetch('/download_files', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ 'file_paths': selectedFiles })
})
.then(response => {
if (!response.ok) {
return response.json().then(err => { throw new Error(err.error); });
}
return response.blob();
})
.then(blob => {
if (blob.type !== 'application/zip') {
throw new Error("Received invalid ZIP file.");
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'files.zip';
document.body.appendChild(a);
a.click();
a.remove();
})
.catch(error => alert('Error: ' + error.message));
} else {
alert('No files selected');
body: JSON.stringify({ file_paths: selectedFiles })
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Failed to download files.');
}
const blob = await response.blob();
if (blob.type !== 'application/zip') {
throw new Error('Received invalid ZIP file.');
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'files.zip';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
} catch (error) {
console.error('Download error:', error);
alert(`Error: ${error.message}`);
}
}
@@ -69,36 +80,25 @@ function sortTable(columnIndex, tableId) {
const table = document.getElementById(tableId);
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.rows);
const isAscending = table.getAttribute('data-sort-asc') === 'true';
const isAscending = table.dataset.sortAsc === 'true';
rows.sort((rowA, rowB) => {
const cellA = rowA.cells[columnIndex].innerText.toLowerCase();
const cellB = rowB.cells[columnIndex].innerText.toLowerCase();
if (cellA < cellB) {
return isAscending ? -1 : 1;
}
if (cellA > cellB) {
return isAscending ? 1 : -1;
}
return 0;
const cellA = rowA.cells[columnIndex].innerText.trim().toLowerCase();
const cellB = rowB.cells[columnIndex].innerText.trim().toLowerCase();
return cellA.localeCompare(cellB) * (isAscending ? 1 : -1);
});
// Toggle the sorting order for the next click
table.setAttribute('data-sort-asc', !isAscending);
// Toggle sorting order for next click
table.dataset.sortAsc = !isAscending;
// Append sorted rows back to the tbody
// Reinsert sorted rows
rows.forEach(row => tbody.appendChild(row));
}
// Function to check or uncheck all checkboxes in a table by checking or unchecking the "CheckAll" checkbox
function checkAllCheckboxes(tableId, checkAllCheckboxId) {
const table = document.getElementById(tableId);
const checkboxes = table.querySelectorAll('input[type="checkbox"][name="fileCheckbox"]');
const checkboxes = table.querySelectorAll('input[name="fileCheckbox"]');
const checkAllCheckbox = document.getElementById(checkAllCheckboxId);
const isChecked = checkAllCheckbox.checked;
checkboxes.forEach(checkbox => {
checkbox.checked = isChecked;
});
}
checkboxes.forEach(checkbox => checkbox.checked = checkAllCheckbox.checked);
}