fix: 同步记录弹窗改为局部刷新 (#62)

This commit is contained in:
chengma
2026-08-09 21:02:28 +08:00
parent ee1e060e3a
commit d8a24247df
9 changed files with 243 additions and 62 deletions
+63 -10
View File
@@ -265,17 +265,70 @@
});
}
/* 同步记录继续由服务端渲染。刷新按钮带着 history=1 和当前页码重新加载,
返回后弹窗会自动打开;点击后先显示加载态,避免连续刷新。 */
/* 同步记录继续由服务端渲染,但只替换弹窗内部的 HTML 片段。
事件委托挂在不会被替换的 slot 上,所以刷新和翻页后不用重新绑定。 */
function setupSybHistoryRefresh() {
var button = document.querySelector("[data-history-refresh-url]");
if (!button) return;
button.addEventListener("click", function () {
if (button.disabled) return;
button.disabled = true;
button.setAttribute("aria-busy", "true");
button.textContent = "刷新中…";
window.location.assign(button.getAttribute("data-history-refresh-url"));
var slot = document.querySelector("[data-syb-history-slot]");
if (!slot) return;
slot.addEventListener("click", function (event) {
var trigger = event.target.closest("[data-history-fetch-url]");
if (!trigger || !slot.contains(trigger)) return;
event.preventDefault();
if (slot.getAttribute("aria-busy") === "true") return;
var fetchURL = trigger.getAttribute("data-history-fetch-url");
var refreshButton = slot.querySelector("button[data-history-fetch-url]");
var loading = slot.querySelector("[data-history-loading]");
var previousError = slot.querySelector("[data-history-load-error]");
var isRefreshButton = trigger === refreshButton;
slot.setAttribute("aria-busy", "true");
slot.querySelectorAll("[data-history-fetch-url]").forEach(function (control) {
control.setAttribute("aria-disabled", "true");
});
if (refreshButton) refreshButton.disabled = true;
if (loading) loading.hidden = false;
if (previousError) previousError.hidden = true;
if (isRefreshButton) refreshButton.textContent = "刷新中…";
fetch(fetchURL, {
cache: "no-store",
headers: { "X-Requested-With": "fetch" }
})
.then(function (response) {
if (response.redirected) {
window.location.assign(response.url);
return null;
}
if (!response.ok) throw new Error("HTTP " + response.status);
return response.text();
})
.then(function (html) {
if (html === null) return;
slot.innerHTML = html;
slot.removeAttribute("aria-busy");
var nextRefreshButton = slot.querySelector("button[data-history-fetch-url]");
if (nextRefreshButton) nextRefreshButton.focus({ preventScroll: true });
})
.catch(function (error) {
slot.removeAttribute("aria-busy");
slot.querySelectorAll("[data-history-fetch-url]").forEach(function (control) {
control.removeAttribute("aria-disabled");
});
refreshButton = slot.querySelector("button[data-history-fetch-url]");
loading = slot.querySelector("[data-history-loading]");
var errorBox = slot.querySelector("[data-history-load-error]");
if (refreshButton) {
refreshButton.disabled = false;
refreshButton.textContent = "刷新同步记录";
refreshButton.focus({ preventScroll: true });
}
if (loading) loading.hidden = true;
if (errorBox) {
errorBox.textContent = "刷新同步记录失败:" + error.message + "。请重试。";
errorBox.hidden = false;
}
});
});
}