167 lines
3.9 KiB
JavaScript
167 lines
3.9 KiB
JavaScript
import state from './state.js';
|
|
import {
|
|
getServers,
|
|
saveServers,
|
|
getCurrentServer,
|
|
setCurrentServer as persistCurrentServer,
|
|
clearAuth
|
|
} from './storage.js';
|
|
import { showToast } from './toast.js';
|
|
|
|
export default class ServerManager {
|
|
constructor() {
|
|
this.serverInput = document.getElementById('serverInput');
|
|
this.addBtn = document.getElementById('addServerBtn');
|
|
this.removeBtn = document.getElementById('removeServerBtn');
|
|
this.selectEl = document.getElementById('serverSelect');
|
|
this.servers = [];
|
|
this.changeHandler = null;
|
|
}
|
|
|
|
init() {
|
|
this.servers = getServers();
|
|
this.renderOptions();
|
|
|
|
const storedCurrent = getCurrentServer();
|
|
if (storedCurrent) {
|
|
this.setCurrentServer(storedCurrent);
|
|
state.currentServer = storedCurrent;
|
|
}
|
|
|
|
if (this.addBtn) {
|
|
this.addBtn.addEventListener('click', () => this.handleAdd());
|
|
}
|
|
|
|
if (this.removeBtn) {
|
|
this.removeBtn.addEventListener('click', () => this.handleRemove());
|
|
}
|
|
|
|
if (this.selectEl) {
|
|
this.selectEl.addEventListener('change', () => this.handleSelect());
|
|
}
|
|
}
|
|
|
|
onChange(handler) {
|
|
this.changeHandler = handler;
|
|
}
|
|
|
|
handleAdd() {
|
|
const server = (this.serverInput?.value || '').trim();
|
|
|
|
if (!server) {
|
|
showToast('请输入服务器地址', 'error');
|
|
return;
|
|
}
|
|
|
|
if (!this.isValidUrl(server)) {
|
|
showToast('无效的服务器地址格式', 'error');
|
|
return;
|
|
}
|
|
|
|
if (this.servers.includes(server)) {
|
|
showToast('服务器已存在', 'error');
|
|
return;
|
|
}
|
|
|
|
this.servers.push(server);
|
|
saveServers(this.servers);
|
|
this.renderOptions();
|
|
this.setCurrentServer(server);
|
|
persistCurrentServer(server);
|
|
showToast('服务器已添加', 'success');
|
|
|
|
if (this.changeHandler) {
|
|
this.changeHandler(server);
|
|
}
|
|
if (this.serverInput) {
|
|
this.serverInput.value = '';
|
|
}
|
|
}
|
|
|
|
handleRemove() {
|
|
const server = this.selectEl?.value;
|
|
if (!server) {
|
|
showToast('请选择要删除的服务器', 'error');
|
|
return;
|
|
}
|
|
|
|
if (!confirm(`确定要删除服务器 "${server}" 吗?`)) {
|
|
return;
|
|
}
|
|
|
|
this.servers = this.servers.filter(item => item !== server);
|
|
saveServers(this.servers);
|
|
clearAuth(server);
|
|
|
|
if (state.currentServer === server) {
|
|
state.currentServer = '';
|
|
}
|
|
|
|
persistCurrentServer('');
|
|
this.renderOptions();
|
|
showToast('服务器已删除', 'success');
|
|
|
|
if (this.changeHandler) {
|
|
this.changeHandler('');
|
|
}
|
|
}
|
|
|
|
handleSelect() {
|
|
const server = this.selectEl?.value || '';
|
|
persistCurrentServer(server);
|
|
state.currentServer = server;
|
|
|
|
if (this.changeHandler) {
|
|
this.changeHandler(server);
|
|
}
|
|
}
|
|
|
|
ensureServerInList(server) {
|
|
if (!server) return;
|
|
if (!this.servers.includes(server)) {
|
|
this.servers.push(server);
|
|
saveServers(this.servers);
|
|
this.renderOptions();
|
|
}
|
|
this.setCurrentServer(server);
|
|
}
|
|
|
|
setCurrentServer(server) {
|
|
if (!this.selectEl) return;
|
|
const optionExists = Array.from(this.selectEl.options).some(opt => opt.value === server);
|
|
if (!optionExists && server) {
|
|
const option = document.createElement('option');
|
|
option.value = server;
|
|
option.textContent = server;
|
|
this.selectEl.appendChild(option);
|
|
}
|
|
this.selectEl.value = server || '';
|
|
}
|
|
|
|
renderOptions() {
|
|
if (!this.selectEl) return;
|
|
|
|
this.selectEl.innerHTML = '';
|
|
const placeholder = document.createElement('option');
|
|
placeholder.value = '';
|
|
placeholder.textContent = '选择服务器';
|
|
this.selectEl.appendChild(placeholder);
|
|
|
|
this.servers.forEach(server => {
|
|
const option = document.createElement('option');
|
|
option.value = server;
|
|
option.textContent = server;
|
|
this.selectEl.appendChild(option);
|
|
});
|
|
}
|
|
|
|
isValidUrl(value) {
|
|
try {
|
|
new URL(value);
|
|
return true;
|
|
} catch (error) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|