class HeaderTools extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.render(); this.addEventListeners(); this.startEngineStatusCheck(); } // 添加getter方法 get selectedPlane() { return this.shadowRoot.getElementById('planeSelect').value; } get selectedConfiguration() { return this.shadowRoot.getElementById('configurationSelect').value; } get selectedDomain() { const select = this.shadowRoot.getElementById('configurationSelect'); const selectedOption = select.options[select.selectedIndex]; return selectedOption ? selectedOption.dataset.domainId : ''; } get selectedConfigurationName() { const select = this.shadowRoot.getElementById('configurationSelect'); const selectedOption = select.options[select.selectedIndex]; return selectedOption ? selectedOption.textContent : ''; } // 启动引擎状态检查定时器 startEngineStatusCheck() { this.checkEngineStatus(); this.engineStatusInterval = setInterval(() => { this.checkEngineStatus(); }, 1000); } // 检查引擎运行状态 async checkEngineStatus() { try { const response = await fetch('/api/engine-status'); if (response.ok) { const data = await response.json(); this.updateEngineStatus(data.running); } } catch (error) { console.error('检查引擎状态失败:', error); this.updateEngineStatus(false); } } // 更新引擎状态显示 updateEngineStatus(isRunning) { const statusLight = this.shadowRoot.getElementById('engineStatusLight'); const planeSelect = this.shadowRoot.getElementById('planeSelect'); const configurationSelect = this.shadowRoot.getElementById('configurationSelect'); if (statusLight) { statusLight.className = `status-light ${isRunning ? 'running' : 'stopped'}`; statusLight.title = isRunning ? '引擎正在运行' : '引擎已停止'; } // 根据引擎状态控制选择器的可用性 if (planeSelect) { planeSelect.disabled = isRunning; } if (configurationSelect) { configurationSelect.disabled = isRunning; } } // 保存选择到localStorage saveSelection() { const selection = { plane: this.selectedPlane, configurationId: this.selectedConfiguration, configurationName: this.selectedConfigurationName, domainId: this.selectedDomain }; localStorage.setItem('xnsim-selection', JSON.stringify(selection)); } // 从localStorage恢复选择 restoreSelection() { const savedSelection = localStorage.getItem('xnsim-selection'); if (savedSelection) { const selection = JSON.parse(savedSelection); const planeSelect = this.shadowRoot.getElementById('planeSelect'); const configurationSelect = this.shadowRoot.getElementById('configurationSelect'); // 先加载机型列表 this.loadPlanes().then(() => { if (selection.plane) { planeSelect.value = selection.plane; // 加载该机型下的构型列表 this.loadConfigurations(selection.plane).then(() => { if (selection.configurationId) { configurationSelect.value = selection.configurationId; // 触发change事件以确保domainId被更新 configurationSelect.dispatchEvent(new Event('change')); } }); } }); } } render() { this.shadowRoot.innerHTML = `