fix trashing in rooms
This commit is contained in:
@@ -44,35 +44,49 @@ export class FileManager {
|
||||
}
|
||||
}
|
||||
|
||||
async deleteFile(fileId) {
|
||||
console.log('[FileManager] Deleting file:', fileId);
|
||||
async deleteFile(filename, path = '') {
|
||||
console.log('[FileManager] Deleting file:', { filename, path });
|
||||
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||
|
||||
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',
|
||||
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) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
console.log('[FileManager] Delete result:', result);
|
||||
|
||||
if (result.success) {
|
||||
this.currentFiles = this.currentFiles.filter(file => file.id !== fileId);
|
||||
await this.roomManager.viewManager.renderFiles(this.currentFiles);
|
||||
console.log('[FileManager] File deleted and view updated');
|
||||
return { success: true, message: 'File moved to trash' };
|
||||
console.log('[FileManager] File deleted successfully');
|
||||
await this.fetchFiles();
|
||||
// Clear any existing error message
|
||||
const errorEl = document.getElementById('fileError');
|
||||
if (errorEl) {
|
||||
errorEl.textContent = '';
|
||||
}
|
||||
} 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) {
|
||||
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.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,12 +56,29 @@ export class ModalManager {
|
||||
}
|
||||
|
||||
showDeleteModal(filename, path = '') {
|
||||
console.log('[ModalManager] Showing delete modal for:', { filename, path });
|
||||
const fileNameEl = document.getElementById('deleteFileName');
|
||||
const labelEl = document.getElementById('deleteConfirmLabel');
|
||||
const confirmBtn = document.getElementById('confirmDeleteBtn');
|
||||
|
||||
if (fileNameEl) fileNameEl.textContent = filename;
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,9 @@ class RoomManager {
|
||||
await this.fileManager.fetchFiles();
|
||||
console.log('[RoomManager] Files fetched');
|
||||
|
||||
// Render breadcrumb after files are fetched
|
||||
this.viewManager.renderBreadcrumb();
|
||||
|
||||
console.log('[RoomManager] Initializing search...');
|
||||
// Initialize search
|
||||
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() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const path = urlParams.get('path') || '';
|
||||
console.log('[RoomManager] Getting path from URL:', path);
|
||||
return path;
|
||||
return urlParams.get('path') || '';
|
||||
}
|
||||
|
||||
initializeEventListeners() {
|
||||
@@ -117,54 +151,18 @@ class RoomManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the room manager when the script loads
|
||||
function initializeRoom() {
|
||||
console.log('[RoomManager] Starting room initialization...');
|
||||
// Initialize the room manager when the DOM is loaded
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
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 {
|
||||
// Wait for all meta tags to be available
|
||||
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);
|
||||
}
|
||||
// Create and expose the room manager instance
|
||||
window.roomManager = new RoomManager(config);
|
||||
});
|
||||
@@ -89,24 +89,35 @@ export class ViewManager {
|
||||
const upBtn = document.getElementById('upBtn');
|
||||
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) {
|
||||
console.log('[ViewManager] No path, hiding breadcrumb');
|
||||
breadcrumb.innerHTML = '';
|
||||
console.log('[ViewManager] No path, showing only root');
|
||||
breadcrumb.innerHTML = html;
|
||||
upBtn.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[ViewManager] Building breadcrumb for path:', path);
|
||||
const parts = path.split('/').filter(Boolean);
|
||||
let html = '';
|
||||
let currentPath = '';
|
||||
|
||||
parts.forEach((part, index) => {
|
||||
currentPath += '/' + part;
|
||||
html += `
|
||||
<span class="d-flex align-items-center">
|
||||
${index > 0 ? '<i class="fas fa-chevron-right mx-2 text-muted"></i>' : ''}
|
||||
<a href="?path=${encodeURIComponent(currentPath)}" class="text-decoration-none">
|
||||
<i class="fas fa-chevron-right mx-2" style="color: var(--secondary-color);"></i>
|
||||
<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}
|
||||
</a>
|
||||
</span>
|
||||
@@ -115,6 +126,17 @@ export class ViewManager {
|
||||
|
||||
breadcrumb.innerHTML = html;
|
||||
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');
|
||||
}
|
||||
|
||||
@@ -179,7 +201,9 @@ export class ViewManager {
|
||||
const modified = new Date(file.modified).toLocaleString();
|
||||
|
||||
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>
|
||||
<input type="checkbox" class="form-check-input select-item-checkbox"
|
||||
data-index="${index}" style="margin: 0;"
|
||||
@@ -211,7 +235,10 @@ export class ViewManager {
|
||||
|
||||
return `
|
||||
<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="mb-2 w-100 d-flex justify-content-start">
|
||||
<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) {
|
||||
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);">
|
||||
<i class="fas fa-trash"></i>
|
||||
</button>
|
||||
|
||||
Reference in New Issue
Block a user