Files
wordpress-theme-aksrt/assets/js/core/pagination.js
T
kate522andClaude Fable 5 89a9cf24e9 fix(posts): repair the load-more skeleton and align it with the reference
Clicking 加载更多 showed no skeleton at all, and the placeholder shape did
not match the reference theme's post cards.

- Class-name mismatch: pagination.js queried .post-ajax-loader while every
  PHP producer emits .post_ajax_loader, so showSkeleton() never found a
  target and the loader stayed hidden. Accept both spellings.
- Inline display:none on the loader (pagination, comments, widget tab
  panes) outranked .is-loading, so even the correct selector could not
  reveal it. Drop the inline style; the reference uses a bare
  <div class="post_ajax_loader"> and toggles visibility from JS.
- loadNextPage() set indicator.style.display = "flex", which forced the
  .posts-item.card skeleton blocks (inline-block layout) into a single
  row instead of stacking vertically — the visible symptom. Remove those
  inline writes and drive .is-loading from CSS for every surface.
- Scope the hide/show rules on the class itself rather than requiring an
  .ajaxpager ancestor: homepage tab panes, archive streams and comment
  lists sit at three different depths.
- Rebuild aksrt_render_placeholder() on zib_get_post_placeholder's
  structure — list = t1/k1/k2 + two s1, card = t1 item-heading/k2 + two
  s1, thumb wrapped in post-graphic + radius8 item-thumbnail, all carrying
  ajax-item so the loader is not mistaken for a real card.
- Add mini/mini_thumb modes so ContentRenderer::placeholder() stops
  hand-rolling the same markup; normalize its 'lists' arg to 'list'.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-10-03 15:42:18 +08:00

