Files
wordpress-theme-aksrt/assets/js/yiyan.js
T
kate522 8aa116ec43 fix(theme): align reference layouts and restore passing contracts
Align homepage and category discovery, modern widgets, admin controls, quotes, and comments with the reference theme. Remove superseded paths and add rendering and behavior coverage. Update stale asset and finance contracts; all 760 Node tests and 139 widget assertions pass.
2026-10-29 19:16:03 +08:00

86 lines
2.6 KiB
JavaScript

/**
* Bilingual quote switcher, matching the reference theme's local quote file.
*
* @package AKSRT
*/
(function () {
'use strict';
var REFRESH_MS = 30000;
function updateQuote(el) {
if (el.dataset.loading === '1') return;
el.dataset.loading = '1';
fetch(el.dataset.endpoint, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: 'action=aksrt_yiyan',
})
.then(function (response) {
if (!response.ok) throw new Error('HTTP ' + response.status);
return response.json();
})
.then(function (response) {
var quote = response && response.success && response.data;
if (!quote || !quote.cn || !quote.en) throw new Error('empty quote');
var cn = el.querySelector('.cn');
var en = el.querySelector('.en');
if (cn) cn.textContent = quote.cn;
if (en) en.textContent = quote.en;
el.classList.remove('is-loading');
el.classList.remove('is-error');
el.classList.add('is-ready');
})
.catch(function () {
el.classList.remove('is-loading');
el.classList.add('is-error');
var cn = el.querySelector('.cn');
if (cn && !el.classList.contains('is-ready')) cn.textContent = '加载失败,点击重试';
})
.finally(function () {
delete el.dataset.loading;
});
}
function init(root) {
var scope = root || document;
var elements = [];
if (scope.matches && scope.matches('[data-aksrt-yiyan]')) elements.push(scope);
if (scope.querySelectorAll) elements = elements.concat(Array.prototype.slice.call(scope.querySelectorAll('[data-aksrt-yiyan]')));
elements.forEach(function (el) {
if (el.dataset.yiyanBound) return;
el.dataset.yiyanBound = '1';
el.addEventListener('click', function () {
updateQuote(el);
});
el.addEventListener('keydown', function (event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
updateQuote(el);
}
});
updateQuote(el);
window.setInterval(function () {
if (!document.hidden) updateQuote(el);
}, REFRESH_MS);
});
}
window.AksrtYiyan = { init: init };
if (window.jQuery) {
window.jQuery(document).off('auto_fun.aksrt-yiyan').on('auto_fun.aksrt-yiyan', function () {
init(document);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init, { once: true });
} else {
init();
}
})();