/* EunomiaGraph: readable, local, framework-free SVG legal graph. */ (function (global) { 'use strict'; const SVG_NS = 'http://www.w3.org/2000/svg'; const colors = { document: '#29465f', fragment: '#587c9b', code: '#29465f', plenum: '#52728d', review: '#52728d', 'federal-law': '#3f6685', bylaw: '#617d92', thematic: '#a36d67', edge: '#b9c7d0' }; const typeNames = { article: 'Статья', fragment: 'Фрагмент', point: 'Пункт', document: 'Документ', code: 'Кодекс', 'federal-law': 'Федеральный закон', bylaw: 'Подзаконный акт', plenum: 'Пленум', review: 'Обзор' }; const el = (name, attrs = {}) => { const node = document.createElementNS(SVG_NS, name); Object.entries(attrs).forEach(([key, value]) => node.setAttribute(key, value)); return node; }; const label = node => { const raw = String(node.label || node.id || 'Без названия'); if (node.kind === 'point') { const found = raw.match(/Пункт\s*\d+/i); return found ? found[0] : 'Пункт'; } return raw; }; const relation = edge => String(edge.relation || edge.kind || 'direct'); const wrap = (node, value, max = 28) => { const words = String(value).split(/\s+/); let line = ''; let row = 0; for (const word of words) { if ((line + ' ' + word).trim().length > max && line) { const t = el('tspan', { x: -115, y: -20 + row * 19 }); t.textContent = line; node.append(t); line = word; row += 1; if (row === 2) break; } else line = (line + ' ' + word).trim(); } if (row === 2) line = `${line.slice(0, max - 1)}…`; if (line) { const t = el('tspan', { x: -115, y: -20 + row * 19 }); t.textContent = line; node.append(t); } }; function mount(container, data, options = {}) { const callbacks = { onSelect() {}, onOpen() {}, onExplore() {}, ...options }; const svg = el('svg', { viewBox: data?.focus_id ? '0 0 900 760' : '0 0 1200 760', role: 'application', tabindex: '0', 'aria-label': 'Граф правовых связей', style: 'background:#fdfdfb' }); const viewport = el('g'); const edgesLayer = el('g'); const nodesLayer = el('g'); viewport.append(edgesLayer, nodesLayer); svg.append(viewport); container.replaceChildren(svg); const allNodes = Array.isArray(data?.nodes) ? data.nodes.map(node => ({ ...node })) : []; const allEdges = Array.isArray(data?.edges) ? data.edges : []; const byId = id => allNodes.find(node => node.id === id); let scale = 1; let offsetX = 0; let offsetY = 0; let dragStart = null; let moved = false; const focusId = data?.focus_id; const visible = () => { if (!focusId) return allNodes.slice(0, 8); const ids = new Set([focusId]); allEdges.forEach(edge => { const a = edge.from || edge.source; const b = edge.to || edge.target; if (a === focusId) ids.add(b); if (b === focusId) ids.add(a); }); return [...ids].map(byId).filter(Boolean).slice(0, 8); }; function layout(nodes) { if (focusId) { const center = nodes.find(node => node.id === focusId); if (center) { center.x = 140; center.y = 380; } const others = nodes.filter(node => node !== center); others.forEach((node, index) => { node.x = 480 + (index % 2) * 285; node.y = 125 + Math.floor(index / 2) * 125; }); return; } const groups = ['code', 'federal-law', 'bylaw', 'plenum', 'review']; const counters = Object.fromEntries(groups.map(group => [group, 0])); nodes.forEach(node => { const group = groups.includes(node.document_kind || node.kind) ? (node.document_kind || node.kind) : 'bylaw'; const index = counters[group]++; const column = groups.indexOf(group); node.x = 115 + column * 240; node.y = 115 + index * 105; node.group = group; }); } function render() { const nodes = visible(); layout(nodes); edgesLayer.replaceChildren(); nodesLayer.replaceChildren(); const ids = new Set(nodes.map(node => node.id)); if (!focusId) ['code', 'federal-law', 'bylaw', 'plenum', 'review'].forEach((group, index) => { const title = el('text', { x: 115 + index * 240, y: 40, 'text-anchor': 'middle', fill: '#29465f', 'font-size': 14, 'font-family': 'ui-sans-serif, sans-serif', 'font-weight': 700 }); title.textContent = typeNames[group]; nodesLayer.append(title); }); allEdges.forEach(edge => { const a = byId(edge.from || edge.source), b = byId(edge.to || edge.target); if (!a || !b || !ids.has(a.id) || !ids.has(b.id)) return; edgesLayer.append(el('line', { x1: a.x, y1: a.y, x2: b.x, y2: b.y, stroke: relation(edge) === 'thematic' ? colors.thematic : colors.edge, 'stroke-width': relation(edge) === 'thematic' ? 2 : 1.5, 'stroke-dasharray': relation(edge) === 'thematic' ? '6 5' : '' })); }); nodes.forEach(node => { const group = el('g', { transform: `translate(${node.x} ${node.y})`, tabindex: 0, role: 'button', 'aria-label': label(node) }); group.dataset.node = node.id; const rect = el('rect', { x: -125, y: -42, width: 250, height: 84, rx: 7, fill: '#fdfdfb', stroke: node.needs_review ? colors.thematic : colors[node.document_kind || node.kind] || colors.document, 'stroke-width': node.id === focusId ? 3 : 1.5 }); group.append(rect); const title = el('text', { fill: '#26343f', 'font-size': 16, 'font-family': 'ui-sans-serif, sans-serif', 'font-weight': 600 }); wrap(title, label(node)); group.append(title); const meta = el('text', { x: -115, y: 29, fill: '#587080', 'font-size': 14, 'font-family': 'ui-sans-serif, sans-serif' }); meta.textContent = typeNames[node.kind] || typeNames[node.document_kind] || 'Документ'; group.append(meta); group.onclick = event => { event.stopPropagation(); if (moved) return; highlight(node.id); callbacks.onSelect(node, allEdges.filter(edge => (edge.from || edge.source) === node.id || (edge.to || edge.target) === node.id)); }; group.ondblclick = event => { event.stopPropagation(); callbacks.onExplore(node); }; group.onkeydown = event => { if (event.key === 'Enter' || event.key === ' ') { event.preventDefault(); callbacks.onSelect(node, allEdges.filter(edge => (edge.from || edge.source) === node.id || (edge.to || edge.target) === node.id)); } }; nodesLayer.append(group); }); transform(); } function highlight(id) { const neighbours = new Set([id]); allEdges.forEach(edge => { const a = edge.from || edge.source; const b = edge.to || edge.target; if (a === id) neighbours.add(b); if (b === id) neighbours.add(a); }); nodesLayer.querySelectorAll('[data-node]').forEach(node => node.setAttribute('opacity', neighbours.has(node.dataset.node) ? 1 : .28)); } function transform() { viewport.setAttribute('transform', `translate(${offsetX} ${offsetY}) scale(${scale})`); } function point(event) { const p = svg.createSVGPoint(); p.x = event.clientX; p.y = event.clientY; return p.matrixTransform(svg.getScreenCTM().inverse()); } svg.onwheel = event => { event.preventDefault(); scale = Math.max(.45, Math.min(3.2, scale * (event.deltaY < 0 ? 1.1 : .9))); transform(); }; svg.onpointerdown = event => { if (event.target.closest('[data-node]')) return; dragStart = point(event); moved = false; svg.setPointerCapture(event.pointerId); }; svg.onpointermove = event => { if (!dragStart) return; const next = point(event); const dx = next.x - dragStart.x; const dy = next.y - dragStart.y; if (Math.abs(dx) + Math.abs(dy) > 3) moved = true; offsetX += dx; offsetY += dy; dragStart = next; transform(); }; svg.onpointerup = () => { dragStart = null; setTimeout(() => { moved = false; }, 0); }; const controller = { zoom(factor) { scale = Math.max(.45, Math.min(3.2, scale * factor)); transform(); }, reset() { scale = 1; offsetX = 0; offsetY = 0; transform(); }, destroy() { svg.remove(); } }; render(); return controller; } global.EunomiaGraph = { mount }; })(window);