- rehydrate.js 新增 boot()/aksrt:ready 事件,支持 boot 后晚注册立即以 init 触发 - aksrt-runtime.js 初始加载改走 AksrtFrontend.boot,tooltip/倒计时注册为组件 - 约 30 个前端脚本(首页、评论、支付、路由、页面叶子、widgets) 从直接 DOMContentLoaded 迁移为 AksrtFrontend.register,并加幂等标记 - section-navs 依赖改为 aksrt-runtime,保证注册时序 - 清理死代码:删除孤儿 loader.js/main.js 及 runtime/page-navs 遗留副本 - loader.js 移除已迁移模块的 tbquire 路径映射,main.js 相应瘦身 - build-public-assets.mjs 增加 runtime/main.min.js 自动压缩步骤 - 新增 4 个契约测试(homepage-dom/rehydrate-runtime/global-dom-init/ public-bundle-parity),3 个既有 harness 适配注册表初始化方式
199 lines
6.7 KiB
JavaScript
199 lines
6.7 KiB
JavaScript
/**
|
|
* AKSRT Misc — related swiper, slide covers, hidden content
|
|
*/
|
|
(function () {
|
|
"use strict";
|
|
|
|
var globalsBound = false;
|
|
|
|
function scopeFor(root) {
|
|
return root && root.querySelectorAll ? root : document;
|
|
}
|
|
|
|
function eachMatch(root, selector, callback) {
|
|
var scope = scopeFor(root);
|
|
if (scope.nodeType === 1 && scope.matches(selector)) callback(scope);
|
|
scope.querySelectorAll(selector).forEach(callback);
|
|
}
|
|
|
|
function renderGradientBackground(container) {
|
|
if (!container || container.dataset.aksrtGradientState || typeof window.Grade !== "function") return;
|
|
|
|
var source = container.querySelector("img");
|
|
if (!source || (!source.classList.contains("lazyloaded") && !(source.complete && source.naturalWidth > 0))) return;
|
|
|
|
container.dataset.aksrtGradientState = "loading";
|
|
window.Grade(container, null, function (result) {
|
|
var gradientData = result && result[0];
|
|
var colors = gradientData && gradientData.gradientData;
|
|
if (!gradientData || !colors || !colors[0] || !colors[1]) {
|
|
delete container.dataset.aksrtGradientState;
|
|
return;
|
|
}
|
|
|
|
var opacity = container.getAttribute("data-opacity");
|
|
var colorName = opacity ? "rgba" : "rgb";
|
|
var alpha = opacity ? "," + opacity : "";
|
|
var color1 = colorName + "(" + colors[0].rgba.slice(0, 3).join(",") + alpha + ") 10%";
|
|
var color2 = colorName + "(" + colors[1].rgba.slice(0, 3).join(",") + alpha + ") 80%";
|
|
|
|
gradientData.element.classList.add("gradient-bg-loaded");
|
|
gradientData.element.style.backgroundImage = "linear-gradient(135deg, " + color1 + ", " + color2 + ")";
|
|
container.dataset.aksrtGradientState = "loaded";
|
|
});
|
|
}
|
|
|
|
function initGradientBackgrounds(root) {
|
|
eachMatch(root, ".gradient-bg", renderGradientBackground);
|
|
}
|
|
|
|
function initPostThumbMedia(root) {
|
|
eachMatch(root, "[data-post-thumb-video]", function (box) {
|
|
if (box.dataset.aksrtThumbVideoBound === "1") return;
|
|
var video = box.querySelector("video");
|
|
if (!video) return;
|
|
box.dataset.aksrtThumbVideoBound = "1";
|
|
|
|
function loadVideo() {
|
|
if (!video.getAttribute("src") && video.dataset.src) {
|
|
video.setAttribute("src", video.dataset.src);
|
|
video.load();
|
|
}
|
|
}
|
|
|
|
function playVideo() {
|
|
loadVideo();
|
|
var result = video.play();
|
|
var postItem = box.closest(".posts-item");
|
|
if (postItem) postItem.classList.add("thumb-dplayer-playing");
|
|
if (result && typeof result.catch === "function") result.catch(function () {});
|
|
}
|
|
|
|
function pauseVideo() {
|
|
video.pause();
|
|
var postItem = box.closest(".posts-item");
|
|
if (postItem) postItem.classList.remove("thumb-dplayer-playing");
|
|
}
|
|
|
|
var item = box.closest(".item-thumbnail") || box;
|
|
item.addEventListener("mouseenter", playVideo);
|
|
item.addEventListener("mouseleave", pauseVideo);
|
|
item.addEventListener("focusin", playVideo);
|
|
item.addEventListener("focusout", pauseVideo);
|
|
});
|
|
}
|
|
|
|
function initSlideCovers(root) {
|
|
eachMatch(root, ".single-cover--slide", function (cover) {
|
|
if (cover.dataset.aksrtSlideCoverBound === "1") return;
|
|
var slides = cover.querySelectorAll(".single-cover__slide");
|
|
var dots = cover.querySelectorAll(".single-cover__dot");
|
|
if (slides.length < 2) return;
|
|
cover.dataset.aksrtSlideCoverBound = "1";
|
|
|
|
var current = 0;
|
|
var interval = 5000;
|
|
var timer = null;
|
|
|
|
function goTo(idx) {
|
|
slides[current].classList.remove("is-active");
|
|
if (dots[current]) dots[current].classList.remove("is-active");
|
|
current = idx;
|
|
slides[current].classList.add("is-active");
|
|
if (dots[current]) dots[current].classList.add("is-active");
|
|
}
|
|
|
|
function startTimer() {
|
|
timer = window.setInterval(function () {
|
|
goTo((current + 1) % slides.length);
|
|
}, interval);
|
|
}
|
|
|
|
startTimer();
|
|
dots.forEach(function (dot, i) {
|
|
dot.addEventListener("click", function () {
|
|
window.clearInterval(timer);
|
|
goTo(i);
|
|
startTimer();
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function bindGlobals() {
|
|
if (globalsBound) return;
|
|
globalsBound = true;
|
|
|
|
document.addEventListener("lazyloaded", function (event) {
|
|
if (!(event.target instanceof Element) || !event.target.matches(".gradient-bg img")) return;
|
|
renderGradientBackground(event.target.closest(".gradient-bg"));
|
|
}, true);
|
|
|
|
document.addEventListener("click", function (e) {
|
|
var btn = e.target.closest(".aksrt-hidden-content__submit-btn");
|
|
if (!btn) return;
|
|
|
|
e.preventDefault();
|
|
var wrapper = btn.closest(".aksrt-hidden-content--locked");
|
|
if (!wrapper) return;
|
|
|
|
var mode = wrapper.getAttribute("data-aksrt-visibility-mode");
|
|
var pid = wrapper.getAttribute("data-aksrt-visibility-pid");
|
|
var input = wrapper.querySelector(".aksrt-hidden-content__password-input");
|
|
var password = input ? input.value.trim() : "";
|
|
|
|
if (mode === "password" && !password) {
|
|
if (input) input.focus();
|
|
return;
|
|
}
|
|
|
|
var formData = new FormData();
|
|
formData.append("action", "aksrt_check_visibility");
|
|
formData.append("_nonce", (window.aksrtData && window.aksrtData.visibilityNonce) || "");
|
|
formData.append("mode", mode);
|
|
formData.append("pid", pid);
|
|
if (password) formData.append("password", password);
|
|
|
|
btn.disabled = true;
|
|
btn.textContent = "...";
|
|
|
|
fetch((window.aksrtData && window.aksrtData.ajaxUrl) || "/wp-admin/admin-ajax.php", {
|
|
method: "POST",
|
|
body: formData,
|
|
credentials: "same-origin",
|
|
})
|
|
.then(function (res) { return res.json(); })
|
|
.then(function (data) {
|
|
if (data.success) {
|
|
window.location.reload();
|
|
} else {
|
|
var msg = (data.data && data.data.message) ? data.data.message : "验证失败";
|
|
alert(msg);
|
|
btn.disabled = false;
|
|
btn.textContent = "验证";
|
|
}
|
|
})
|
|
.catch(function () {
|
|
alert("网络错误,请重试");
|
|
btn.disabled = false;
|
|
btn.textContent = "验证";
|
|
});
|
|
});
|
|
|
|
document.addEventListener("keydown", function (e) {
|
|
if (e.key !== "Enter" || !e.target.classList.contains("aksrt-hidden-content__password-input")) return;
|
|
e.preventDefault();
|
|
var wrapper = e.target.closest(".aksrt-hidden-content--locked");
|
|
var btn = wrapper ? wrapper.querySelector(".aksrt-hidden-content__submit-btn") : null;
|
|
if (btn) btn.click();
|
|
});
|
|
}
|
|
|
|
window.AksrtFrontend.register("aksrt-misc", function (root, reason) {
|
|
bindGlobals();
|
|
initPostThumbMedia(root);
|
|
initGradientBackgrounds(root);
|
|
initSlideCovers(root);
|
|
});
|
|
})();
|