feat(web): close settings navigation and page lifecycle

This commit is contained in:
abaishu0730 committed 2026-10-19 12:41:51 +08:00
1 parent 539b33cd42
commit 37b4a156c9
19 files changed
+182 -101

No files matched your search

+4 -4
View File
@@ -29,11 +29,11 @@
if(auth.value==='credentials')Object.assign(input,{username:host.querySelector('#storage-username').value,password:passwordless.checked?'':password.value,passwordless:passwordless.checked});return input;
}
async function request(route,input,signal,timeout=18000){
const controller=new AbortController(),cancel=()=>controller.abort(signal.reason);let timedOut=false;
const submission=window.PageLifecycle?.submission,controller=new AbortController(),cancel=()=>controller.abort(signal.reason);let timedOut=false;
if(signal?.aborted)cancel();else signal?.addEventListener('abort',cancel,{once:true});
const timer=setTimeout(()=>{timedOut=true;controller.abort();},timeout);
try{const response=await fetch('/api/storage/servers'+route,{method:'POST',credentials:'same-origin',headers:{'Content-Type':'application/json'},body:JSON.stringify(input),signal:controller.signal});const body=await response.json();if(!response.ok)throw new Error(body.error||'存储操作失败。');return body;}
catch(error){if(timedOut)throw new Error(route===''?'保存响应超时,请刷新列表确认保存结果。':'连接超时,请检查网络后重试。');throw error;}
try{const response=await fetch('/api/storage/servers'+route,{method:'POST',credentials:'same-origin',headers:{'Content-Type':'application/json'},body:JSON.stringify(input),signal:controller.signal});const body=await response.json();if(!response.ok)throw new Error(body.error||'存储操作失败。');if(route==='')window.PageLifecycle?.succeeded(submission);return body;}
catch(error){if(route===''){window.PageLifecycle?.failed(submission);if(timedOut||error instanceof TypeError){api('/storage').then(()=>{if(alive())status.textContent='结果待核实:已查询服务器列表,请核对保存结果;不会自动重试。';}).catch(()=>{});throw new Error('结果待核实:保存响应超时或连接中断,正在查询状态;不会自动重试。');}}if(timedOut)throw new Error('连接超时,请检查网络后重试。');throw error;}
finally{clearTimeout(timer);signal?.removeEventListener('abort',cancel);}
}
function cancelOperation(){const current=operation;operation=null;revision++;checking=false;verified=false;current?.abort();if(alive()){status.textContent='已取消连接操作,可修改后重试。';update();}}
@@ -58,7 +58,7 @@
finally{if(operation===controller){operation=null;checking=false;if(alive())update();}}
}
checkButton.onclick=()=>runConnection(false);browseButton.onclick=()=>runConnection(true);
const observer=new MutationObserver(()=>{if(!form.isConnected){cancelOperation();observer.disconnect();}});observer.observe(container,{childList:true,subtree:true});
const observer=new MutationObserver(()=>{if(!form.isConnected){cancelOperation();observer.disconnect();}});observer.observe(container,{childList:true,subtree:true});window.PageLifecycle?.onDispose(()=>{cancelOperation();observer.disconnect();password.value='';});
host.querySelector('#storage-cancel').onclick=()=>{cancelOperation();observer.disconnect();host.replaceChildren();};
form.onsubmit=async e=>{
e.preventDefault();if(saving||checking)return;if(!verified){errorBox.textContent='请先检查连接或浏览选择可读取目录。';return;}