class VariableForm extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.mode = 'add'; this.interfaceData = null; this.products = []; this.ataChapters = []; this.modelStructs = []; } connectedCallback() { this.render(); this.addEventListeners(); } // 接收父组件传递的选项数据 setOptions(products, atas) { this.products = products; this.ataChapters = atas; this.updateProductOptions(); this.updateAtaOptions(); } updateProductOptions() { const select = this.shadowRoot.querySelector('#productName'); if (select) { select.innerHTML = ` ${this.products.map(product => ` `).join('')} `; } } updateAtaOptions() { const select = this.shadowRoot.querySelector('#ataName'); if (select) { select.innerHTML = ` ${this.ataChapters.map(chapter => ` `).join('')} `; } } async loadModelStructs(productName, ataName) { try { const response = await fetch(`/api/interface/struct/list?systemName=XNSim&productName=${productName}&ataName=${ataName}`); if (!response.ok) { throw new Error('获取模型结构体列表失败'); } const data = await response.json(); this.modelStructs = data.map(item => item.ModelStructName); this.updateModelStructSelect(); } catch (error) { console.error('加载模型结构体列表失败:', error); alert('加载模型结构体列表失败'); } } updateModelStructSelect() { const select = this.shadowRoot.querySelector('#modelStructName'); if (select) { select.innerHTML = ` ${this.modelStructs.map(struct => ` `).join('')} `; } } setMode(mode, data = null) { this.mode = mode; this.interfaceData = data; if (data) { // 保存原始数据 this.originalData = { ProductName: data.ProductName, ATAName: data.ATAName, ModelStructName: data.ModelStructName, InterfaceName: data.InterfaceName }; // 如果是在编辑模式下,需要先加载模型结构体列表 this.loadModelStructs(data.ProductName, data.ATAName).then(() => { this.render(); this.addEventListeners(); this.show(); }); } else { this.render(); this.addEventListeners(); this.show(); } } show() { const overlay = this.shadowRoot.querySelector('.form-overlay'); if (overlay) { overlay.style.display = 'block'; } } hide() { const overlay = this.shadowRoot.querySelector('.form-overlay'); if (overlay) { overlay.style.display = 'none'; } } addEventListeners() { const form = this.shadowRoot.querySelector('form'); const cancelBtn = this.shadowRoot.querySelector('.cancel-btn'); const productSelect = this.shadowRoot.querySelector('#productName'); const ataSelect = this.shadowRoot.querySelector('#ataName'); const interfaceNameInput = this.shadowRoot.querySelector('#interfaceName'); const isArrayCheckbox = this.shadowRoot.querySelector('#interfaceIsArray'); const arraySize1Input = this.shadowRoot.querySelector('#interfaceArraySize_1'); const arraySize2Input = this.shadowRoot.querySelector('#interfaceArraySize_2'); // 监听构型和ATA章节的变化 productSelect.addEventListener('change', () => { const productName = productSelect.value; const ataName = ataSelect.value; if (productName && ataName) { this.loadModelStructs(productName, ataName); } }); ataSelect.addEventListener('change', () => { const productName = productSelect.value; const ataName = ataSelect.value; if (productName && ataName) { this.loadModelStructs(productName, ataName); } }); // 验证C++命名规范 interfaceNameInput.addEventListener('input', (e) => { const value = e.target.value; // C++命名规范:以字母或下划线开头,只能包含字母、数字和下划线 const isValid = /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value); if (!isValid) { e.target.setCustomValidity('接口名称必须以字母或下划线开头,只能包含字母、数字和下划线'); } else { e.target.setCustomValidity(''); } }); // 验证数组大小 arraySize1Input.addEventListener('input', (e) => { const value = parseInt(e.target.value); if (value <= 1) { e.target.setCustomValidity('第一维大小必须大于1'); } else { e.target.setCustomValidity(''); } }); arraySize2Input.addEventListener('input', (e) => { const value = e.target.value; if (value && parseInt(value) <= 1 && parseInt(value) !== 0) { e.target.setCustomValidity('第二维大小必须大于1'); } else { e.target.setCustomValidity(''); } }); form.addEventListener('submit', (e) => { e.preventDefault(); const formData = new FormData(form); const isArray = formData.get('interfaceIsArray') ? 1 : 0; const arraySize1 = parseInt(formData.get('interfaceArraySize_1')) || 0; const arraySize2 = parseInt(formData.get('interfaceArraySize_2')) || 0; const data = { SystemName: 'XNSim', ProductName: formData.get('productName'), ATAName: formData.get('ataName'), ModelStructName: formData.get('modelStructName'), InterfaceName: formData.get('interfaceName'), InterfaceType: formData.get('interfaceType'), InterfaceOption: 1, InterfaceIsArray: isArray, InterfaceArraySize_1: isArray && arraySize1 > 1 ? arraySize1 : 0, InterfaceArraySize_2: isArray && arraySize1 > 1 ? arraySize2 : 0, InterfaceNotes: formData.get('interfaceNotes') }; // 如果是编辑模式,添加原始数据 if (this.mode === 'edit' && this.originalData) { data.originalProductName = this.originalData.ProductName; data.originalATAName = this.originalData.ATAName; data.originalModelStructName = this.originalData.ModelStructName; data.originalInterfaceName = this.originalData.InterfaceName; } // 验证必填字段 const requiredFields = [ 'SystemName', 'ProductName', 'ATAName', 'ModelStructName', 'InterfaceName', 'InterfaceType' ]; const missingFields = requiredFields.filter(field => !data[field]); if (missingFields.length > 0) { alert(`请填写以下必填字段:${missingFields.join(', ')}`); return; } // 如果是数组类型,验证数组大小 if (data.InterfaceIsArray === 1) { if (!data.InterfaceArraySize_1 || data.InterfaceArraySize_1 <= 1) { alert('第一维大小必须大于1'); return; } if (data.InterfaceArraySize_2 && data.InterfaceArraySize_2 <= 1 && data.InterfaceArraySize_2 !== 0) { alert('第二维大小必须大于1或等于0'); return; } } this.dispatchEvent(new CustomEvent('save-success', { detail: { ...data, mode: this.mode } })); this.hide(); }); cancelBtn.addEventListener('click', () => { this.hide(); }); // 监听数组大小输入框的变化 isArrayCheckbox.addEventListener('change', () => { arraySize1Input.disabled = !isArrayCheckbox.checked; arraySize2Input.disabled = !isArrayCheckbox.checked; if (!isArrayCheckbox.checked) { arraySize1Input.value = ''; arraySize2Input.value = ''; } }); } render() { const interfaceTypes = [ 'char', 'octet', 'short', 'unsigned short', 'long', 'unsigned long', 'long long', 'unsigned long long', 'float', 'double', 'long double', 'boolean' ]; this.shadowRoot.innerHTML = `
`; } } customElements.define('variable-form', VariableForm);