class HeaderTools extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
this.addEventListeners();
}
// 添加getter方法
get selectedPlane() {
return this.shadowRoot.getElementById('planeSelect').value;
}
get selectedProduct() {
return this.shadowRoot.getElementById('productSelect').value;
}
// 保存选择到localStorage
saveSelection() {
const selection = {
plane: this.selectedPlane,
product: this.selectedProduct
};
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 productSelect = this.shadowRoot.getElementById('productSelect');
// 先加载机型列表
this.loadPlanes().then(() => {
if (selection.plane) {
planeSelect.value = selection.plane;
// 加载该机型下的构型列表
this.loadProducts(selection.plane).then(() => {
if (selection.product) {
productSelect.value = selection.product;
}
});
}
});
}
}
render() {
this.shadowRoot.innerHTML = `
机型:
构型:
`;
}
addEventListeners() {
// 加载机型列表
this.loadPlanes();
// 加载构型列表
this.loadProducts();
// 恢复上次的选择
this.restoreSelection();
// 字体大小调整
this.shadowRoot.getElementById('fontSizeBtn').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('font-size-click'));
});
// 主题调整
this.shadowRoot.getElementById('themeBtn').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('theme-click'));
});
// 通知
this.shadowRoot.getElementById('notificationBtn').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('notification-click'));
});
// 在线用户
this.shadowRoot.getElementById('onlineUsersBtn').addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('online-users-click'));
});
// 机型选择
this.shadowRoot.getElementById('planeSelect').addEventListener('change', (e) => {
this.dispatchEvent(new CustomEvent('plane-change', {
detail: { plane: e.target.value }
}));
// 当机型改变时,重新加载该机型下的构型列表
this.loadProducts(e.target.value);
// 保存选择
this.saveSelection();
});
// 构型选择
this.shadowRoot.getElementById('productSelect').addEventListener('change', (e) => {
this.dispatchEvent(new CustomEvent('product-change', {
detail: { product: e.target.value }
}));
// 保存选择
this.saveSelection();
});
}
async loadPlanes() {
try {
const response = await fetch('/api/planes');
if (!response.ok) {
throw new Error('获取机型列表失败');
}
const planes = await response.json();
const select = this.shadowRoot.getElementById('planeSelect');
// 清空现有选项
select.innerHTML = '';
planes.forEach(plane => {
const option = document.createElement('option');
option.value = plane.PlaneName;
option.textContent = plane.PlaneName;
select.appendChild(option);
});
} catch (error) {
console.error('加载机型列表失败:', error);
}
}
async loadProducts(planeName = '') {
try {
const url = planeName ? `/api/configurations?plane=${planeName}` : '/api/configurations';
const response = await fetch(url);
if (!response.ok) {
throw new Error('获取构型列表失败');
}
const products = await response.json();
const select = this.shadowRoot.getElementById('productSelect');
// 清空现有选项
select.innerHTML = '';
products.forEach(product => {
const option = document.createElement('option');
option.value = product.ConfName;
option.textContent = product.ConfName;
select.appendChild(option);
});
} catch (error) {
console.error('加载构型列表失败:', error);
}
}
}
customElements.define('header-tools', HeaderTools);