(directory = null)
| 313 | } |
| 314 | |
| 315 | async function refreshFileList(directory = null) { |
| 316 | try { |
| 317 | const targetDirectory = directory || elements.directoryInput.value.trim(); |
| 318 | if (!targetDirectory) { |
| 319 | elements.fileSelect.innerHTML = '<option value="">请先输入目录路径...</option>'; |
| 320 | currentFileList = []; |
| 321 | currentFileIndex = -1; |
| 322 | updateNavigationButtons(); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | showLoading(); |
| 327 | |
| 328 | const url = `/api/list_files?directory=${encodeURIComponent(targetDirectory)}`; |
| 329 | const data = await apiCall(url); |
| 330 | |
| 331 | elements.fileSelect.innerHTML = '<option value="">选择Trace文件...</option>'; |
| 332 | |
| 333 | if (data.files.length === 0) { |
| 334 | elements.fileSelect.innerHTML = '<option value="">该目录下没有JSON文件</option>'; |
| 335 | currentFileList = []; |
| 336 | currentFileIndex = -1; |
| 337 | showSuccess(`目录 "${targetDirectory}" 下没有找到JSON文件`); |
| 338 | updateNavigationButtons(); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // 保存文件列表到全局变量 |
| 343 | currentFileList = data.files; |
| 344 | currentFileIndex = -1; |
| 345 | |
| 346 | data.files.forEach((file, index) => { |
| 347 | const option = document.createElement('option'); |
| 348 | option.value = file.path; |
| 349 | option.dataset.index = index; |
| 350 | const fileSize = formatFileSize(file.size); |
| 351 | const modifiedDate = new Date(file.modified * 1000).toLocaleString('zh-CN'); |
| 352 | option.textContent = `${file.name} (${fileSize}, ${modifiedDate})`; |
| 353 | elements.fileSelect.appendChild(option); |
| 354 | }); |
| 355 | |
| 356 | showSuccess(`在目录 "${targetDirectory}" 中找到 ${data.files.length} 个JSON文件`); |
| 357 | updateNavigationButtons(); |
| 358 | |
| 359 | } catch (error) { |
| 360 | showError('获取文件列表失败: ' + error.message); |
| 361 | elements.fileSelect.innerHTML = '<option value="">获取文件列表失败</option>'; |
| 362 | currentFileList = []; |
| 363 | currentFileIndex = -1; |
| 364 | updateNavigationButtons(); |
| 365 | } finally { |
| 366 | hideLoading(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // 文件切换功能 |
| 371 | function onFileSelect() { |
no test coverage detected