additional cleanup and fixes
This commit is contained in:
@@ -35,18 +35,6 @@ export function loadFileList() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export function toggleDeleteButton() {
|
|
||||||
const selectedFiles = document.querySelectorAll(".file-checkbox:checked");
|
|
||||||
const deleteBtn = document.getElementById("deleteSelectedBtn");
|
|
||||||
const copyBtn = document.getElementById("copySelectedBtn");
|
|
||||||
const moveBtn = document.getElementById("moveSelectedBtn");
|
|
||||||
const disabled = selectedFiles.length === 0;
|
|
||||||
deleteBtn.disabled = disabled;
|
|
||||||
if (copyBtn) copyBtn.disabled = disabled;
|
|
||||||
if (moveBtn) moveBtn.disabled = disabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function toggleAllCheckboxes(source) {
|
export function toggleAllCheckboxes(source) {
|
||||||
const checkboxes = document.querySelectorAll(".file-checkbox");
|
const checkboxes = document.querySelectorAll(".file-checkbox");
|
||||||
checkboxes.forEach(checkbox => checkbox.checked = source.checked);
|
checkboxes.forEach(checkbox => checkbox.checked = source.checked);
|
||||||
@@ -90,59 +78,6 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export function editFile(fileName) {
|
|
||||||
const threshold = 10 * 1024 * 1024; // 10 MB threshold
|
|
||||||
fetch(`uploads/${encodeURIComponent(fileName)}`, { method: 'HEAD' })
|
|
||||||
.then(response => {
|
|
||||||
const fileSize = parseInt(response.headers.get('Content-Length') || "0", 10);
|
|
||||||
if (fileSize > threshold) {
|
|
||||||
alert("This file is too large to edit in the browser.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return fetch(`uploads/${encodeURIComponent(fileName)}?t=${new Date().getTime()}`);
|
|
||||||
})
|
|
||||||
.then(response => {
|
|
||||||
if (!response) return;
|
|
||||||
if (!response.ok) throw new Error("HTTP error! Status: " + response.status);
|
|
||||||
return response.text();
|
|
||||||
})
|
|
||||||
.then(content => {
|
|
||||||
if (!content) return;
|
|
||||||
const modal = document.createElement("div");
|
|
||||||
modal.id = "editorContainer";
|
|
||||||
modal.classList.add("modal", "editor-modal");
|
|
||||||
modal.innerHTML = `
|
|
||||||
<h3>Editing: ${fileName}</h3>
|
|
||||||
<textarea id="fileEditor" style="width:100%; height:60%; resize:none;">${content}</textarea>
|
|
||||||
<div style="margin-top:10px; text-align:right;">
|
|
||||||
<button onclick="saveFile('${fileName}')" class="btn btn-primary">Save</button>
|
|
||||||
<button onclick="document.getElementById('editorContainer').remove()" class="btn btn-secondary">Close</button>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
document.body.appendChild(modal);
|
|
||||||
modal.style.display = "block";
|
|
||||||
})
|
|
||||||
.catch(error => console.error("Error in editFile:", error));
|
|
||||||
}
|
|
||||||
|
|
||||||
export function saveFile(fileName) {
|
|
||||||
const editor = document.getElementById("fileEditor");
|
|
||||||
if (!editor) {
|
|
||||||
console.error("Editor not found!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const fileData = {
|
|
||||||
fileName: fileName,
|
|
||||||
content: editor.value
|
|
||||||
};
|
|
||||||
sendRequest("saveFile.php", "POST", fileData)
|
|
||||||
.then(result => {
|
|
||||||
alert(result.success || result.error);
|
|
||||||
document.getElementById("editorContainer")?.remove();
|
|
||||||
loadFileList();
|
|
||||||
})
|
|
||||||
.catch(error => console.error("Error saving file:", error));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== NEW CODE: Copy & Move Functions =====
|
// ===== NEW CODE: Copy & Move Functions =====
|
||||||
|
|
||||||
@@ -159,6 +94,10 @@ export function copySelectedFiles() {
|
|||||||
alert("Please select a target folder.");
|
alert("Please select a target folder.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (currentFolder === targetFolder) {
|
||||||
|
alert("Cannot copy files to the same folder.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Send the correct keys
|
// Send the correct keys
|
||||||
sendRequest("copyFiles.php", "POST", {
|
sendRequest("copyFiles.php", "POST", {
|
||||||
source: currentFolder,
|
source: currentFolder,
|
||||||
@@ -172,8 +111,6 @@ export function copySelectedFiles() {
|
|||||||
.catch(error => console.error("Error copying files:", error));
|
.catch(error => console.error("Error copying files:", error));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export function moveSelectedFiles() {
|
export function moveSelectedFiles() {
|
||||||
const selectedFiles = Array.from(document.querySelectorAll(".file-checkbox:checked"))
|
const selectedFiles = Array.from(document.querySelectorAll(".file-checkbox:checked"))
|
||||||
.map(checkbox => checkbox.value);
|
.map(checkbox => checkbox.value);
|
||||||
@@ -186,11 +123,10 @@ export function moveSelectedFiles() {
|
|||||||
alert("Please select a target folder.");
|
alert("Please select a target folder.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log("Payload:", {
|
if (currentFolder === targetFolder) {
|
||||||
source: currentFolder,
|
alert("Cannot move files to the same folder.");
|
||||||
destination: document.getElementById("copyMoveFolderSelect").value,
|
return;
|
||||||
files: selectedFiles
|
}
|
||||||
});
|
|
||||||
sendRequest("moveFiles.php", "POST", {
|
sendRequest("moveFiles.php", "POST", {
|
||||||
source: currentFolder,
|
source: currentFolder,
|
||||||
destination: targetFolder,
|
destination: targetFolder,
|
||||||
@@ -204,14 +140,13 @@ export function moveSelectedFiles() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Populate the Copy/Move folder dropdown
|
// Populate the Copy/Move folder dropdown
|
||||||
export function loadCopyMoveFolderList() {
|
export function loadCopyMoveFolderList() {
|
||||||
$.get('getFolderList.php', function (response) {
|
$.get('getFolderList.php', function (response) {
|
||||||
const folderSelect = $('#copyMoveFolderSelect');
|
const folderSelect = $('#copyMoveFolderSelect');
|
||||||
folderSelect.empty();
|
folderSelect.empty();
|
||||||
// Always add a "Root" option as the default.
|
// Always add a "Root" option as the default.
|
||||||
folderSelect.append($('<option>', { value: "root", text: "Root" }));
|
folderSelect.append($('<option>', { value: "root", text: "(Root)" }));
|
||||||
if (Array.isArray(response) && response.length > 0) {
|
if (Array.isArray(response) && response.length > 0) {
|
||||||
response.forEach(function (folder) {
|
response.forEach(function (folder) {
|
||||||
folderSelect.append($('<option>', {
|
folderSelect.append($('<option>', {
|
||||||
@@ -224,11 +159,8 @@ export function loadCopyMoveFolderList() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Attach functions to window for inline onclick support
|
// Attach functions to window for inline onclick support
|
||||||
window.toggleDeleteButton = toggleDeleteButton;
|
|
||||||
window.toggleAllCheckboxes = toggleAllCheckboxes;
|
window.toggleAllCheckboxes = toggleAllCheckboxes;
|
||||||
window.deleteSelectedFiles = deleteSelectedFiles;
|
window.deleteSelectedFiles = deleteSelectedFiles;
|
||||||
window.editFile = editFile;
|
|
||||||
window.saveFile = saveFile;
|
|
||||||
window.loadFileList = loadFileList;
|
window.loadFileList = loadFileList;
|
||||||
window.copySelectedFiles = copySelectedFiles;
|
window.copySelectedFiles = copySelectedFiles;
|
||||||
window.moveSelectedFiles = moveSelectedFiles;
|
window.moveSelectedFiles = moveSelectedFiles;
|
||||||
|
|||||||
46
utils.js
46
utils.js
@@ -428,6 +428,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
document.getElementById("deleteSelectedBtn").style.display = "none";
|
document.getElementById("deleteSelectedBtn").style.display = "none";
|
||||||
document.getElementById("copySelectedBtn").style.display = "none";
|
document.getElementById("copySelectedBtn").style.display = "none";
|
||||||
document.getElementById("moveSelectedBtn").style.display = "none";
|
document.getElementById("moveSelectedBtn").style.display = "none";
|
||||||
|
document.getElementById("copyMoveFolderSelect").style.display = "none";
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => console.error("Error loading file list:", error));
|
.catch(error => console.error("Error loading file list:", error));
|
||||||
@@ -499,11 +500,14 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
deleteBtn.style.display = "block";
|
deleteBtn.style.display = "block";
|
||||||
copyBtn.style.display = "block";
|
copyBtn.style.display = "block";
|
||||||
moveBtn.style.display = "block";
|
moveBtn.style.display = "block";
|
||||||
|
document.getElementById("copyMoveFolderSelect").style.display = "inline-block";
|
||||||
} else {
|
} else {
|
||||||
deleteBtn.style.display = "none";
|
deleteBtn.style.display = "none";
|
||||||
copyBtn.style.display = "none";
|
copyBtn.style.display = "none";
|
||||||
moveBtn.style.display = "none";
|
moveBtn.style.display = "none";
|
||||||
|
document.getElementById("copyMoveFolderSelect").style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sortFiles(column, folder) {
|
function sortFiles(column, folder) {
|
||||||
@@ -534,28 +538,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Update the visibility and enabled state of the Delete, Copy, and Move buttons
|
|
||||||
function updateDeleteSelectedVisibility() {
|
|
||||||
const checkboxes = document.querySelectorAll(".file-checkbox");
|
|
||||||
const deleteBtn = document.getElementById("deleteSelectedBtn");
|
|
||||||
const copyBtn = document.getElementById("copySelectedBtn");
|
|
||||||
const moveBtn = document.getElementById("moveSelectedBtn");
|
|
||||||
if (checkboxes.length > 0) {
|
|
||||||
// Show all three action buttons
|
|
||||||
deleteBtn.style.display = "inline-block";
|
|
||||||
copyBtn.style.display = "inline-block";
|
|
||||||
moveBtn.style.display = "inline-block";
|
|
||||||
let anyChecked = false;
|
|
||||||
checkboxes.forEach(chk => { if (chk.checked) anyChecked = true; });
|
|
||||||
deleteBtn.disabled = !anyChecked;
|
|
||||||
copyBtn.disabled = !anyChecked;
|
|
||||||
moveBtn.disabled = !anyChecked;
|
|
||||||
} else {
|
|
||||||
deleteBtn.style.display = "none";
|
|
||||||
copyBtn.style.display = "none";
|
|
||||||
moveBtn.style.display = "none";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete Selected Files handler (existing)
|
// Delete Selected Files handler (existing)
|
||||||
function handleDeleteSelected(e) {
|
function handleDeleteSelected(e) {
|
||||||
@@ -706,9 +689,23 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
if (existingEditor) { existingEditor.remove(); }
|
if (existingEditor) { existingEditor.remove(); }
|
||||||
const folderUsed = folder || currentFolder || "root";
|
const folderUsed = folder || currentFolder || "root";
|
||||||
const folderPath = (folderUsed === "root") ? "uploads/" : "uploads/" + encodeURIComponent(folderUsed) + "/";
|
const folderPath = (folderUsed === "root") ? "uploads/" : "uploads/" + encodeURIComponent(folderUsed) + "/";
|
||||||
fetch(folderPath + encodeURIComponent(fileName) + "?t=" + new Date().getTime())
|
const fileUrl = folderPath + encodeURIComponent(fileName) + "?t=" + new Date().getTime();
|
||||||
|
|
||||||
|
// First, use a HEAD request to check file size
|
||||||
|
fetch(fileUrl, { method: "HEAD" })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
if (!response.ok) { throw new Error("HTTP error! Status: " + response.status); }
|
const contentLength = response.headers.get("Content-Length");
|
||||||
|
if (contentLength && parseInt(contentLength) > 10485760) {
|
||||||
|
alert("This file is larger than 10 MB and cannot be edited in the browser.");
|
||||||
|
throw new Error("File too large.");
|
||||||
|
}
|
||||||
|
// File size is acceptable; now fetch the file content
|
||||||
|
return fetch(fileUrl);
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("HTTP error! Status: " + response.status);
|
||||||
|
}
|
||||||
return response.text();
|
return response.text();
|
||||||
})
|
})
|
||||||
.then(content => {
|
.then(content => {
|
||||||
@@ -728,6 +725,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
|||||||
})
|
})
|
||||||
.catch(error => console.error("Error loading file:", error));
|
.catch(error => console.error("Error loading file:", error));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
window.saveFile = function (fileName, folder) {
|
window.saveFile = function (fileName, folder) {
|
||||||
const editor = document.getElementById("fileEditor");
|
const editor = document.getElementById("fileEditor");
|
||||||
|
|||||||
Reference in New Issue
Block a user