class ImportDialog extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.data = [];
this.planes = [];
this.atas = [];
}
connectedCallback() {
this.render();
this.addEventListeners();
}
// 接收父组件传递的选项数据
setOptions(planes, atas) {
this.planes = planes;
this.atas = atas;
this.updateAtaOptions();
}
updateAtaOptions() {
const select = this.shadowRoot.querySelector('#ata-select');
if (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 ataSelect = this.shadowRoot.querySelector('#ata-select');
const modelInput = this.shadowRoot.querySelector('#model-input');
// 从 localStorage 获取机型信息
const savedSelection = localStorage.getItem('xnsim-selection');
if (!savedSelection) {
alert('请先选择机型');
return;
}
const selection = JSON.parse(savedSelection);
if (!selection.configurationId) {
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
PlaneName: selection.plane,
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
ConfID: selection.configurationId
}));
// 验证数据
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() {
// 从 localStorage 获取机型信息
const savedSelection = localStorage.getItem('xnsim-selection');
const selection = savedSelection ? JSON.parse(savedSelection) : null;
const planeName = selection?.plane || '';
this.shadowRoot.innerHTML = `
当前机型:${planeName}
变量名称 |
变量类型 |
结构体 |
是否数组 |
第一维大小 |
第二维大小 |
描述 |
`;
}
}
customElements.define('import-dialog', ImportDialog);