-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayFileList.js
92 lines (80 loc) · 3.18 KB
/
displayFileList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
let sortDirection = {};
let sortFunctions = {
'File Name': (a, b) => a.name.localeCompare(b.name),
'File Size': (a, b) => a.sizeBytes - b.sizeBytes,
'File Time': (a, b) => new Date(a.modified) - new Date(b.modified),
'Upload Time': (a, b) => new Date(a.uploaded) - new Date(b.uploaded)
};
async function loadFileList() {
try {
const { username, password } = authCredentials;
console.log('Loading file list with credentials:', { username, password }); // Debugging
const response = await fetch('getFileList.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fileList = await response.json();
console.log('File list loaded:', fileList); // Debugging: Log the file list to the console
displayFileList(fileList);
} catch (error) {
console.error('Error loading file list:', error); // Debugging: Log any errors to the console
}
}
function displayFileList(fileList) {
const fileListContainer = document.getElementById('fileList');
fileListContainer.innerHTML = '';
const table = document.createElement('table');
const headerRow = table.insertRow();
['File Name', 'File Size', 'File Time', 'Upload Time', '', ''].forEach(headerText => {
const th = document.createElement('th');
if (headerText && headerText !== '') {
const button = document.createElement('button');
button.textContent = headerText;
button.onclick = () => sortTable(headerText, fileList);
button.style.background = 'none';
button.style.border = 'none';
button.style.cursor = 'pointer';
th.appendChild(button);
} else {
th.textContent = headerText;
}
headerRow.appendChild(th);
});
fileList.forEach(file => {
const row = table.insertRow();
row.insertCell().textContent = file.name;
row.insertCell().textContent = file.size;
row.insertCell().textContent = file.modified;
row.insertCell().textContent = file.uploaded;
const linkCell = row.insertCell();
const link = document.createElement('a');
link.href = file.url;
link.textContent = 'Download';
linkCell.appendChild(link);
const deleteCell = row.insertCell();
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.className = 'btn btn-delete';
deleteButton.onclick = () => deleteFile(file.name);
deleteCell.appendChild(deleteButton);
});
fileListContainer.appendChild(table);
}
function sortTable(column, fileList) {
if (!sortDirection[column]) {
sortDirection[column] = 'asc';
} else {
sortDirection[column] = sortDirection[column] === 'asc' ? 'desc' : 'asc';
}
fileList.sort(sortFunctions[column]);
if (sortDirection[column] === 'desc') {
fileList.reverse();
}
displayFileList(fileList);
}