/** * OxiCloud - UI Module * This file handles UI-related functions, view toggling, and interface interactions */ // @ts-check // UI Module const ui = { /** @type {HTMLDListElement | null} */ //dragPreview, /** * Initialize context menus and dialogs */ initializeContextMenus() { // Folder context menu if (!document.getElementById('folder-context-menu')) { const folderMenu = document.createElement('div'); folderMenu.className = 'context-menu'; folderMenu.id = 'folder-context-menu'; folderMenu.innerHTML = `
Download
Add to favorites
Share
Rename
Move to...
Delete
`; document.body.appendChild(folderMenu); } // File context menu if (!document.getElementById('file-context-menu')) { const fileMenu = document.createElement('div'); fileMenu.className = 'context-menu'; fileMenu.id = 'file-context-menu'; fileMenu.innerHTML = `
View
Download
Add to favorites
Share
Rename
Move to...
Delete
`; document.body.appendChild(fileMenu); } // Rename dialog — modern if (!document.getElementById('rename-dialog')) { const renameDialog = document.createElement('div'); renameDialog.className = 'rename-dialog'; renameDialog.id = 'rename-dialog'; renameDialog.innerHTML = `
Rename
`; document.body.appendChild(renameDialog); } // Move dialog — modern with navigation if (!document.getElementById('move-file-dialog')) { const moveDialog = document.createElement('div'); moveDialog.className = 'rename-dialog'; moveDialog.id = 'move-file-dialog'; moveDialog.innerHTML = `
Move

Select destination folder:

`; document.body.appendChild(moveDialog); } // Share dialog if (!document.getElementById('share-dialog')) { const shareDialog = document.createElement('div'); shareDialog.className = 'share-dialog'; shareDialog.id = 'share-dialog'; shareDialog.innerHTML = `
Share file
Item:

Share options

`; document.body.appendChild(shareDialog); // Add event listeners for share dialog document.getElementById('share-cancel-btn').addEventListener('click', () => { contextMenus.closeShareDialog(); }); document.getElementById('share-confirm-btn').addEventListener('click', async () => { await contextMenus.createSharedLink(); }); document.getElementById('copy-share-btn').addEventListener('click', async () => { const shareUrl = document.getElementById('generated-share-url').value; await fileSharing.copyLinkToClipboard(shareUrl); }); document.getElementById('notify-share-btn').addEventListener('click', () => { const shareUrl = document.getElementById('generated-share-url').value; contextMenus.showEmailNotificationDialog(shareUrl); }); } // Notification dialog if (!document.getElementById('notification-dialog')) { const notificationDialog = document.createElement('div'); notificationDialog.className = 'share-dialog'; notificationDialog.id = 'notification-dialog'; notificationDialog.innerHTML = `
Notify shared link

URL:

