import { openTab as _openTab } from './ui/tabs.js'; import { initTrainForm } from './train/form.js'; import { startProcessPolling, stopProcessPolling, loadProcesses } from './processes/list.js'; import { loadLogFiles } from './logfiles/list.js'; const hooks = { onEnterProcesses: () => { loadProcesses(); }, onLeaveProcesses: () => { stopProcessPolling(); }, onEnterLogfiles: () => { loadLogFiles(); }, }; window.openTab = (evt, tabName) => _openTab(evt, tabName, hooks); // 文件夹选择器功能 - 直接显示服务器端文件浏览器 window.selectFolder = (inputId) => { // 直接使用远程文件浏览器,不尝试本地文件系统访问 openRemoteFileBrowser(inputId); }; // 远程文件浏览器 - 支持文件和文件夹选择 let currentFileBrowserTarget = null; let currentBrowsePath = './'; let selectedFilePath = null; let currentSelectionMode = 'auto'; // 'file', 'folder', or 'auto' function openRemoteFileBrowser(inputId) { console.log('openRemoteFileBrowser called with:', inputId); currentFileBrowserTarget = inputId; // 根据输入框ID确定选择模式 if (inputId === 'data_path') { currentSelectionMode = 'file'; // 数据路径需要文件选择 console.log('Mode set to: FILE selection'); } else if (inputId === 'save_dir' || inputId.includes('reward_model_path')) { currentSelectionMode = 'folder'; // 保存目录和奖励模型路径需要文件夹选择 console.log('Mode set to: FOLDER selection'); } else { currentSelectionMode = 'auto'; // 自动模式 console.log('Mode set to: AUTO selection'); } const modal = document.getElementById('file-browser-modal'); if (modal) { modal.classList.remove('hidden'); console.log('Modal opened successfully'); } else { console.error('Modal element not found!'); return; } // 重置选择状态 selectedFilePath = null; const selectedPathInput = document.getElementById('selected-path'); if (selectedPathInput) { selectedPathInput.value = ''; console.log('Selected path input cleared'); } // 加载初始路径 loadQuickPaths(); browsePath('./'); } function closeFileBrowser() { document.getElementById('file-browser-modal').classList.add('hidden'); currentFileBrowserTarget = null; currentBrowsePath = './'; selectedFilePath = null; currentSelectionMode = 'auto'; } function confirmFileSelection() { console.log('confirmFileSelection called'); console.log('selectedFilePath:', selectedFilePath); console.log('currentFileBrowserTarget:', currentFileBrowserTarget); if (selectedFilePath && currentFileBrowserTarget) { const targetElement = document.getElementById(currentFileBrowserTarget); console.log('targetElement:', targetElement); if (targetElement) { targetElement.value = selectedFilePath; console.log('Value set successfully'); closeFileBrowser(); } else { console.error('Target element not found:', currentFileBrowserTarget); alert('错误:无法找到目标输入框'); } } else { console.log('Missing selection or target'); alert('请先选择文件或文件夹'); } } function navigateToParent() { if (window.currentParentPath) { // 使用后端提供的父目录路径(绝对路径) browsePath(window.currentParentPath); } else if (currentBrowsePath && currentBrowsePath !== './') { // 回退到基于当前路径的计算 const parentPath = currentBrowsePath.includes('/') ? currentBrowsePath.substring(0, currentBrowsePath.lastIndexOf('/')) : './'; browsePath(parentPath || './'); } } function selectCurrentDirectory() { // 选择当前目录 selectedFilePath = currentBrowsePath; document.getElementById('selected-path').value = currentBrowsePath; // 可以关闭模态框或让用户继续浏览 } async function loadQuickPaths() { try { const response = await fetch('/api/quick-paths'); const data = await response.json(); const quickPathsContainer = document.getElementById('quick-paths'); quickPathsContainer.innerHTML = ''; if (data.paths && data.paths.length > 0) { data.paths.forEach(path => { const btn = document.createElement('button'); btn.className = 'quick-path-btn'; btn.textContent = path.name; btn.onclick = () => browsePath(path.path); btn.title = path.path; quickPathsContainer.appendChild(btn); }); } } catch (error) { console.warn('加载快捷路径失败:', error); } } async function browsePath(path) { console.log('browsePath called with:', path); try { currentBrowsePath = path; selectedFilePath = null; // 重置选中的文件路径 document.getElementById('selected-path').value = ''; // 清空显示 // 更新帮助文本 updateHelpText(); const response = await fetch(`/api/browse?path=${encodeURIComponent(path)}`); const data = await response.json(); if (data.error) { alert(`浏览失败: ${data.error}`); return; } renderFileList(data); console.log('File list rendered successfully'); } catch (error) { console.error('浏览路径失败:', error); alert('浏览路径失败,请检查网络连接'); } } function renderFileList(data) { const fileList = document.getElementById('file-list'); fileList.innerHTML = ''; if (!data.items || data.items.length === 0) { fileList.innerHTML = '