初始化政体志子站
收录 zhengquan-site 前后端完整代码:24 朝数据、24 幅权力结构图、 5 条沿革(67 节点 / 269 条细释)、44 个词条,以及部署配置与接口测试。
This commit is contained in:
commit
aa12b9f910
58 files changed
+16253
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { search } from '../api.js';
|
||||
import { esc, highlight } from '../util.js';
|
||||
|
||||
const HOT = ['三省六部', '军机处', '科举制', '行省', '二府三司', '内阁', '刺史', '九品中正制'];
|
||||
|
||||
function resultRow(r, q) {
|
||||
return `
|
||||
<a class="result" href="${esc(r.url)}">
|
||||
<div class="result-title">${highlight(r.title, q)}</div>
|
||||
<div class="result-sub">${esc(r.subtitle)}</div>
|
||||
<div class="result-snippet">${highlight(r.snippet, q)}</div>
|
||||
</a>`;
|
||||
}
|
||||
|
||||
export async function renderSearch({ query }) {
|
||||
const q = (query.get('q') || '').trim();
|
||||
const data = q ? await search(q) : null;
|
||||
|
||||
let body = '';
|
||||
if (!q) {
|
||||
body = `
|
||||
<div class="hot-terms">
|
||||
<span>试着检索</span>
|
||||
${HOT.map((t) => `<a class="chip-btn" href="#/search?q=${encodeURIComponent(t)}">${esc(t)}</a>`).join('')}
|
||||
</div>
|
||||
<div class="search-scope">
|
||||
<a href="#/"><b>二十四朝</b><span>按时代与国祚翻阅</span></a>
|
||||
<a href="#/threads"><b>五条沿革</b><span>宰相、监察、地方、选官、军事</span></a>
|
||||
<a href="#/glossary"><b>四十词条</b><span>职官与制度术语释义</span></a>
|
||||
</div>
|
||||
<p class="search-hint">检索范围包括朝代名与概要、国体政体表述、中央机构与长官、地方层级、沿革要点、史料引文,以及五条沿革的全部节点与词条释义。</p>`;
|
||||
} else if (!data.total) {
|
||||
body = `<p class="empty">没有找到与「${esc(q)}」相关的内容。可换个说法,或从总目逐朝翻阅。</p>`;
|
||||
} else {
|
||||
const groups = [
|
||||
['朝代', data.groups.dynasty],
|
||||
['沿革', data.groups.thread],
|
||||
['词条', data.groups.term],
|
||||
].filter(([, list]) => list.length);
|
||||
body = `
|
||||
<p class="search-meta">共 ${data.total} 条结果,显示 ${data.groups.dynasty.length + data.groups.thread.length + data.groups.term.length} 条</p>
|
||||
<div class="search-groups">
|
||||
${groups.map(([label, list]) => `
|
||||
<section class="search-group">
|
||||
<h2>${label}</h2>
|
||||
${list.map((r) => resultRow(r, q)).join('')}
|
||||
</section>`).join('')}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
const html = `
|
||||
<header class="search-hero">
|
||||
<p class="kicker">检索 · 全志</p>
|
||||
<h1>检索</h1>
|
||||
<form class="search-form" data-search-form role="search">
|
||||
<input class="search-input" type="search" name="q" value="${esc(q)}" placeholder="朝代、机构、制度、人物……" aria-label="检索全志" autofocus />
|
||||
<button class="btn" type="submit">检索</button>
|
||||
</form>
|
||||
</header>
|
||||
${body}
|
||||
`;
|
||||
|
||||
function mount() {
|
||||
const form = document.querySelector('[data-search-form]');
|
||||
form?.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const val = form.querySelector('input[name="q"]').value.trim();
|
||||
location.hash = val ? `#/search?q=${encodeURIComponent(val)}` : '#/search';
|
||||
});
|
||||
}
|
||||
|
||||
return { html, title: q ? `检索 · ${q}` : '检索', mount };
|
||||
}
|
||||
Reference in new issue
Block a user