add global config support
This commit is contained in:
parent
cd1e1a42f3
commit
34d553a890
23 changed files with 1682 additions and 343 deletions
|
|
@ -25,22 +25,26 @@ function getFormStateAsString() {
|
|||
}
|
||||
return JSON.stringify(data);
|
||||
}
|
||||
|
||||
async function attemptExitForm() {
|
||||
if (getFormStateAsString() !== state.initialFormState) {
|
||||
if (await notification.confirm('您有未保存的更改。确定要放弃吗?')) switchView(DOMElements.configListPanel);
|
||||
} else switchView(DOMElements.configListPanel);
|
||||
}
|
||||
|
||||
async function handleLogout() {
|
||||
if (!await notification.confirm('您确定要退出登录吗?')) return;
|
||||
notification.toast('正在退出...', 'info');
|
||||
setTimeout(() => { window.location.href = `/v0/api/auth/logout`; }, 500);
|
||||
}
|
||||
|
||||
async function loadAllConfigs() {
|
||||
try {
|
||||
const filenames = await api.get('/config/filenames');
|
||||
renderConfigList(filenames);
|
||||
} catch(error) { if (error.message) notification.toast(`加载配置列表失败: ${error.message}`, 'error'); }
|
||||
} catch (error) { if (error.message) notification.toast(`加载配置列表失败: ${error.message}`, 'error'); }
|
||||
}
|
||||
|
||||
async function handleEditConfig(originalFilename) {
|
||||
try {
|
||||
const [config, rendered] = await Promise.all([
|
||||
|
|
@ -55,16 +59,18 @@ async function handleEditConfig(originalFilename) {
|
|||
const mode = config.upstream_config?.enable_upstream ? 'reverse_proxy' : (config.file_server_config?.enable_file_server ? 'file_server' : 'none');
|
||||
updateServiceModeView(mode);
|
||||
state.initialFormState = getFormStateAsString();
|
||||
} catch(error) { notification.toast(`加载配置详情失败: ${error.message}`, 'error'); }
|
||||
} catch (error) { notification.toast(`加载配置详情失败: ${error.message}`, 'error'); }
|
||||
}
|
||||
|
||||
async function handleDeleteConfig(filename) {
|
||||
if (!await notification.confirm(`确定要删除配置 "${filename}" 吗?`)) return;
|
||||
try {
|
||||
await api.delete(`/config/file/${filename}`);
|
||||
notification.toast('配置已成功删除。', 'success');
|
||||
loadAllConfigs();
|
||||
} catch(error) { notification.toast(`删除失败: ${error.message}`, 'error'); }
|
||||
} catch (error) { notification.toast(`删除失败: ${error.message}`, 'error'); }
|
||||
}
|
||||
|
||||
async function handleSaveConfig(e) {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(DOMElements.configForm);
|
||||
|
|
@ -115,7 +121,7 @@ async function handleSaveConfig(e) {
|
|||
switchView(DOMElements.configListPanel);
|
||||
loadAllConfigs();
|
||||
}, 500);
|
||||
} catch(error) { notification.toast(`保存失败: ${error.message}`, 'error'); }
|
||||
} catch (error) { notification.toast(`保存失败: ${error.message}`, 'error'); }
|
||||
}
|
||||
|
||||
async function openPresetModal(targetType) {
|
||||
|
|
@ -151,11 +157,11 @@ async function openPresetModal(targetType) {
|
|||
}
|
||||
|
||||
const choice = await notification.confirm('如何填充预设?', '选择填充方式', { confirmText: '追加', cancelText: '替换' });
|
||||
|
||||
|
||||
if (choice === false) { // 用户选择了“替换”
|
||||
targetContainer.innerHTML = '';
|
||||
}
|
||||
|
||||
|
||||
Object.entries(preset.headers).forEach(([key, values]) => {
|
||||
values.forEach(value => {
|
||||
addKeyValueInput(targetContainer, keyName, valueName, key, value);
|
||||
|
|
@ -171,29 +177,30 @@ async function openPresetModal(targetType) {
|
|||
function init() {
|
||||
theme.init(DOMElements.themeToggleInput);
|
||||
notification.init(DOMElements.toastContainer, DOMElements.dialogContainer, DOMElements.modalContainer);
|
||||
activateNav('configs');
|
||||
initCaddyStatus();
|
||||
|
||||
|
||||
loadAllConfigs();
|
||||
|
||||
|
||||
api.get('/config/headers-presets')
|
||||
.then(presets => {
|
||||
state.headerPresets = presets || [];
|
||||
})
|
||||
.catch(err => {
|
||||
.catch(err => {
|
||||
if (err.message) notification.toast(`加载Header预设失败: ${err.message}`, 'error');
|
||||
});
|
||||
|
||||
DOMElements.menuToggleBtn.addEventListener('click', () => DOMElements.sidebar.classList.toggle('is-open'));
|
||||
DOMElements.mainContent.addEventListener('click', () => DOMElements.sidebar.classList.remove('is-open'));
|
||||
|
||||
|
||||
DOMElements.addNewConfigBtn.addEventListener('click', () => {
|
||||
state.isEditing = false;
|
||||
switchView(DOMElements.configFormPanel);
|
||||
DOMElements.formTitle.textContent = '创建新配置';
|
||||
DOMElements.configForm.reset();
|
||||
|
||||
|
||||
const noneButton = DOMElements.serviceModeControl.querySelector('[data-mode="none"]');
|
||||
if(noneButton) updateSegmentedControl(noneButton);
|
||||
if (noneButton) updateSegmentedControl(noneButton);
|
||||
updateServiceModeView('none');
|
||||
updateMultiUpstreamView(false);
|
||||
|
||||
|
|
@ -203,13 +210,13 @@ function init() {
|
|||
DOMElements.multiUpstreamContainer.innerHTML = '';
|
||||
DOMElements.originalFilenameInput.value = '';
|
||||
});
|
||||
|
||||
|
||||
DOMElements.backToListBtn.addEventListener('click', attemptExitForm);
|
||||
DOMElements.cancelEditBtn.addEventListener('click', attemptExitForm);
|
||||
|
||||
|
||||
DOMElements.configForm.addEventListener('submit', handleSaveConfig);
|
||||
DOMElements.logoutBtn.addEventListener('click', handleLogout);
|
||||
|
||||
|
||||
DOMElements.configListContainer.addEventListener('click', e => {
|
||||
const button = e.target.closest('button');
|
||||
if (!button) return;
|
||||
|
|
@ -237,7 +244,7 @@ function init() {
|
|||
e.preventDefault();
|
||||
openPresetModal('upstream');
|
||||
});
|
||||
|
||||
|
||||
DOMElements.addMultiUpstreamBtn.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
addSingleInput(DOMElements.multiUpstreamContainer, 'upstream_servers', '例如: 127.0.0.1:8081');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue