Files
kate522andClaude Opus 5 f80ca1e27d fix(widgets): restore single-card reference UI for search and quote widgets
The 搜索框 and 一言 sidebar widgets fell into the generic renderer branch, so
they rendered a .jio-panel card wrapping a second bespoke card
(.jio-widget-search / .jio-widget-quote). In-article 一言 also used
divergent modifier classes and an extra hover rule.

- search: render through the shared jio_get_search_box() (legacy keyword,
  history and category pickers) inside one reference card, mirroring the
  reference widget's markup and title default 「搜索」
- quote: render the reference .yiyan-box card itself as the click root; the
  shell now emits only the reference widget-container/title wrapper
- article 一言: adopt the reference class lists for header, footer and the
  standalone after-article box; drop the invented yiyan--* modifiers and
  the duplicate hover/opacity rules
- client: fill .cn/.en in the reference order (first line .cn, second .en)
  and drop the stale window.JioYiyan bridge plus the dead qv-yiyan.php AJAX
  path in runtime main.js
- schema: replace the ad-hoc search_style radio with the reference switches
  (keywords, history, input category, more_cats) and default title
- assets: enqueue assets/js/yiyan.js through the quote widget's asset list

Tests: widget-system suite 820/821 (the one failure is the pre-existing
frontend-lifecycle theme-init stub); new search/quote card contracts pass.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-03 15:56:52 +08:00

100 lines
3.1 KiB
JavaScript

/**
* Bilingual quote switcher, matching the reference theme's local quote file.
*
* @package Jio
*/
(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=jio_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');
// 参考结构:.yiyan 初始为空,由脚本写入首行 .cn 与次行 .en。
var body = el.querySelector('.yiyan');
if (body) {
var cn = body.querySelector('.cn');
var en = body.querySelector('.en');
if (cn && en) {
cn.textContent = quote.en;
en.textContent = quote.cn;
} else {
body.innerHTML = '';
var lineCn = document.createElement('div');
lineCn.className = 'cn';
lineCn.textContent = quote.en;
var lineEn = document.createElement('div');
lineEn.className = 'en';
lineEn.textContent = quote.cn;
body.appendChild(lineCn);
body.appendChild(lineEn);
}
}
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 body = el.querySelector('.yiyan');
if (body && !el.classList.contains('is-ready')) body.textContent = '加载失败,点击重试';
})
.finally(function () {
delete el.dataset.loading;
});
}
function init(root) {
var scope = root || document;
var elements = [];
if (scope.matches && scope.matches('[data-jio-yiyan]')) elements.push(scope);
if (scope.querySelectorAll) elements = elements.concat(Array.prototype.slice.call(scope.querySelectorAll('[data-jio-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);
});
}
if (window.jQuery) {
window.jQuery(document).off('auto_fun.jio-yiyan').on('auto_fun.jio-yiyan', function () {
init(document);
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init, { once: true });
} else {
init();
}
})();