收录 zhengquan-site 前后端完整代码:24 朝数据、24 幅权力结构图、 5 条沿革(67 节点 / 269 条细释)、44 个词条,以及部署配置与接口测试。
194 lines
7.0 KiB
JavaScript
194 lines
7.0 KiB
JavaScript
import { esc } from './util.js';
|
||
|
||
const NAME = 15;
|
||
const ROOT_NAME = 17;
|
||
const NOTE = 11;
|
||
const CHIP = 11.5;
|
||
const CHIP_H = 17;
|
||
const CHIP_GAP = 4;
|
||
const ROW_H = 19;
|
||
const PAD_TOP = 9;
|
||
const MAX_W = 268;
|
||
const MIN_W = 92;
|
||
const MIN_ROOT = 150;
|
||
const GAP_X = 18;
|
||
const GAP_Y = 46;
|
||
const CANVAS = 18;
|
||
|
||
function textWidth(text, size) {
|
||
let w = 0;
|
||
for (const ch of String(text)) {
|
||
if (/[\u4e00-\u9fa5]/.test(ch)) w += size;
|
||
else if (/[A-Za-z0-9]/.test(ch)) w += size * 0.56;
|
||
else if (ch === '·' || ch === '—' || ch === '(' || ch === ')') w += size * 0.92;
|
||
else w += size * 0.46;
|
||
}
|
||
return w;
|
||
}
|
||
|
||
function build(raw, depth, isRoot) {
|
||
const node = {
|
||
...raw,
|
||
depth,
|
||
isRoot,
|
||
nameSize: isRoot ? ROOT_NAME : NAME,
|
||
children: (raw.children || []).map((c) => build(c, depth + 1, false)),
|
||
};
|
||
|
||
const nameW = textWidth(node.name, node.nameSize);
|
||
const noteW = node.note ? textWidth(node.note, NOTE) : 0;
|
||
const chips = (node.items || []).map((text) => ({
|
||
text,
|
||
w: textWidth(text, CHIP) + 14,
|
||
}));
|
||
|
||
const rows = [];
|
||
let row = [];
|
||
let rowW = 0;
|
||
let maxRowW = 0;
|
||
for (const chip of chips) {
|
||
if (row.length && rowW + CHIP_GAP + chip.w > MAX_W - 24) {
|
||
rows.push({ items: row, w: rowW });
|
||
maxRowW = Math.max(maxRowW, rowW);
|
||
row = [];
|
||
rowW = 0;
|
||
}
|
||
rowW += (row.length ? CHIP_GAP : 0) + chip.w;
|
||
row.push(chip);
|
||
}
|
||
if (row.length) {
|
||
rows.push({ items: row, w: rowW });
|
||
maxRowW = Math.max(maxRowW, rowW);
|
||
}
|
||
|
||
node.rows = rows;
|
||
node.w = Math.max(isRoot ? MIN_ROOT : MIN_W, nameW + 26, noteW + 20, maxRowW + 24);
|
||
node.w = Math.min(node.w, MAX_W);
|
||
node.h = PAD_TOP + 21 + (node.note ? 14 : 0) + (rows.length ? rows.length * ROW_H + 4 : 0) + 9;
|
||
|
||
const childTotal = node.children.length
|
||
? node.children.reduce((sum, c) => sum + c.sw, 0) + GAP_X * (node.children.length - 1)
|
||
: 0;
|
||
node.sw = Math.max(node.w, childTotal);
|
||
return node;
|
||
}
|
||
|
||
function walk(node, fn) {
|
||
fn(node);
|
||
for (const child of node.children) walk(child, fn);
|
||
}
|
||
|
||
function linkPath(parent, child) {
|
||
const x1 = parent.x + parent.w / 2;
|
||
const y1 = parent.y + parent.h;
|
||
const x2 = child.x + child.w / 2;
|
||
const y2 = child.y;
|
||
const mid = y1 + (y2 - y1) / 2;
|
||
if (Math.abs(x2 - x1) < 1) return `M${x1} ${y1}V${y2}`;
|
||
const r = 7;
|
||
const dir = x2 > x1 ? 1 : -1;
|
||
return `M${x1} ${y1}V${mid - r}Q${x1} ${mid} ${x1 + dir * r} ${mid}H${x2 - dir * r}Q${x2} ${mid} ${x2} ${mid + r}V${y2}`;
|
||
}
|
||
|
||
function nodeSvg(node) {
|
||
const chipsY = PAD_TOP + 21 + (node.note ? 14 : 0) + 3;
|
||
const nameY = PAD_TOP + (node.isRoot ? 17 : 15);
|
||
const parts = [];
|
||
|
||
parts.push(`<text class="oc-name" x="${node.w / 2}" y="${nameY}" text-anchor="middle" font-size="${node.nameSize}">${esc(node.name)}</text>`);
|
||
if (node.note) {
|
||
parts.push(`<text class="oc-note" x="${node.w / 2}" y="${PAD_TOP + 31}" text-anchor="middle" font-size="${NOTE}">${esc(node.note)}</text>`);
|
||
}
|
||
|
||
let y = chipsY;
|
||
for (const row of node.rows) {
|
||
let x = (node.w - row.w) / 2;
|
||
for (const chip of row.items) {
|
||
parts.push(`<g class="oc-chip"><rect x="${x.toFixed(1)}" y="${y}" width="${chip.w.toFixed(1)}" height="${CHIP_H}" rx="2"/><text x="${(x + chip.w / 2).toFixed(1)}" y="${y + 12}" text-anchor="middle" font-size="${CHIP}">${esc(chip.text)}</text></g>`);
|
||
x += chip.w + CHIP_GAP;
|
||
}
|
||
y += ROW_H;
|
||
}
|
||
|
||
const cls = node.isRoot ? 'oc-node oc-root' : `oc-node oc-${node.tone || 'plain'}`;
|
||
return `<g class="${cls}" transform="translate(${node.x.toFixed(1)} ${node.y.toFixed(1)})"><rect width="${node.w.toFixed(1)}" height="${node.h.toFixed(1)}" rx="3"/>${parts.join('')}</g>`;
|
||
}
|
||
|
||
function treeList(node) {
|
||
const cls = ['om-node', node.isRoot ? 'root' : '', node.tone || ''].filter(Boolean).join(' ');
|
||
const items = (node.items || []).length
|
||
? `<div class="om-items">${node.items.map((i) => `<span>${esc(i)}</span>`).join('')}</div>`
|
||
: '';
|
||
const kids = node.children.length ? `<ul>${node.children.map(treeList).join('')}</ul>` : '';
|
||
return `<li><div class="${cls}"><span class="om-name">${esc(node.name)}</span>${node.note ? `<span class="om-note">${esc(node.note)}</span>` : ''}${items}</div>${kids}</li>`;
|
||
}
|
||
|
||
export function chartHtml(chart) {
|
||
if (!chart?.root) return '';
|
||
|
||
const root = build(chart.root, 0, true);
|
||
const depthMax = [];
|
||
walk(root, (node) => {
|
||
depthMax[node.depth] = Math.max(depthMax[node.depth] || 0, node.h);
|
||
});
|
||
|
||
const rowY = [];
|
||
let y = CANVAS;
|
||
depthMax.forEach((h, depth) => {
|
||
rowY[depth] = y;
|
||
y += h + GAP_Y;
|
||
});
|
||
const height = Math.ceil(y - GAP_Y + CANVAS);
|
||
|
||
function place(node, left) {
|
||
node.x = left + (node.sw - node.w) / 2;
|
||
node.y = rowY[node.depth];
|
||
if (node.children.length) {
|
||
const total = node.children.reduce((sum, c) => sum + c.sw, 0) + GAP_X * (node.children.length - 1);
|
||
let cursor = left + (node.sw - total) / 2;
|
||
for (const child of node.children) {
|
||
place(child, cursor);
|
||
cursor += child.sw + GAP_X;
|
||
}
|
||
}
|
||
}
|
||
place(root, CANVAS);
|
||
|
||
const width = Math.ceil(root.sw + CANVAS * 2);
|
||
const nodes = [];
|
||
const links = [];
|
||
walk(root, (node) => {
|
||
nodes.push(nodeSvg(node));
|
||
for (const child of node.children) {
|
||
const dashed = child.dashed || child.tone === 'watch' || child.tone === 'local';
|
||
links.push(`<path class="oc-link${dashed ? ' dashed' : ''}" d="${linkPath(node, child)}"/>`);
|
||
}
|
||
});
|
||
|
||
return `
|
||
<div class="org-chart-panel">
|
||
<div class="org-chart-head">
|
||
<p class="oc-title">结构图<em>ORG CHART</em></p>
|
||
<div class="oc-legend" aria-label="图例">
|
||
<span><i class="lg-line"></i>统属</span>
|
||
<span><i class="lg-line dash"></i>监察 / 派出 / 地方</span>
|
||
<span><i class="lg-box core"></i>中枢</span>
|
||
<span><i class="lg-box watch"></i>监察</span>
|
||
<span><i class="lg-box local"></i>地方</span>
|
||
</div>
|
||
</div>
|
||
<div class="org-chart-view">
|
||
<div class="org-chart-scroll" tabindex="0" role="group" aria-label="可横向滚动的结构图">
|
||
<svg class="org-svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" role="img" aria-label="${esc(chart.caption || '政权组织架构图')}">
|
||
${links.join('')}
|
||
${nodes.join('')}
|
||
</svg>
|
||
</div>
|
||
<button class="oc-nav oc-nav-prev" type="button" aria-label="向左查看结构图">‹</button>
|
||
<button class="oc-nav oc-nav-next" type="button" aria-label="向右查看结构图">›</button>
|
||
</div>
|
||
<div class="org-mobile-tree"><ul>${treeList(root)}</ul></div>
|
||
<p class="org-chart-caption">${esc(chart.caption || '')}<span class="oc-hint">图为权力关系简示,不表示品级高低;具体职掌见下方逐层说明。</span><span class="oc-swipe">图较宽,共 ${root.children.length} 个分支;可按住左右拖动,或用两侧箭头查看全图。</span><span class="oc-mobile-hint">按隶属关系缩进;金底为中枢,绿底为监察,虚线框为地方。</span></p>
|
||
</div>`;
|
||
}
|