fix(widgets): replace old page before showing pagination skeleton

This commit is contained in:
kate522 committed 2026-10-29 21:29:17 +08:00
1 parent 985f18c750
commit 4c13ef478c
4 files changed
+189 -29

No files matched your search

+24 -21
View File
@@ -121,9 +121,8 @@
}
}
// 参考主题 post_ajax 的 beforeSend 语义:骨架每次请求都挪到列表末尾
// (最后一个条目之后、分页控件之前),请求成功后只隐藏而不删除,
// 下一轮翻页复用同一个节点。
// 追加时骨架紧跟已有条目;替换时先清空旧页,骨架留在新页的位置。
// 请求成功后隐藏并保留同一个骨架节点,供下一轮请求复用。
function anchorLoader(container) {
var pag = container.querySelector(':scope > .ajax-pag');
var loader = loaderOf(container);
@@ -144,40 +143,44 @@
if (!widget || !instance || !config.restUrl || container.dataset.loading === '1') return;
container.querySelectorAll(':scope > .aksrt-widget-request-error').forEach(function (error) { error.remove(); });
setBusy(container, true);
var previous = [];
if (!append) {
// 对齐参考主题 beforeSend:数字分页与跳页先移除旧内容,而非等待响应。
// 暂存原节点,只在请求失败时恢复内容和分页控件。
var loader = loaderOf(container);
Array.prototype.slice.call(container.children).forEach(function (child) {
if (child === loader) return;
previous.push(child);
child.remove();
});
}
anchorLoader(container);
return fetch(itemsUrl(widget, instance, page, segment), { credentials: 'same-origin', headers: { Accept: 'application/json' } })
.then(function (response) { if (!response.ok) throw new Error('items'); return response.json(); })
.then(function (data) {
var fragment = document.createElement('div');
fragment.innerHTML = String(data.html || '');
if (append) {
// 追加方向为底部:新条目插到骨架之前,骨架始终贴住最后一张卡片。
var anchor = loaderOf(container) || container.querySelector(':scope > .ajax-pag');
Array.prototype.slice.call(fragment.children).forEach(function (node) {
if (anchor && anchor.parentNode === container) {
container.insertBefore(node, anchor);
} else {
container.appendChild(node);
}
});
} else {
// 替换时保留骨架节点本身(参考主题 success 后只 hide() 不删除)。
var loader = loaderOf(container);
Array.prototype.slice.call(container.children).forEach(function (child) {
if (child !== loader && !child.classList.contains('ajax-pag')) child.remove();
});
Array.prototype.slice.call(fragment.children).forEach(function (node) { container.appendChild(node); });
}
var anchor = loaderOf(container) || container.querySelector(':scope > .ajax-pag');
Array.prototype.slice.call(fragment.children).forEach(function (node) {
if (anchor && anchor.parentNode === container) {
container.insertBefore(node, anchor);
} else {
container.appendChild(node);
}
});
Array.prototype.slice.call(container.querySelectorAll(':scope > .ajax-pag')).forEach(function (node) { node.remove(); });
if (data.paginate_html) container.insertAdjacentHTML('beforeend', data.paginate_html);
container.dataset.page = String(data.page || page);
container.dataset.loaded = '1';
anchorLoader(container);
previous = [];
scan(container);
if (window.jQuery) window.jQuery(document).trigger('auto_fun');
})
.catch(function () {
var loader = loaderOf(container);
previous.forEach(function (node) { container.insertBefore(node, loader); });
anchorLoader(container);
if (loader && loader.parentNode === container) {
loader.insertAdjacentHTML('beforebegin', errorMarkup(page, append));
} else {