class ImportDialog extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }); this.data = []; this.products = []; this.atas = []; } connectedCallback() { this.render(); this.addEventListeners(); } // 接收父组件传递的选项数据 setOptions(products, atas) { this.products = products; this.atas = atas; this.updateProductOptions(); this.updateAtaOptions(); } updateProductOptions() { const select = this.shadowRoot.querySelector('#product-select'); select.innerHTML = ` ${this.products.map(product => ` `).join('')} `; } updateAtaOptions() { const select = this.shadowRoot.querySelector('#ata-select'); select.innerHTML = ` ${this.atas.map(ata => ` `).join('')} `; } show(data) { this.data = data; this.updateTable(); this.style.display = 'block'; } hide() { this.style.display = 'none'; } updateTable() { const tbody = this.shadowRoot.querySelector('tbody'); const lowercaseCheckbox = this.shadowRoot.querySelector('#lowercase-checkbox'); const shouldLowercase = lowercaseCheckbox.checked; tbody.innerHTML = this.data.map(item => ` ${shouldLowercase ? item.name.toLowerCase() : item.name} ${item.variableType || '-'} ${item.structName} ${item.isArray ? '是' : '否'} ${item.arraySize1 || '-'} ${item.arraySize2 || '-'} ${item.description} `).join(''); } addEventListeners() { const closeBtn = this.shadowRoot.querySelector('.close-btn'); const confirmBtn = this.shadowRoot.querySelector('.confirm-btn'); const cancelBtn = this.shadowRoot.querySelector('.cancel-btn'); const lowercaseCheckbox = this.shadowRoot.querySelector('#lowercase-checkbox'); closeBtn.addEventListener('click', () => this.hide()); cancelBtn.addEventListener('click', () => this.hide()); // 添加小写复选框的事件监听 lowercaseCheckbox.addEventListener('change', () => { this.updateTable(); }); confirmBtn.addEventListener('click', () => { const productSelect = this.shadowRoot.querySelector('#product-select'); const ataSelect = this.shadowRoot.querySelector('#ata-select'); const modelInput = this.shadowRoot.querySelector('#model-input'); if (!productSelect.value) { alert('请选择构型'); return; } if (!ataSelect.value) { alert('请选择ATA章节'); return; } if (!modelInput.value.trim()) { alert('请输入模型名称'); return; } const modelName = modelInput.value.trim(); // 处理数据,转换为数据库格式 const processedData = this.data.map(item => ({ SystemName: 'XNSim', // 系统名称固定为XNSim ProductName: productSelect.value, ATAName: ataSelect.value, ModelStructName: `${modelName}_${item.structName}`, InterfaceName: lowercaseCheckbox.checked ? item.name.toLowerCase() : item.name, InterfaceType: item.variableType || '', InterfaceOption: 1, InterfaceIsArray: item.isArray ? 1 : 0, // 确保是数字类型 InterfaceArraySize_1: item.arraySize1 || 0, // 确保是数字类型 InterfaceArraySize_2: item.arraySize2 || 0, // 确保是数字类型 InterfaceNotes: item.description || '' // 确保不是null })); // 验证数据 const invalidData = processedData.find(item => { return !item.InterfaceType || (item.InterfaceIsArray === 1 && (!item.InterfaceArraySize_1 || item.InterfaceArraySize_1 <= 1)); }); if (invalidData) { alert('数据格式不正确,请检查:\n1. 变量类型不能为空\n2. 如果是数组,第一维大小必须大于1'); return; } this.dispatchEvent(new CustomEvent('confirm-import', { detail: processedData, bubbles: true, composed: true })); this.hide(); }); } render() { this.shadowRoot.innerHTML = `
导入数据预览
变量名称 变量类型 结构体 是否数组 第一维大小 第二维大小 描述
`; } } customElements.define('import-dialog', ImportDialog);