class ServiceDevelopment extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.services = []; this.currentServices = []; this.currentServiceVersions = []; this.currentVersion = null; this.currentView = 'services'; // 'services', 'versions', 'versionForm' this.currentService = null; this.isEditMode = false; // 初始化DOM结构 this.shadowRoot.innerHTML = `
加载数据失败: ${message}
`; // 添加到容器 container.appendChild(errorContainer); const retryBtn = errorContainer.querySelector('.retry-btn'); if (retryBtn) { retryBtn.addEventListener('click', () => { if (this.currentView === 'services') { this.init(); } else if (this.currentView === 'versions' && this.currentService) { this.fetchServiceVersions(this.currentService.className, this.currentService.name); } else { this.init(); } }); } } // 组件被重新激活时调用 reactivate() { } showVersionEditor(versionData) { this.currentView = 'versionEditor'; // 确保 CmdList 是有效的数组 if (versionData.CmdList) { try { if (typeof versionData.CmdList === 'string') { // 如果已经是字符串,尝试解析它 const parsed = JSON.parse(versionData.CmdList); versionData.CmdList = Array.isArray(parsed) ? parsed : []; } else if (!Array.isArray(versionData.CmdList)) { // 如果不是数组,初始化为空数组 versionData.CmdList = []; } } catch (e) { console.error('解析 CmdList 失败:', e); versionData.CmdList = []; } } else { versionData.CmdList = []; } this.currentVersion = versionData; this.isEditMode = versionData && versionData.Version ? true : false; const container = this.shadowRoot.querySelector('.service-dev-container'); if (!container) { console.error('找不到容器元素'); return; } // 更新标题 const title = this.shadowRoot.querySelector('.page-title'); if (title) { title.textContent = this.isEditMode ? `编辑版本: ${versionData.Name || this.currentService.name} (v${versionData.Version})` : `为 ${this.currentService.name} 创建新版本`; } container.innerHTML = ''; // 添加工具栏按钮 const toolbarDiv = document.createElement('div'); toolbarDiv.className = 'editor-toolbar'; toolbarDiv.style.cssText = 'display: flex; justify-content: space-between; margin-bottom: 20px; grid-column: 1 / -1; width: 100%;'; // 添加返回按钮 const backButton = document.createElement('div'); backButton.className = 'back-button'; backButton.innerHTML = `← 返回版本列表`; backButton.style.cssText = 'margin: 0; cursor: pointer; color: #667eea; font-weight: 500;'; backButton.addEventListener('click', () => { this.fetchServiceVersions(this.currentService.className, this.currentService.name); }); // 添加按钮组 const actionsDiv = document.createElement('div'); actionsDiv.className = 'toolbar-actions'; actionsDiv.style.cssText = 'display: flex; gap: 10px;'; // 创建各种按钮 const buttonConfigs = [ { text: '刷新', color: '#38a169', action: async () => { try { // 保存当前版本ID用于刷新后重新定位 const versionId = this.currentVersion.Version; // 先刷新版本列表 await this.fetchServiceVersions(this.currentService.className, this.currentService.name); // 然后找到当前编辑的版本并重新打开 if (versionId) { const refreshedVersion = this.currentServiceVersions.find(v => v.Version === versionId); if (refreshedVersion) { this.showVersionEditor(refreshedVersion); } else { alert('未能找到当前版本,可能已被删除'); // 如果找不到当前版本,回到版本列表 this.renderServiceVersions(); } } } catch (error) { console.error('刷新版本数据失败:', error); alert(`刷新失败: ${error.message}`); } } }, { text: '保存', color: '#3182ce', action: () => { try { // 获取表单元素 const form = this.shadowRoot.querySelector('#versionEditorForm'); if (form) { // 触发提交事件 form.dispatchEvent(new Event('submit')); } else { console.error('找不到表单元素'); alert('保存失败: 找不到表单元素'); } } catch (error) { console.error('保存时发生错误:', error); alert(`保存失败: ${error.message}`); } } }, { text: '生成代码', color: '#805ad5', action: () => alert('生成代码功能即将上线') }, { text: '编辑代码', color: '#d69e2e', action: () => alert('编辑代码功能即将上线') }, { text: '服务编译', color: '#dd6b20', action: () => alert('服务编译功能即将上线') }, { text: '服务提交', color: '#e53e3e', action: () => alert('服务提交功能即将上线') } ]; buttonConfigs.forEach(config => { const button = document.createElement('button'); button.className = 'toolbar-button'; button.textContent = config.text; button.style.cssText = `background-color: ${config.color}; color: white; border: none; border-radius: 4px; padding: 8px 16px; cursor: pointer; font-weight: 500;`; button.addEventListener('click', config.action); actionsDiv.appendChild(button); }); toolbarDiv.appendChild(backButton); toolbarDiv.appendChild(actionsDiv); container.appendChild(toolbarDiv); // 创建表单容器 - 使用网格布局 const formContainer = document.createElement('div'); formContainer.className = 'form-container'; formContainer.style.cssText = 'display: grid; grid-template-columns: 1fr 1fr; gap: 20px; grid-column: 1 / -1; width: 100%;'; // 创建表单 const form = document.createElement('form'); form.className = 'version-form'; form.id = 'versionEditorForm'; form.style.cssText = 'background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); padding: 20px; grid-column: 1 / -1;'; // 创建两列布局的基本信息部分 const basicInfoSection = document.createElement('div'); basicInfoSection.className = 'form-section basic-info'; basicInfoSection.style.cssText = 'display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px;'; // 左列 - 基本信息 const leftColumn = document.createElement('div'); leftColumn.className = 'form-column'; leftColumn.innerHTML = `名称 | 描述 | 调用函数 | 操作 |
---|---|---|---|
暂无指令参数 |