/** * OxiCloud - Context Menus and Dialogs Module * This file handles context menus and dialog functionality */ // Context Menus Module const contextMenus = { /** * Assign events to menu items and dialogs */ assignMenuEvents() { // Folder context menu options document.getElementById('download-folder-option').addEventListener('click', () => { if (window.app.contextMenuTargetFolder) { window.fileOps.downloadFolder( window.app.contextMenuTargetFolder.id, window.app.contextMenuTargetFolder.name ); } window.ui.closeContextMenu(); }); document.getElementById('favorite-folder-option').addEventListener('click', () => { if (window.app.contextMenuTargetFolder) { const folder = window.app.contextMenuTargetFolder; // Check if folder is already in favorites to toggle if (window.favorites && window.favorites.isFavorite(folder.id, 'folder')) { // Remove from favorites window.favorites.removeFromFavorites(folder.id, 'folder'); // Update menu item text document.getElementById('favorite-folder-option').querySelector('span').textContent = window.i18n ? window.i18n.t('actions.favorite') : 'Añadir a favoritos'; } else { // Add to favorites window.favorites.addToFavorites( folder.id, folder.name, 'folder', folder.parent_id ); // Update menu item text document.getElementById('favorite-folder-option').querySelector('span').textContent = window.i18n ? window.i18n.t('actions.unfavorite') : 'Quitar de favoritos'; } } window.ui.closeContextMenu(); }); document.getElementById('rename-folder-option').addEventListener('click', () => { if (window.app.contextMenuTargetFolder) { this.showRenameDialog(window.app.contextMenuTargetFolder); } window.ui.closeContextMenu(); }); document.getElementById('move-folder-option').addEventListener('click', () => { if (window.app.contextMenuTargetFolder) { this.showMoveDialog(window.app.contextMenuTargetFolder, 'folder'); } window.ui.closeContextMenu(); }); document.getElementById('share-folder-option').addEventListener('click', () => { if (window.app.contextMenuTargetFolder) { this.showShareDialog(window.app.contextMenuTargetFolder, 'folder'); } window.ui.closeContextMenu(); }); document.getElementById('delete-folder-option').addEventListener('click', async () => { if (window.app.contextMenuTargetFolder) { await window.fileOps.deleteFolder( window.app.contextMenuTargetFolder.id, window.app.contextMenuTargetFolder.name ); } window.ui.closeContextMenu(); }); // File context menu options document.getElementById('view-file-option').addEventListener('click', () => { if (window.app.contextMenuTargetFile) { // Fetch file details to get the mime type fetch(`/api/files/${window.app.contextMenuTargetFile.id}?metadata=true`) .then(response => response.json()) .then(fileDetails => { // Check if viewable file type if ((fileDetails.mime_type && fileDetails.mime_type.startsWith('image/')) || (fileDetails.mime_type && fileDetails.mime_type === 'application/pdf')) { // Open with inline viewer if (window.inlineViewer) { window.inlineViewer.openFile(fileDetails); } else if (window.fileViewer) { window.fileViewer.open(fileDetails); } else { // If no viewer is available, download directly window.fileOps.downloadFile( window.app.contextMenuTargetFile.id, window.app.contextMenuTargetFile.name ); } } else { // For non-viewable files, download window.fileOps.downloadFile( window.app.contextMenuTargetFile.id, window.app.contextMenuTargetFile.name ); } }) .catch(error => { console.error('Error fetching file details:', error); // On error, fallback to download window.fileOps.downloadFile( window.app.contextMenuTargetFile.id, window.app.contextMenuTargetFile.name ); }); } window.ui.closeFileContextMenu(); }); document.getElementById('download-file-option').addEventListener('click', () => { if (window.app.contextMenuTargetFile) { window.fileOps.downloadFile( window.app.contextMenuTargetFile.id, window.app.contextMenuTargetFile.name ); } window.ui.closeFileContextMenu(); }); document.getElementById('favorite-file-option').addEventListener('click', () => { if (window.app.contextMenuTargetFile) { const file = window.app.contextMenuTargetFile; // Check if file is already in favorites to toggle if (window.favorites && window.favorites.isFavorite(file.id, 'file')) { // Remove from favorites window.favorites.removeFromFavorites(file.id, 'file'); // Update menu item text document.getElementById('favorite-file-option').querySelector('span').textContent = window.i18n ? window.i18n.t('actions.favorite') : 'Añadir a favoritos'; } else { // Add to favorites window.favorites.addToFavorites( file.id, file.name, 'file', file.folder_id ); // Update menu item text document.getElementById('favorite-file-option').querySelector('span').textContent = window.i18n ? window.i18n.t('actions.unfavorite') : 'Quitar de favoritos'; } } window.ui.closeFileContextMenu(); }); document.getElementById('move-file-option').addEventListener('click', () => { if (window.app.contextMenuTargetFile) { this.showMoveDialog(window.app.contextMenuTargetFile, 'file'); } window.ui.closeFileContextMenu(); }); document.getElementById('share-file-option').addEventListener('click', () => { if (window.app.contextMenuTargetFile) { this.showShareDialog(window.app.contextMenuTargetFile, 'file'); } window.ui.closeFileContextMenu(); }); document.getElementById('delete-file-option').addEventListener('click', async () => { if (window.app.contextMenuTargetFile) { await window.fileOps.deleteFile( window.app.contextMenuTargetFile.id, window.app.contextMenuTargetFile.name ); } window.ui.closeFileContextMenu(); }); // Rename dialog events const renameCancelBtn = document.getElementById('rename-cancel-btn'); const renameConfirmBtn = document.getElementById('rename-confirm-btn'); const renameInput = document.getElementById('rename-input'); renameCancelBtn.addEventListener('click', this.closeRenameDialog); renameConfirmBtn.addEventListener('click', this.renameFolder); // Rename on Enter key renameInput.addEventListener('keyup', (e) => { if (e.key === 'Enter') { this.renameFolder(); } else if (e.key === 'Escape') { this.closeRenameDialog(); } }); // Move dialog events const moveCancelBtn = document.getElementById('move-cancel-btn'); const moveConfirmBtn = document.getElementById('move-confirm-btn'); moveCancelBtn.addEventListener('click', this.closeMoveDialog); moveConfirmBtn.addEventListener('click', async () => { if (window.app.moveDialogMode === 'file' && window.app.contextMenuTargetFile) { const success = await window.fileOps.moveFile( window.app.contextMenuTargetFile.id, window.app.selectedTargetFolderId ); if (success) { this.closeMoveDialog(); } } else if (window.app.moveDialogMode === 'folder' && window.app.contextMenuTargetFolder) { const success = await window.fileOps.moveFolder( window.app.contextMenuTargetFolder.id, window.app.selectedTargetFolderId ); if (success) { this.closeMoveDialog(); } } }); }, /** * Show rename dialog for a folder * @param {Object} folder - Folder object */ showRenameDialog(folder) { const renameInput = document.getElementById('rename-input'); const renameDialog = document.getElementById('rename-dialog'); renameInput.value = folder.name; renameDialog.style.display = 'flex'; renameInput.focus(); renameInput.select(); }, /** * Close rename dialog */ closeRenameDialog() { document.getElementById('rename-dialog').style.display = 'none'; window.app.contextMenuTargetFolder = null; }, /** * Show move dialog for a file or folder * @param {Object} item - File or folder object * @param {string} mode - 'file' or 'folder' */ async showMoveDialog(item, mode) { // Set mode window.app.moveDialogMode = mode; // Reset selection window.app.selectedTargetFolderId = ""; // Update dialog title const dialogHeader = document.getElementById('move-file-dialog').querySelector('.rename-dialog-header'); dialogHeader.textContent = mode === 'file' ? (window.i18n ? window.i18n.t('dialogs.move_file') : 'Mover archivo') : (window.i18n ? window.i18n.t('dialogs.move_folder') : 'Mover carpeta'); // Load all available folders await this.loadAllFolders(item.id, mode); // Show dialog document.getElementById('move-file-dialog').style.display = 'flex'; }, /** * Close move dialog */ closeMoveDialog() { document.getElementById('move-file-dialog').style.display = 'none'; window.app.contextMenuTargetFile = null; window.app.contextMenuTargetFolder = null; }, /** * Rename the selected folder */ async renameFolder() { if (!window.app.contextMenuTargetFolder) return; const newName = document.getElementById('rename-input').value.trim(); if (!newName) { alert(window.i18n ? window.i18n.t('errors.empty_name') : 'El nombre no puede estar vacío'); return; } const success = await window.fileOps.renameFolder(window.app.contextMenuTargetFolder.id, newName); if (success) { contextMenus.closeRenameDialog(); window.loadFiles(); } }, /** * Load all folders for the move dialog * @param {string} itemId - ID of the item being moved * @param {string} mode - 'file' or 'folder' */ async loadAllFolders(itemId, mode) { try { const response = await fetch('/api/folders'); if (response.ok) { const folders = await response.json(); const folderSelectContainer = document.getElementById('folder-select-container'); // Clear container except root option folderSelectContainer.innerHTML = `