`; document.body.appendChild(notificationDialog); // Add event listeners for notification dialog document.getElementById('notification-cancel-btn').addEventListener('click', () => { contextMenus.closeNotificationDialog(); }); document.getElementById('notification-send-btn').addEventListener('click', () => { contextMenus.sendShareNotification(); }); } // Assign events to menu items if (window.contextMenus) { window.contextMenus.assignMenuEvents(); } else { console.warn('contextMenus module not loaded'); } }, /** * Set up drag and drop functionality */ setupDragAndDrop() { // prepare area to build dragged elements this.dragPreview = document.createElement('div'); this.dragPreview.className = 'drag-preview'; document.body.appendChild(this.dragPreview); this.draggedItems = null; const dropzone = document.getElementById('dropzone'); const collectDroppedEntries = async (dataTransfer) => { const items = Array.from(dataTransfer?.items || []); const rootEntries = items.map((it) => (typeof it.webkitGetAsEntry === 'function' ? it.webkitGetAsEntry() : null)).filter(Boolean); if (rootEntries.length === 0) return null; const out = []; const walkEntry = async (entry, prefix = '') => { if (!entry) return; if (entry.isFile) { await new Promise((resolve) => { entry.file( (file) => { out.push({ file, relativePath: `${prefix}${file.name}` }); resolve(); }, () => resolve() ); }); return; } if (entry.isDirectory) { const dirPrefix = `${prefix}${entry.name}/`; const reader = entry.createReader(); while (true) { const children = await new Promise((resolve) => { reader.readEntries(resolve, () => resolve([])); }); if (!children || children.length === 0) break; for (const child of children) { // eslint-disable-next-line no-await-in-loop await walkEntry(child, dirPrefix); } } } }; for (const root of rootEntries) { // eslint-disable-next-line no-await-in-loop await walkEntry(root, ''); } return out; }; // Dropzone events dropzone.addEventListener('dragover', (e) => { e.preventDefault(); dropzone.classList.add('active'); }); dropzone.addEventListener('dragleave', () => { dropzone.classList.remove('active'); }); dropzone.addEventListener('drop', async (e) => { e.preventDefault(); e.stopPropagation(); // Prevent bubbling to document's drop handler (avoids double upload) e._oxiHandled = true; // Mark as handled for document-level fallback dropzone.classList.remove('active'); if (e.dataTransfer.files.length > 0) { // First try directory-aware extraction (Finder folder drag & drop) const droppedEntries = await collectDroppedEntries(e.dataTransfer); if (droppedEntries && droppedEntries.length > 0) { const hasFolderStructure = droppedEntries.some((x) => x.relativePath?.includes('/')); if (hasFolderStructure) { fileOps.uploadFolderEntries(droppedEntries); } else { fileOps.uploadFiles(droppedEntries.map((x) => x.file)); } setTimeout(() => { dropzone.style.display = 'none'; }, 500); return; } // Detect folder drops: files from folder drops have webkitRelativePath set const hasRelativePaths = Array.from(e.dataTransfer.files).some((f) => f.webkitRelativePath?.includes('/')); if (hasRelativePaths) { fileOps.uploadFolderFiles(e.dataTransfer.files); } else { fileOps.uploadFiles(e.dataTransfer.files); } } setTimeout(() => { dropzone.style.display = 'none'; }, 500); }); // Document-wide drag and drop document.addEventListener('dragover', (e) => { e.preventDefault(); if (e.dataTransfer.types.includes('Files')) { dropzone.style.display = 'block'; dropzone.classList.add('active'); } }); document.addEventListener('dragleave', (e) => { if (e.clientX <= 0 || e.clientY <= 0 || e.clientX >= window.innerWidth || e.clientY >= window.innerHeight) { dropzone.classList.remove('active'); setTimeout(() => { if (!dropzone.classList.contains('active')) { dropzone.style.display = 'none'; } }, 100); } }); document.addEventListener('drop', async (e) => { e.preventDefault(); dropzone.classList.remove('active'); // Skip if already handled by the dropzone handler (defensive against bubble leaks) if (e._oxiHandled) return; if (e.dataTransfer.files.length > 0) { // First try directory-aware extraction (Finder folder drag & drop) const droppedEntries = await collectDroppedEntries(e.dataTransfer); if (droppedEntries && droppedEntries.length > 0) { const hasFolderStructure = droppedEntries.some((x) => x.relativePath?.includes('/')); if (hasFolderStructure) { fileOps.uploadFolderEntries(droppedEntries); } else { fileOps.uploadFiles(droppedEntries.map((x) => x.file)); } setTimeout(() => { dropzone.style.display = 'none'; }, 500); return; } // Detect folder drops: files from folder drops have webkitRelativePath set const hasRelativePaths = Array.from(e.dataTransfer.files).some((f) => f.webkitRelativePath?.includes('/')); if (hasRelativePaths) { fileOps.uploadFolderFiles(e.dataTransfer.files); } else { fileOps.uploadFiles(e.dataTransfer.files); } } setTimeout(() => { dropzone.style.display = 'none'; }, 500); }); }, /** * Switch to grid view */ switchToGridView() { this._hydrateViewIfNeeded(); window.app.currentView = 'grid'; localStorage.setItem('oxicloud-view', 'grid'); window.syncViewContainers(); }, /** * Switch to list view */ switchToListView() { this._hydrateViewIfNeeded(); window.app.currentView = 'list'; localStorage.setItem('oxicloud-view', 'list'); window.syncViewContainers(); }, /** * Update breadcrumb navigation from the breadcrumbPath array. * Renders: Home > folder1 > folder2 > ... * Each segment is clickable to navigate back to that level. */ updateBreadcrumb() { const breadcrumb = document.querySelector('.breadcrumb'); breadcrumb.innerHTML = ''; const path = window.app.breadcrumbPath; // [{id, name}, ...] // Helper function to safely get translation text const getTranslatedText = (key, defaultValue) => { if (!window.i18n?.t) return defaultValue; return window.i18n.t(key); }; // -- Home icon (always present, clickable to go to root) -- const homeIcon = document.createElement('span'); homeIcon.className = 'breadcrumb-item breadcrumb-home'; homeIcon.innerHTML = ''; homeIcon.title = getTranslatedText('breadcrumb.home', 'Home'); // Home is always clickable if we have a home folder if (window.app.userHomeFolderId) { homeIcon.classList.add('breadcrumb-link'); homeIcon.addEventListener('click', () => { window.app.breadcrumbPath = []; window.app.currentPath = window.app.userHomeFolderId; this.updateBreadcrumb(); window.loadFiles(); }); } breadcrumb.appendChild(homeIcon); // -- Root/Home folder name (if available) is always the first element of the breadcrumb -- // TODO clarify the difference between homeIcon & this first element if (window.app.userHomeFolderName) { if (path.length === 0 || path[0].id !== window.app.userHomeFolderId) { path.unshift({ name: window.app.userHomeFolderName, id: window.app.userHomeFolderId }); } } // -- Root/Home + Intermediate + current segments -- path.forEach((segment, index) => { const isLast = index === path.length - 1; // Separator const separator = document.createElement('span'); separator.className = 'breadcrumb-separator'; separator.textContent = '>'; breadcrumb.appendChild(separator); // Segment item const item = document.createElement('span'); item.className = 'breadcrumb-item'; item.textContent = segment.name; item.dataset.folderId = segment.id; if (!isLast) { // Intermediate segment: clickable – truncate path to this level item.classList.add('breadcrumb-link'); item.addEventListener('click', () => { window.app.breadcrumbPath = path.slice(0, index + 1); window.app.currentPath = segment.id; this.updateBreadcrumb(); window.loadFiles(); }); // can drag files on this folder // dragover – only folders are valid drop targets item.addEventListener('dragover', (e) => { const card = e.target.closest('span'); if (!card?.dataset.folderId) return; e.preventDefault(); card.classList.add('drop-target'); }); // dragleave item.addEventListener('dragleave', (e) => { console.log('dragleave ', e); const card = e.target.closest('span'); if (!card?.dataset.folderId) return; card.classList.remove('drop-target'); }); // drop – only folders accept drops item.addEventListener('drop', async (e) => { const card = e.target.closest('span'); if (!card) return; const targetFolderId = card.dataset.folderId; if (!targetFolderId) return; e.preventDefault(); card.classList.remove('drop-target'); const action = e.dataTransfer?.dropEffect; await this._dropToFolder(action, targetFolderId, e.dataTransfer); }); } else { // Last segment: current location, not clickable item.classList.add('breadcrumb-current'); } breadcrumb.appendChild(item); }); }, /** * Check if a file can be previewed in the viewer * @param {Object} file - File object with mime_type property * @returns {boolean} */ isViewableFile(file) { return window.uiFileTypes.isViewableFile(file); }, /** * Get FontAwesome icon class for a filename based on its extension. * Used as fallback when the backend DTO doesn't include icon_class * (e.g. trash items). */ getIconClass(fileName) { return window.uiFileTypes.getIconClass(fileName); }, /** * Get CSS special class for icon styling based on filename extension. * Used as fallback when the backend DTO doesn't include icon_special_class. */ getIconSpecialClass(fileName) { return window.uiFileTypes.getIconSpecialClass(fileName); }, /** * Show notification * @param {string} title - Notification title * @param {string} message - Notification message */ showNotification(title, message) { window.uiNotifications.show(title, message); }, /** * Close folder context menu */ closeContextMenu() { const menu = document.getElementById('folder-context-menu'); if (menu) { menu.style.display = 'none'; window.app.contextMenuTargetFolder = null; } }, /** * Close file context menu */ closeFileContextMenu() { const menu = document.getElementById('file-context-menu'); if (menu) { menu.style.display = 'none'; window.app.contextMenuTargetFile = null; } }, /* ================================================================ * Data store + event delegation (replaces per-item listeners) * ================================================================ */ /** @type {Map} item data keyed by id */ _items: new Map(), /** @type {Array} last rendered folder dataset */ _lastFolders: [], /** @type {Array} last rendered file dataset */ _lastFiles: [], /** @type {boolean} */ _delegationReady: false, _getActiveView() { if (window.app && window.app.currentView === 'list') return 'list'; if (window.app && window.app.currentView === 'grid') return 'grid'; const stored = localStorage.getItem('oxicloud-view'); return stored === 'list' ? 'list' : 'grid'; }, /** * * @param {Object[]} folders * @returns */ _renderFoldersToView(folders) { if (!Array.isArray(folders) || folders.length === 0) return; const target = document.getElementById('files-list'); if (!target) return; const frag = document.createDocumentFragment(); for (const folder of folders) { frag.appendChild(this._createFolderItem(folder)); } target.appendChild(frag); }, /** * * @param {Object[]} files * @returns */ _renderFilesToView(files) { if (!Array.isArray(files) || files.length === 0) return; const target = document.getElementById('files-list'); if (!target) return; const frag = document.createDocumentFragment(); for (const file of files) { frag.appendChild(this._createFileItem(file)); } target.appendChild(frag); }, _upsertById(arr, item) { if (!Array.isArray(arr) || !item?.id) return; const idx = arr.findIndex((x) => x && x.id === item.id); if (idx >= 0) { arr[idx] = item; } else { arr.push(item); } }, /** * handle the drop * @param {string} action copy|move * @param {string} targetFolderId the target * @param {any} dataTransfer fallback if nothing is selected */ async _dropToFolder(action, targetFolderId, dataTransfer) { const selection = window.multiSelect.getSelection(targetFolderId); window.multiSelect.clear(); if (selection.fileIds.length === 0 && selection.folderIds.length === 0) { // try to use dataTransfer (direct move without selection) const id = dataTransfer.getData('text/plain'); const isFolder = dataTransfer.getData('application/oxicloud-folder') === 'true'; if (isFolder && id === targetFolderId) { console.log('nothing to do'); return; //nothing to do } // append current item to selection if (isFolder) { console.log(`adding ${id} as folder`); selection.folderIds.push(id); } else { console.log(`adding ${id} as file`); selection.fileIds.push(id); } } console.log(`request ${action} of: `, selection); /* TODO do we prefer use atomic operation on 1 item ? like: await fileOps.moveFolder(sourceId, targetFolderId); await fileOps.moveFile(sourceId, targetFolderId); */ let result; switch (action) { case 'copy': result = await window.fileOps.batchCopy(selection.fileIds, selection.folderIds, targetFolderId); break; case 'move': result = await window.fileOps.batchMove(selection.fileIds, selection.folderIds, targetFolderId); // redraw directory if (result.success > 0) window.loadFiles(); break; default: console.error(`drag and drop: action ${action} unknown`); return; } window.multiSelect.showBatchResult(action, result); console.log(result); }, _hydrateViewIfNeeded() { // Only hydrate if there is at least one rendered item in the opposite/current DOM. // This prevents stale cache hydration in empty-state screens. const hasAnyRenderedItem = !!document.querySelector('#files-list .file-item'); if (!hasAnyRenderedItem) return; // FIXME: thre is the header... const listView = document.getElementById('files-list'); if (!listView) return; if (listView.children.length > 1) return; this._renderFoldersToView(this._lastFolders); this._renderFilesToView(this._lastFiles); }, /** * Attach a fixed set of delegated event listeners to the two * container elements (files-list). * Called once – idempotent. */ initDelegation() { if (this._delegationReady) return; const filesList = document.getElementById('files-list'); if (!filesList) return; this._delegationReady = true; // ── helpers ──────────────────────────────────────────────── const itemInfo = (card) => { if (!card) return null; const fileId = card.dataset.fileId; if (fileId) return { type: 'file', id: fileId, name: card.dataset.fileName, data: this._items.get(fileId) }; const folderId = card.dataset.folderId; if (folderId) return { type: 'folder', id: folderId, name: card.dataset.folderName, data: this._items.get(folderId) }; return null; }; const openFile = async (file) => { if (!file) return; if (window.recent) { document.dispatchEvent(new CustomEvent('file-accessed', { detail: { file } })); } // WOPI editor intercept: open Office documents in the WOPI editor // But NOT image files - those should be previewed in the inline viewer const ext = (file.name || '').split('.').pop().toLowerCase(); const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'bmp', 'ico', 'heic', 'heif', 'avif', 'tiff']; const isImage = file.mime_type?.startsWith('image/') || imageExts.includes(ext); try { if (!isImage && window.wopiEditor && (await window.wopiEditor.canEdit(file.name))) { await window.wopiEditor.openInModal(file.id, file.name, 'edit'); return; } } catch (e) { console.warn(`WOPI Editor failed, falling bck to classic view `, e); } if (this.isViewableFile(file) || isImage) { if (window.inlineViewer) { window.inlineViewer.openFile(file); // update history window.app.viewFile = file.id; window.updateHistory(false); } else { window.fileOps.downloadFile(file.id, file.name); } } else { window.fileOps.downloadFile(file.id, file.name); } }; const navigateFolder = (card) => { const folderId = card.dataset.folderId; const folderName = card.dataset.folderName; window.app.breadcrumbPath.push({ id: folderId, name: folderName }); window.app.currentPath = folderId; this.updateBreadcrumb(); window.loadFiles(); }; const setContextTarget = (card, info) => { if (info.type === 'folder') { window.app.contextMenuTargetFolder = { id: info.id, name: card.dataset.folderName, parent_id: card.dataset.parentId || '' }; } else { window.app.contextMenuTargetFile = { id: info.id, name: card.dataset.fileName, folder_id: card.dataset.folderId || '' }; } }; // ── click (open / navigate; select only via checkbox) ── filesList.addEventListener('click', (e) => { const card = e.target.closest('.file-item'); if (!card) return; if (e.target.closest('.file-actions')) { e.stopPropagation(); e.preventDefault(); const info = itemInfo(card); if (!info) return; setContextTarget(card, info); const menuId = info.type === 'folder' ? 'folder-context-menu' : 'file-context-menu'; showContextMenuAtElement(e.target.closest('.file-actions'), menuId); return; } if (e.target.closest('.checkbox-cell')) { toggleCardSelection(card, e); return; } // Favorite star – handled by direct onclick on the button if (e.target.closest('.favorite-star')) return; // Single-click opens/navigates (selection is only via checkbox) const info = itemInfo(card); if (!info) return; // use modifier key to select/deselect item // note: shift key is used in multiselect // note: on MacOS, ctrl Key is used to convert click into right click, which invoke the `contextmenu` event if (e.metaKey || e.altKey || e.ctrlKey) { toggleCardSelection(card, e); return; } // shiftkey is used to complete selection if (e.shiftKey && window.multiSelect) { window.multiSelect.handleToggleItem(card, e); return; } if (info.type === 'folder') { navigateFolder(card); } else { openFile(info.data); } }); // ── GRID: dblclick (navigate / open) ────────────────────── filesList.addEventListener('dblclick', (e) => { // Single-click already handles open/navigate. // Prevent duplicate actions on double-click. e.preventDefault(); }); // ── shared events ────────────────────── filesList.addEventListener('contextmenu', (e) => { const card = e.target.closest('.file-item'); if (!card) return; e.preventDefault(); const info = itemInfo(card); if (!info) return; setContextTarget(card, info); const menuId = info.type === 'folder' ? 'folder-context-menu' : 'file-context-menu'; const menu = document.getElementById(menuId); if (window.contextMenus && typeof window.contextMenus.syncFavoriteOptionLabels === 'function') { window.contextMenus.syncFavoriteOptionLabels(); } if (window.contextMenus && typeof window.contextMenus.syncWopiOptionVisibility === 'function') { window.contextMenus.syncWopiOptionVisibility().catch(() => {}); } menu.style.left = `${e.pageX}px`; menu.style.top = `${e.pageY}px`; menu.style.display = 'block'; }); // dragstart filesList.addEventListener('dragstart', (e) => { const card = e.target.closest('.file-item'); if (!card) { e.preventDefault(); return; } const info = itemInfo(card); if (!info) { e.preventDefault(); return; } e.dataTransfer.setData('text/plain', info.id); if (info.type === 'folder') { e.dataTransfer.setData('application/oxicloud-folder', 'true'); } // allow copy or move (handled by the browser) e.dataTransfer.effectAllowed = 'copyMove'; this.draggedItems = document.createElement('div'); this.draggedItems.className = 'dragged-items'; let selectedCardFromList = filesList.querySelectorAll(`div.selected > div.name-cell`); if (selectedCardFromList.length === 0) { // fallback to current element selectedCardFromList = card.querySelectorAll('div.name-cell'); } let index = 0; const maxElements = 4; let lastItemDiv = null; while (index < selectedCardFromList.length && index < maxElements) { const iconCell = document.createElement('div'); const icon = selectedCardFromList[index].getElementsByClassName('file-icon').item(0)?.cloneNode(true); if (icon) { iconCell.appendChild(icon); iconCell.querySelectorAll('img')?.forEach((img) => { img.loading = 'eager'; }); } const nameCell = document.createElement('div'); const name = selectedCardFromList[index].getElementsByTagName('span').item(0)?.cloneNode(true); if (name) { nameCell.appendChild(name); } const div = document.createElement('div'); div.className = 'file-item'; div.appendChild(iconCell); div.appendChild(nameCell); this.draggedItems.appendChild(div); index += 1; lastItemDiv = div; } // if more than 1 item, display the badge if (selectedCardFromList.length > 1) { const badge = document.createElement('span'); badge.className = 'dragged-items-badge'; badge.innerText = `${selectedCardFromList.length}`; this.draggedItems.appendChild(badge); } // if more than maxElements display the fading if (selectedCardFromList.length > maxElements) { lastItemDiv.classList.add('fading'); } this.dragPreview.appendChild(this.draggedItems); e.dataTransfer.setDragImage(this.draggedItems, 0, 0); }); // dragend filesList.addEventListener('dragend', (_e) => { this.dragPreview.removeChild(this.draggedItems); document.querySelectorAll('.drop-target').forEach((el) => { el.classList.remove('drop-target'); }); }); // dragover – only folders are valid drop targets filesList.addEventListener('dragover', (e) => { const card = e.target.closest('.file-item'); if (!card || card.dataset.fileId) return; if (!card.dataset.folderId) return; e.preventDefault(); card.classList.add('drop-target'); }); // dragleave filesList.addEventListener('dragleave', (e) => { const card = e.target.closest('.file-item'); if (!card || card.dataset.fileId) return; card.classList.remove('drop-target'); }); // drop – only folders accept drops filesList.addEventListener('drop', async (e) => { const card = e.target.closest('.file-item'); if (!card || card.dataset.fileId) return; const targetFolderId = card.dataset.folderId; if (!targetFolderId) return; e.preventDefault(); card.classList.remove('drop-target'); const action = e.dataTransfer.dropEffect; await this._dropToFolder(action, targetFolderId, e.dataTransfer); }); }, /* ================================================================ * Favorite star helper – attaches a direct click handler to a * star `; if (window.app.currentPath !== '') { el.setAttribute('draggable', 'true'); } this._bindStarClick(el); return el; }, /** Create a grid card for a file */ _createFileItem(file) { const iconClass = file.icon_class || this.getIconClass(file.name); const iconSpecialClass = file.icon_special_class || this.getIconSpecialClass(file.name); const cat = file.category || ''; const typeLabel = cat ? window.i18n ? window.i18n.t(`files.file_types.${cat.toLowerCase()}`) || cat : cat : window.i18n ? window.i18n.t('files.file_types.document') : 'Document'; const fileSize = file.size_formatted || window.formatFileSize(file.size); const formattedDate = window.formatDateTime(file.modified_at); const isFav = window.favorites?.isFavorite(file.id, 'file'); const el = document.createElement('div'); el.className = 'file-item'; el.dataset.fileId = file.id; el.dataset.fileName = file.name; el.dataset.folderId = file.folder_id || ''; el.setAttribute('draggable', 'true'); el.innerHTML = `
${iconSpecialClass === 'image-icon' ? `` : ''}
${escapeHtml(file.name)} ${isFav ? '' : ''}
${typeLabel}
${fileSize}
${formattedDate}
`; var thumb = el.querySelector('.file-thumb'); if (thumb) thumb.addEventListener('error', function () { this.style.display = 'none'; }); this._bindStarClick(el); return el; }, /* ================================================================ * Batch rendering with DocumentFragment * ================================================================ */ resetFilesList() { const filesList = document.getElementById('files-list'); const filesContainerError = document.getElementById('files-container-error'); if (!filesList) return; filesList.innerHTML = `
Name
Type
Size
Modified
`; if (window.i18n?.translateElement) window.i18n.translateElement(filesList); filesList.classList.remove('hidden'); filesContainerError?.classList.add('hidden'); }, showEmptyList() { this.showError(`

`); }, /** * * @param {string} content * */ showError(content) { const filesContainerError = document.getElementById('files-container-error'); const filesList = document.getElementById('files-list'); if (filesContainerError) filesContainerError.innerHTML = content; if (window.i18n?.translateElement) window.i18n.translateElement(filesContainerError); filesContainerError?.classList.remove('hidden'); filesList?.classList.add('hidden'); }, /** * Render an array of folders into both grid and list views * using DocumentFragment for minimal reflows. */ renderFolders(folders) { if (!this._delegationReady) this.initDelegation(); const safeFolders = Array.isArray(folders) ? folders : []; this._lastFolders = safeFolders.slice(); for (const folder of safeFolders) { this._items.set(folder.id, folder); } this._renderFoldersToView(safeFolders); }, /** * Render an array of files into both grid and list views * using DocumentFragment for minimal reflows. */ renderFiles(files) { if (!this._delegationReady) this.initDelegation(); const safeFiles = Array.isArray(files) ? files : []; this._lastFiles = safeFiles.slice(); for (const file of safeFiles) { this._items.set(file.id, file); } this._renderFilesToView(safeFiles); }, /* ================================================================ * Single-item add (backward-compatible API for post-upload, etc.) * ================================================================ */ /** * Add a single folder to the active view. * @param {Object} folder - Folder object */ addFolderToView(folder) { if (!this._delegationReady) this.initDelegation(); // Duplicate guard if (document.querySelector(`.file-item[data-folder-id="${folder.id}"]`)) { console.log(`Folder ${folder.name} (${folder.id}) already exists in the view, not duplicating`); return; } this._items.set(folder.id, folder); this._upsertById(this._lastFolders, folder); this._renderFoldersToView([folder]); }, /** * Add a single file to the active view. * @param {Object} file - File object */ addFileToView(file) { if (!this._delegationReady) this.initDelegation(); // Duplicate guard if (document.querySelector(`.file-item[data-file-id="${file.id}"]`)) { console.log(`File ${file.name} (${file.id}) already exists in the view, not duplicating`); return; } this._items.set(file.id, file); this._upsertById(this._lastFiles, file); this._renderFilesToView([file]); } }; // --- Global helper functions for card interactions --- /** * Toggle selection state of a file/folder card. * Routes through the multiSelect module so batch actions know about selected items. */ function toggleCardSelection(card, event) { if (window.multiSelect) { window.multiSelect.handleToggleItem(card, event); } else { card.classList.toggle('selected'); } } /** * Show the context menu anchored next to a trigger element (the 3-dot button). */ function showContextMenuAtElement(triggerElement, menuId) { // Hide any open menus first document.querySelectorAll('.context-menu').forEach((m) => { m.style.display = 'none'; }); const menu = document.getElementById(menuId); if (!menu) return; const rect = triggerElement.getBoundingClientRect(); const menuWidth = 200; // approximate // Position below the trigger, aligned to the right edge let left = rect.right - menuWidth + window.scrollX; let top = rect.bottom + 4 + window.scrollY; // Keep inside viewport if (left < 8) left = 8; if (top + 300 > window.innerHeight + window.scrollY) { top = rect.top - 4 + window.scrollY; // flip above if no room } if (window.contextMenus && typeof window.contextMenus.syncFavoriteOptionLabels === 'function') { window.contextMenus.syncFavoriteOptionLabels(); } if (window.contextMenus && typeof window.contextMenus.syncWopiOptionVisibility === 'function') { window.contextMenus.syncWopiOptionVisibility().catch(() => {}); } menu.style.left = `${left}px`; menu.style.top = `${top}px`; menu.style.display = 'block'; } /** * Rubber band (lasso) selection — click + drag on empty grid area * to draw a rectangle and select all cards it touches. */ function initRubberBandSelection() { // Create the visual rectangle element once let selRect = document.getElementById('selection-rect'); if (!selRect) { selRect = document.createElement('div'); selRect.id = 'selection-rect'; selRect.className = 'selection-rect'; document.body.appendChild(selRect); } let active = false; let startX = 0, startY = 0; // We listen on the whole files-container (covers grid + empty space) const container = document.querySelector('.files-container') || document.getElementById('files-list'); if (!container) return; container.addEventListener('mousedown', (e) => { // Only start if clicking empty area (not on a card, button, menu, input…) if (e.button !== 0) return; // left click only if ( e.target.closest('.file-item') || e.target.closest('.context-menu') || e.target.closest('.upload-dropdown') || e.target.closest('button') || e.target.closest('input') || e.target.closest('.breadcrumb') || e.target.closest('.list-header') ) return; active = true; startX = e.clientX; startY = e.clientY; selRect.style.left = `${startX}px`; selRect.style.top = `${startY}px`; selRect.style.width = '0px'; selRect.style.height = '0px'; selRect.style.display = 'none'; // show only after a small movement e.preventDefault(); // prevent text selection }); document.addEventListener('mousemove', (e) => { if (!active) return; const curX = e.clientX; const curY = e.clientY; const left = Math.min(startX, curX); const top = Math.min(startY, curY); const width = Math.abs(curX - startX); const height = Math.abs(curY - startY); // Only show the rect after a small threshold to avoid flicker on click if (width > 5 || height > 5) { selRect.style.display = 'block'; } selRect.style.left = `${left}px`; selRect.style.top = `${top}px`; selRect.style.width = `${width}px`; selRect.style.height = `${height}px`; // Highlight cards that intersect with the rectangle const rectBounds = { left, top, right: left + width, bottom: top + height }; document.querySelectorAll('#files-list .file-item').forEach((card) => { const cardRect = card.getBoundingClientRect(); const intersects = cardRect.left < rectBounds.right && cardRect.right > rectBounds.left && cardRect.top < rectBounds.bottom && cardRect.bottom > rectBounds.top; if (intersects) { card.classList.add('selected'); // Sync with multiSelect module if (window.multiSelect) { const info = window.multiSelect._extractInfo(card); if (info) window.multiSelect.select(info.id, info.name, info.type, info.parentId); } } else { card.classList.remove('selected'); // Deselect from multiSelect module if (window.multiSelect) { const info = window.multiSelect._extractInfo(card); if (info) window.multiSelect.deselect(info.id); } } }); }); document.addEventListener('mouseup', () => { if (!active) return; active = false; const hadSelection = selRect.style.display === 'block'; selRect.style.display = 'none'; // Update the batch bar after rubber band selection completes if (window.multiSelect) window.multiSelect._syncUI(); // Suppress the click event that follows mouseup so the global // deselect handler doesn't immediately clear the selection. if (hadSelection) { window.__rubberBandJustFinished = true; requestAnimationFrame(() => { window.__rubberBandJustFinished = false; }); } }); } // Initialize rubber band once DOM is ready if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initRubberBandSelection); } else { initRubberBandSelection(); } // Expose helpers globally window.toggleCardSelection = toggleCardSelection; window.showContextMenuAtElement = showContextMenuAtElement; window.initRubberBandSelection = initRubberBandSelection; /** * Show a modern confirm dialog (replaces native confirm()) * @param {Object} options * @param {string} options.title - Dialog title * @param {string} options.message - Dialog message/body * @param {string} [options.confirmText='Confirmar'] - Text for confirm button * @param {string} [options.cancelText='Cancelar'] - Text for cancel button * @param {boolean} [options.danger=false] - Use danger styling (red) * @returns {Promise} true if confirmed, false if cancelled */ function showConfirmDialog({ title, message, confirmText, cancelText, danger = true } = {}) { const ct = confirmText || (window.i18n ? window.i18n.t('actions.delete') : 'Delete'); const cc = cancelText || (window.i18n ? window.i18n.t('actions.cancel') : 'Cancel'); const t = title || (window.i18n ? window.i18n.t('dialogs.confirm_title') : 'Confirm action'); return new Promise((resolve) => { // Remove any previous confirm dialog const prev = document.getElementById('confirm-dialog-overlay'); if (prev) prev.remove(); const overlay = document.createElement('div'); overlay.id = 'confirm-dialog-overlay'; overlay.className = 'confirm-dialog'; overlay.innerHTML = `
${t}
${message || ''}
`; document.body.appendChild(overlay); // Force layout then show requestAnimationFrame(() => { overlay.classList.add('active'); }); const cleanup = (result) => { overlay.classList.remove('active'); setTimeout(() => overlay.remove(), 200); resolve(result); }; overlay.querySelector('.confirm-dialog-cancel').addEventListener('click', () => cleanup(false)); overlay.querySelector('.confirm-dialog-ok').addEventListener('click', () => cleanup(true)); overlay.addEventListener('click', (e) => { if (e.target === overlay) cleanup(false); }); }); } window.showConfirmDialog = showConfirmDialog; // Expose UI module globally window.ui = ui;