fix trashing in rooms

This commit is contained in:
2025-05-28 13:45:32 +02:00
parent d77dcec068
commit 2a1b6f8a22
4 changed files with 130 additions and 81 deletions

View File

@@ -44,35 +44,49 @@ export class FileManager {
} }
} }
async deleteFile(fileId) { async deleteFile(filename, path = '') {
console.log('[FileManager] Deleting file:', fileId); console.log('[FileManager] Deleting file:', { filename, path });
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
try { try {
const response = await fetch(`/api/rooms/${this.roomManager.roomId}/files/${fileId}`, { let url = `/api/rooms/${this.roomManager.roomId}/files/${encodeURIComponent(filename)}`;
if (path) {
url += `?path=${encodeURIComponent(path)}`;
}
const response = await fetch(url, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content') 'X-CSRFToken': csrfToken
} }
}); });
console.log('[FileManager] Delete response status:', response.status);
if (!response.ok) { if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`); throw new Error(`HTTP error! status: ${response.status}`);
} }
const result = await response.json(); const result = await response.json();
console.log('[FileManager] Delete result:', result);
if (result.success) { if (result.success) {
this.currentFiles = this.currentFiles.filter(file => file.id !== fileId); console.log('[FileManager] File deleted successfully');
await this.roomManager.viewManager.renderFiles(this.currentFiles); await this.fetchFiles();
console.log('[FileManager] File deleted and view updated'); // Clear any existing error message
return { success: true, message: 'File moved to trash' }; const errorEl = document.getElementById('fileError');
if (errorEl) {
errorEl.textContent = '';
}
} else { } else {
throw new Error(result.message || 'Failed to delete file'); console.error('[FileManager] Failed to delete file:', result.error);
const errorEl = document.getElementById('fileError');
if (errorEl) {
errorEl.textContent = result.error || 'Failed to delete file.';
}
} }
} catch (error) { } catch (error) {
console.error('[FileManager] Error deleting file:', error); console.error('[FileManager] Error deleting file:', error);
return { success: false, message: error.message }; const errorEl = document.getElementById('fileError');
if (errorEl) {
errorEl.textContent = 'Failed to delete file. Please try again.';
}
} }
} }

View File

@@ -56,12 +56,29 @@ export class ModalManager {
} }
showDeleteModal(filename, path = '') { showDeleteModal(filename, path = '') {
console.log('[ModalManager] Showing delete modal for:', { filename, path });
const fileNameEl = document.getElementById('deleteFileName'); const fileNameEl = document.getElementById('deleteFileName');
const labelEl = document.getElementById('deleteConfirmLabel'); const labelEl = document.getElementById('deleteConfirmLabel');
const confirmBtn = document.getElementById('confirmDeleteBtn');
if (fileNameEl) fileNameEl.textContent = filename; if (fileNameEl) fileNameEl.textContent = filename;
if (labelEl) labelEl.textContent = 'Move to Trash'; if (labelEl) labelEl.textContent = 'Move to Trash';
// Store the file info in the FileManager
this.roomManager.fileManager.fileToDelete = {
name: filename,
path: path
};
// Set up the confirm button click handler
if (confirmBtn) {
confirmBtn.onclick = () => {
console.log('[ModalManager] Delete confirmed for:', { filename, path });
this.roomManager.fileManager.deleteFile(filename, path);
this.deleteModal.hide();
};
}
this.deleteModal.show(); this.deleteModal.show();
} }

View File

@@ -49,6 +49,9 @@ class RoomManager {
await this.fileManager.fetchFiles(); await this.fileManager.fetchFiles();
console.log('[RoomManager] Files fetched'); console.log('[RoomManager] Files fetched');
// Render breadcrumb after files are fetched
this.viewManager.renderBreadcrumb();
console.log('[RoomManager] Initializing search...'); console.log('[RoomManager] Initializing search...');
// Initialize search // Initialize search
this.searchManager.initialize(); this.searchManager.initialize();
@@ -65,11 +68,42 @@ class RoomManager {
} }
} }
navigateToFolder(folderName) {
console.log('[RoomManager] Navigating to folder:', folderName);
const newPath = this.currentPath ? `${this.currentPath}/${folderName}` : folderName;
this.navigateTo(newPath);
}
navigateToParent() {
console.log('[RoomManager] Navigating to parent folder');
if (!this.currentPath) return;
const parts = this.currentPath.split('/');
parts.pop(); // Remove the last part
const newPath = parts.join('/');
this.navigateTo(newPath);
}
navigateTo(path) {
console.log('[RoomManager] Navigating to path:', path);
this.currentPath = path;
// Update the URL with the current path
const url = new URL(window.location);
if (this.currentPath) {
url.searchParams.set('path', this.currentPath);
} else {
url.searchParams.delete('path');
}
window.history.replaceState({}, '', url);
this.fileManager.fetchFiles().then(() => {
// Render breadcrumb after files are fetched
this.viewManager.renderBreadcrumb();
});
}
getPathFromUrl() { getPathFromUrl() {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const path = urlParams.get('path') || ''; return urlParams.get('path') || '';
console.log('[RoomManager] Getting path from URL:', path);
return path;
} }
initializeEventListeners() { initializeEventListeners() {
@@ -117,54 +151,18 @@ class RoomManager {
} }
} }
// Initialize the room manager when the script loads // Initialize the room manager when the DOM is loaded
function initializeRoom() { document.addEventListener('DOMContentLoaded', () => {
console.log('[RoomManager] Starting room initialization...'); const config = {
roomId: document.querySelector('meta[name="room-id"]').getAttribute('content'),
canDelete: document.querySelector('meta[name="can-delete"]').getAttribute('content') === 'true',
canShare: document.querySelector('meta[name="can-share"]').getAttribute('content') === 'true',
canUpload: document.querySelector('meta[name="can-upload"]').getAttribute('content') === 'true',
canDownload: document.querySelector('meta[name="can-download"]').getAttribute('content') === 'true',
canRename: document.querySelector('meta[name="can-rename"]').getAttribute('content') === 'true',
canMove: document.querySelector('meta[name="can-move"]').getAttribute('content') === 'true'
};
try { // Create and expose the room manager instance
// Wait for all meta tags to be available window.roomManager = new RoomManager(config);
const requiredMetaTags = [ });
'room-id',
'can-delete',
'can-share',
'can-upload',
'can-download',
'can-rename',
'can-move'
];
console.log('[RoomManager] Checking required meta tags...');
const missingTags = requiredMetaTags.filter(tag => !document.querySelector(`meta[name="${tag}"]`));
if (missingTags.length > 0) {
console.error('[RoomManager] Missing required meta tags:', missingTags);
return;
}
console.log('[RoomManager] All meta tags found, creating config...');
const config = {
roomId: document.querySelector('meta[name="room-id"]').getAttribute('content'),
canDelete: document.querySelector('meta[name="can-delete"]').getAttribute('content') === 'true',
canShare: document.querySelector('meta[name="can-share"]').getAttribute('content') === 'true',
canUpload: document.querySelector('meta[name="can-upload"]').getAttribute('content') === 'true',
canDownload: document.querySelector('meta[name="can-download"]').getAttribute('content') === 'true',
canRename: document.querySelector('meta[name="can-rename"]').getAttribute('content') === 'true',
canMove: document.querySelector('meta[name="can-move"]').getAttribute('content') === 'true'
};
console.log('[RoomManager] Config created:', config);
window.roomManager = new RoomManager(config);
} catch (error) {
console.error('[RoomManager] Error during initialization:', error);
}
}
// Wait for DOM to be fully loaded
if (document.readyState === 'loading') {
console.log('[RoomManager] DOM still loading, waiting for DOMContentLoaded...');
document.addEventListener('DOMContentLoaded', initializeRoom);
} else {
console.log('[RoomManager] DOM already loaded, initializing with delay...');
// If DOM is already loaded, wait a bit to ensure all scripts are loaded
setTimeout(initializeRoom, 0);
}

View File

@@ -89,24 +89,35 @@ export class ViewManager {
const upBtn = document.getElementById('upBtn'); const upBtn = document.getElementById('upBtn');
const path = this.roomManager.currentPath; const path = this.roomManager.currentPath;
// Always show the root node
let html = `
<span class="d-flex align-items-center">
<a href="?path=" class="text-decoration-none" style="color: var(--primary-color);">
<i class="fas fa-home"></i>
</a>
</span>
`;
if (!path) { if (!path) {
console.log('[ViewManager] No path, hiding breadcrumb'); console.log('[ViewManager] No path, showing only root');
breadcrumb.innerHTML = ''; breadcrumb.innerHTML = html;
upBtn.style.display = 'none'; upBtn.style.display = 'none';
return; return;
} }
console.log('[ViewManager] Building breadcrumb for path:', path); console.log('[ViewManager] Building breadcrumb for path:', path);
const parts = path.split('/').filter(Boolean); const parts = path.split('/').filter(Boolean);
let html = '';
let currentPath = ''; let currentPath = '';
parts.forEach((part, index) => { parts.forEach((part, index) => {
currentPath += '/' + part; currentPath += '/' + part;
html += ` html += `
<span class="d-flex align-items-center"> <span class="d-flex align-items-center">
${index > 0 ? '<i class="fas fa-chevron-right mx-2 text-muted"></i>' : ''} <i class="fas fa-chevron-right mx-2" style="color: var(--secondary-color);"></i>
<a href="?path=${encodeURIComponent(currentPath)}" class="text-decoration-none"> <a href="?path=${encodeURIComponent(currentPath)}" class="text-decoration-none"
style="color: var(--primary-color);"
onmouseover="this.style.color='var(--primary-light)'"
onmouseout="this.style.color='var(--primary-color)'">
${part} ${part}
</a> </a>
</span> </span>
@@ -115,6 +126,17 @@ export class ViewManager {
breadcrumb.innerHTML = html; breadcrumb.innerHTML = html;
upBtn.style.display = 'inline-block'; upBtn.style.display = 'inline-block';
upBtn.onclick = () => this.roomManager.navigateToParent();
upBtn.style.color = 'var(--primary-color)';
upBtn.style.borderColor = 'var(--primary-color)';
upBtn.onmouseover = () => {
upBtn.style.backgroundColor = 'var(--primary-color)';
upBtn.style.color = 'white';
};
upBtn.onmouseout = () => {
upBtn.style.backgroundColor = 'transparent';
upBtn.style.color = 'var(--primary-color)';
};
console.log('[ViewManager] Breadcrumb rendered'); console.log('[ViewManager] Breadcrumb rendered');
} }
@@ -179,7 +201,9 @@ export class ViewManager {
const modified = new Date(file.modified).toLocaleString(); const modified = new Date(file.modified).toLocaleString();
return ` return `
<tr data-index="${index}" class="file-row" style="cursor: pointer;" onclick="window.roomManager.fileManager.updateSelection(${index}, event)"> <tr data-index="${index}" class="file-row" style="cursor: pointer;"
onclick="window.roomManager.fileManager.updateSelection(${index}, event)"
ondblclick="${isFolder ? `window.roomManager.navigateToFolder('${file.name}')` : ''}">
<td> <td>
<input type="checkbox" class="form-check-input select-item-checkbox" <input type="checkbox" class="form-check-input select-item-checkbox"
data-index="${index}" style="margin: 0;" data-index="${index}" style="margin: 0;"
@@ -211,7 +235,10 @@ export class ViewManager {
return ` return `
<div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-3"> <div class="col-12 col-sm-6 col-md-4 col-lg-3 mb-3">
<div class="card file-card h-100 border-0 shadow-sm position-relative" data-index="${index}" onclick="window.roomManager.fileManager.updateSelection(${index}, event)"> <div class="card file-card h-100 border-0 shadow-sm position-relative"
data-index="${index}"
onclick="window.roomManager.fileManager.updateSelection(${index}, event)"
ondblclick="${isFolder ? `window.roomManager.navigateToFolder('${file.name}')` : ''}">
<div class="card-body d-flex flex-column align-items-center justify-content-center text-center p-4"> <div class="card-body d-flex flex-column align-items-center justify-content-center text-center p-4">
<div class="mb-2 w-100 d-flex justify-content-start"> <div class="mb-2 w-100 d-flex justify-content-start">
<input type="checkbox" class="form-check-input select-item-checkbox" <input type="checkbox" class="form-check-input select-item-checkbox"
@@ -273,16 +300,9 @@ export class ViewManager {
`); `);
} }
actions.push(`
<button class="btn btn-sm file-action-btn" title="${file.is_starred ? 'Unstar' : 'Star'}" onclick="window.roomManager.fileManager.toggleStar('${file.id}')"
style="background-color:${file.is_starred ? 'var(--warning-opacity-15)' : 'var(--primary-opacity-8)'};color:${file.is_starred ? 'var(--warning-color)' : 'var(--primary-color)'};">
<i class="fas fa-star"></i>
</button>
`);
if (this.roomManager.canDelete) { if (this.roomManager.canDelete) {
actions.push(` actions.push(`
<button class="btn btn-sm file-action-btn" title="Delete" onclick="window.roomManager.modalManager.showDeleteModal('${file.id}')" <button class="btn btn-sm file-action-btn" title="Delete" onclick="window.roomManager.modalManager.showDeleteModal('${file.name}', '${file.path || ''}')"
style="background-color:var(--danger-opacity-15);color:var(--danger-color);"> style="background-color:var(--danger-opacity-15);color:var(--danger-color);">
<i class="fas fa-trash"></i> <i class="fas fa-trash"></i>
</button> </button>