301 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* AKSRT AJAX Pagination & Lazy Load
*/
(function () {
"use strict";
function hasAjaxConfig() {
return !!(window.aksrtData && aksrtData.ajaxUrl && aksrtData.nonce);
}
function initAjaxPagination(root) {
var containers = (root || document).querySelectorAll("[data-aksrt-pagination], .aksrt-comment-pagination");
if (!containers.length) return;
var triggerText = (window.aksrtData && aksrtData.ajaxTrigger) || "加载更多";
var nomoreText = (window.aksrtData && aksrtData.ajaxNomore) || "没有更多了";
var loadingText = (window.aksrtData && aksrtData.ajaxLoading) || "正在加载...";
var errorText = (window.aksrtData && aksrtData.ajaxLoadError) || "加载失败,请重试";
containers.forEach(function (container) {
if (container.dataset.aksrtPaginationBound === "1") return;
container.dataset.aksrtPaginationBound = "1";
var mode = container.dataset.mode || "load_more";
var nextPage = parseInt(container.dataset.nextPage, 10) || 2;
var maxPages = parseInt(container.dataset.maxPages, 10) || 1;
var isComment = container.classList.contains("aksrt-comment-pagination");
var postId = container.dataset.postId || "";
var commentOrder = container.dataset.commentOrder || "newest";
var loading = false;
var failed = false;
var clickTarget = container.querySelector(".next-page.ajax-next, .ias-btn") || container;
var loadMoreBtn = container.querySelector("[data-aksrt-load-more]") || clickTarget;
var successfulAutoLoads = 0;
var iasMax = isComment
? ((window.aksrtData && typeof aksrtData.commentIasMax !== "undefined") ? parseInt(aksrtData.commentIasMax, 10) : 0)
: ((window.aksrtData && typeof aksrtData.iasMax !== "undefined") ? parseInt(aksrtData.iasMax, 10) : 0);
// legacy 契约:分页容器与列表同处一个 .ajaxpager 内(首页 tab pane、归档流),
// 此时追加目标就是列表容器本身,新卡片插到分页控件之前。
var listContainer = container.closest(".ajaxpager, .posts-row");
var target = container.previousElementSibling;
if (listContainer && container.parentNode === listContainer) {
target = listContainer;
}
if (isComment) {
var commentList = container.closest("#comment-list, .commentlist, ol.commentlist") ||
document.getElementById("comment-list");
if (commentList) target = commentList;
}
if (!target && container.parentElement) {
target = container.parentElement.querySelector(".post-grid, .comment-list, .commentlist, .content-stream__list");
}
function showSkeleton() {
if (!target) return;
// PHP 产出下划线类名 post_ajax_loader;这里同时兼容连字符写法。
var loader = target.querySelector(".post_ajax_loader, .post-ajax-loader");
if (loader) {
loader.classList.add("is-loading");
}
}
function hideSkeleton() {
if (!target) return;
var loader = target.querySelector(".post_ajax_loader, .post-ajax-loader");
if (loader) {
loader.classList.remove("is-loading");
}
}
function stopInfinite(markNomore) {
container.classList.remove("ias-pending");
if (markNomore && loadMoreBtn) {
loadMoreBtn.innerHTML = nomoreText;
loadMoreBtn.classList.remove("is-loading");
loadMoreBtn.classList.add("ajax-nomore");
var nextWrap = loadMoreBtn.closest(".next-page");
if (nextWrap) nextWrap.classList.add("no-more");
loadMoreBtn.style.pointerEvents = "none";
} else if (loadMoreBtn && !loadMoreBtn.classList.contains("ajax-nomore")) {
loadMoreBtn.classList.remove("is-loading");
if (failed) {
loadMoreBtn.textContent = errorText;
} else {
loadMoreBtn.innerHTML = triggerText;
}
loadMoreBtn.style.pointerEvents = "";
}
}
function markFailed(message) {
failed = true;
stopInfinite(false);
if (loadMoreBtn) {
loadMoreBtn.textContent = message || errorText;
loadMoreBtn.classList.remove("is-loading");
loadMoreBtn.classList.remove("ajax-nomore");
loadMoreBtn.style.pointerEvents = "";
}
}
function loadNextPage(fromAuto) {
if (loading || failed || nextPage > maxPages || !target) return Promise.resolve(false);
if (!hasAjaxConfig()) {
markFailed(errorText);
return Promise.resolve(false);
}
loading = true;
// 骨架开关统一走 .is-loading(modal-runtime.css),不要直接写
// display:flex —— 那会让卡片型骨架挤成一行而不是纵向堆叠。
if (loadMoreBtn) {
loadMoreBtn.classList.add("is-loading");
loadMoreBtn.textContent = loadingText;
}
showSkeleton();
var action = isComment ? "aksrt_load_more_comments" : "aksrt_load_more_posts";
var queryContext = container.dataset.queryContext || "";
var queryVars = container.dataset.queryVars || "";
var renderConfig = container.dataset.renderConfig || "";
var body = new URLSearchParams();
body.set("action", action);
body.set("nonce", aksrtData.nonce);
body.set("page", String(nextPage));
if (queryContext) {
body.set("query_context", queryContext);
}
if (!isComment && queryVars) {
body.set("query_vars", queryVars);
}
if (!isComment && renderConfig) {
body.set("render_config", renderConfig);
}
if (isComment && postId) {
body.set("post_id", postId);
body.set("comment_order", commentOrder);
var authorOnly = container.dataset.authorOnly || "0";
if (authorOnly === "1") body.set("author_only", "1");
}
return fetch(aksrtData.ajaxUrl, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" },
credentials: "same-origin",
body: body.toString(),
})
.then(function (r) {
if (!r.ok) {
throw new Error("HTTP " + r.status);
}
return r.text().then(function (text) {
try {
return JSON.parse(text);
} catch (err) {
throw new Error("Invalid JSON");
}
});
})
.then(function (res) {
if (!(res && res.success && res.data && typeof res.data.html === "string")) {
var msg = res && res.data && res.data.message ? res.data.message : errorText;
markFailed(msg);
return false;
}
var temp = document.createElement("div");
temp.innerHTML = res.data.html;
var newItems = temp.children;
while (newItems.length > 0) {
if (container.parentNode === target) {
target.insertBefore(newItems[0], container);
} else {
target.appendChild(newItems[0]);
}
}
// legacy 事件顺序:先 post_ajax.ed,再全局 auto_fun 重扫
target.dispatchEvent(new CustomEvent("post_ajax.ed", { bubbles: true }));
if (typeof window.auto_fun === "function") window.auto_fun();
nextPage++;
container.dataset.nextPage = String(nextPage);
if (fromAuto) {
successfulAutoLoads++;
}
if (nextPage > maxPages || !res.data.has_more) {
stopInfinite(true);
if (!loadMoreBtn) {
container.innerHTML = '<span class="ajax-nomore">' + nomoreText + '</span>';
}
}
return true;
})
.catch(function () {
markFailed(errorText);
return false;
})
.finally(function () {
loading = false;
hideSkeleton();
if (loadMoreBtn && !loadMoreBtn.classList.contains("ajax-nomore") && !failed) {
loadMoreBtn.classList.remove("is-loading");
loadMoreBtn.innerHTML = triggerText;
if (mode === "infinite") {
var lazyTrigger = clickTarget.closest(".next-page") || clickTarget;
if (iasMax > 0 && successfulAutoLoads >= iasMax) {
container.classList.remove("ias-pending");
lazyTrigger.classList.remove("lazyload", "lazyloaded", "lazyloading");
lazyTrigger.removeAttribute("lazyload-action");
} else {
lazyTrigger.classList.remove("lazyloaded", "lazyloading");
lazyTrigger.classList.add("lazyload");
if (window.lazySizes && lazySizes.loader) {
lazySizes.loader.checkElems();
}
}
}
}
});
}
if (loadMoreBtn) {
clickTarget.addEventListener("click", function(e) {
e.preventDefault();
if (loadMoreBtn.dataset.jumpComments === "1") {
var jumpInput = container.querySelector(".jump-input");
if (!jumpInput) return;
var targetPage = parseInt(jumpInput.value, 10) || 1;
var max = parseInt(jumpInput.dataset.maxPages || String(maxPages), 10) || maxPages;
targetPage = Math.max(1, Math.min(max, targetPage));
// 评论跳页:本地 DOM 刷新,地址栏不携带 ? 参数。
if (window.AksrtComments && typeof window.AksrtComments.loadPage === "function") {
e.preventDefault();
window.AksrtComments.loadPage(targetPage);
return;
}
window.location.hash = "comments";
window.location.reload();
return;
}
if (loadMoreBtn.dataset.jumpArchive === "1") {
var archiveJumpInput = container.querySelector(".jump-input");
if (!archiveJumpInput) return;
var archiveTargetPage = parseInt(archiveJumpInput.value, 10) || 1;
var archiveMax = parseInt(archiveJumpInput.dataset.maxPages || String(maxPages), 10) || maxPages;
archiveTargetPage = Math.max(1, Math.min(archiveMax, archiveTargetPage));
var archiveUrl = new URL(window.location.href);
archiveUrl.searchParams.set(loadMoreBtn.dataset.pageVar || "paged", String(archiveTargetPage));
window.location.href = archiveUrl.toString();
return;
}
var fromAuto = clickTarget.dataset.lazyAuto === "1";
delete clickTarget.dataset.lazyAuto;
if (fromAuto && iasMax > 0 && successfulAutoLoads >= iasMax) {
container.classList.remove("ias-pending");
return;
}
// Manual click can recover from a previous auto-load failure.
failed = false;
loadNextPage(fromAuto);
});
}
// The runtime lazySizes dispatches lazybeforeunveil natively but follows up
// with a jQuery-simulated click, which native listeners never receive.
// Route the unveil trigger through the same click pipeline.
if (container.classList.contains("lazyload") || clickTarget.classList.contains("lazyload")) {
container.addEventListener("lazybeforeunveil", function (e) {
if (e.target !== container && e.target !== clickTarget) return;
clickTarget.dataset.lazyAuto = "1";
clickTarget.click();
});
}
if (mode === "infinite") {
if ((isComment && window.aksrtData && aksrtData.commentIasAuto === false) || !hasAjaxConfig()) {
var autoTrigger = clickTarget.closest(".next-page") || clickTarget;
autoTrigger.classList.remove("lazyload");
autoTrigger.removeAttribute("lazyload-action");
return;
}
container.classList.add("ias-pending");
}
});
}
function init(root) {
initAjaxPagination(root);
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", function () { init(); });
} else {
init();
}
// AJAX 注入新内容后由 auto_fun 重扫(container 级 data 标记保证幂等)。
if (window.jQuery) {
window.jQuery(document).on("auto_fun", function () { init(); });
}
})();