152 lines
15 KiB
HTML
152 lines
15 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="ru">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<title>Магистр — полная схема базы данных</title>
|
||
<style>
|
||
*{box-sizing:border-box}html,body{margin:0;height:100%;overflow:hidden}body{background:#f8fafc;color:#0f172a;font:13px/1.4 Inter,-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif;display:grid;grid-template-rows:auto minmax(0,1fr) 36px;grid-template-columns:minmax(0,1fr);height:100dvh}.sans{font-family:Inter,-apple-system,BlinkMacSystemFont,'Segoe UI',sans-serif}.mono,code{font-family:'Cascadia Mono','SFMono-Regular',Consolas,'Noto Sans Mono',monospace}button,input,select{font:inherit}button,select,input{border:1px solid #cbd5e1;background:#fdfefe;color:#0f172a;border-radius:5px;height:34px}button{cursor:pointer;padding:0 12px;white-space:nowrap}button:hover{background:#eeedff;border-color:#4f46e5}button:focus-visible,input:focus-visible,select:focus-visible,.entity:focus-visible{outline:2px solid #4f46e5;outline-offset:2px}.toolbar{display:flex;align-items:center;gap:20px;min-height:76px;padding:12px 20px;border-bottom:1px solid #cbd5e1;background:#fdfefe}.brand{display:flex;align-items:center;gap:12px;flex-shrink:0}.logo{width:32px;height:32px;background:#4f46e5;color:#f8fafc;border-radius:5px;display:grid;place-items:center;font-size:20px;font-weight:600}.brand h1{font-size:15px;letter-spacing:-.02em;margin:0;font-weight:600}.brand p{font-size:11px;color:#475569;margin:3px 0 0}.controls{display:flex;align-items:center;gap:6px;margin-left:auto}.zoom-value{min-width:50px;text-align:center;font-variant-numeric:tabular-nums;color:#475569}.search{width:250px;padding:0 10px}.workspace{height:auto;min-height:0;min-width:0;width:100%;position:relative;display:flex}.viewport{flex:1;min-width:0;overflow:auto;cursor:grab;overscroll-behavior:contain;background-color:#e8edf3;touch-action:pan-x pan-y}.viewport.dragging{cursor:grabbing;user-select:none}.canvas{position:relative;margin:20px;line-height:0;width:max-content;min-width:0}.canvas>svg{display:block;background:#f8fafc}.table-box{fill:#f8fafc;stroke:#475569;stroke-width:1.25}.entity{cursor:pointer;outline:none}.entity.selected .table-box,.entity:focus-visible .table-box{stroke:#4f46e5;stroke-width:3}.wire-under{fill:none;stroke:#f8fafc;stroke-width:5;stroke-linejoin:round}.wire{fill:none;stroke:#64748b;stroke-width:1.65;stroke-linejoin:round;pointer-events:none}.wire-hit{fill:none;stroke:transparent;stroke-width:10;cursor:crosshair}.relation.active .wire{stroke:#4f46e5;stroke-width:3}.relation.active .wire-under{stroke-width:7}.schema-focused .relation:not(.active){opacity:.16}.schema-focused .entity:not(.related):not(.selected){opacity:.38}.relation:hover .wire{stroke:#4f46e5;stroke-width:3}.inspector{width:350px;flex-shrink:0;background:#fdfefe;border-left:1px solid #cbd5e1;overflow:auto;padding:20px;line-height:1.6}.inspector[hidden]{display:none}.inspector h2{font-size:14px;overflow-wrap:anywhere;margin:0 0 4px}.inspector h3{font-size:12px;color:#475569;text-transform:uppercase;letter-spacing:.06em;margin:24px 0 12px}.inspector p{font-size:12px;color:#475569;margin:6px 0}.inspector .close{float:right;height:28px;margin:-6px -6px 8px 8px;padding:0 8px}.link-list{list-style:none;padding:0;margin:0}.link-list li{padding:10px 0;border-top:1px solid #e2e8f0}.link-list button{height:auto;text-align:left;border:0;border-radius:0;padding:0;background:transparent;color:#4338ca;display:block;overflow-wrap:anywhere;white-space:normal;font-size:11px}.link-list small{color:#475569;display:block;font-size:10px;padding-top:3px}.constraint{font-size:10px;overflow-wrap:anywhere;white-space:normal;color:#475569;display:block;padding:8px 0;border-top:1px solid #e2e8f0}.status{height:36px;display:flex;align-items:center;gap:16px;padding:0 20px;background:#fdfefe;border-top:1px solid #cbd5e1;font-size:11px;color:#475569}.status>span:first-child{flex:1;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.source{white-space:nowrap}.hint{position:absolute;bottom:20px;left:20px;pointer-events:none;padding:8px 12px;background:#fdfefe;border:1px solid #cbd5e1;border-radius:5px;color:#475569;font-size:11px;max-width:calc(100% - 40px)}.help{margin-left:6px}.fallback{padding:16px;background:#eeedff;color:#0f172a;font-size:13px}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0)}
|
||
@media(max-width:1100px){.toolbar{gap:10px;flex-wrap:wrap}.controls{flex-wrap:wrap}.brand{margin-right:auto}.search{width:210px}.workspace{height:auto}.toolbar{min-height:116px}.help{display:none}}@media(max-width:640px){.toolbar{padding:10px 12px;gap:8px}.brand{width:100%}.controls{margin-left:0;width:100%;gap:4px}.search{flex:1;min-width:130px;width:auto}.controls button{padding:0 8px}.controls .reset{display:none}.brand h1{font-size:14px}.workspace{height:auto}.inspector{position:absolute;right:0;top:0;bottom:0;width:min(350px,88vw)}.source{display:none}.status{padding:0 12px}.hint{left:12px;bottom:12px;font-size:10px}}
|
||
@media print{html,body{overflow:visible;height:auto}.toolbar,.inspector,.status,.hint{display:none}.workspace,.viewport{height:auto;overflow:visible;display:block}.canvas{margin:0;width:100%!important;height:auto!important}.canvas>svg{width:100%!important;height:auto!important}.schema-focused .relation,.schema-focused .entity{opacity:1!important}}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<header class="toolbar">
|
||
<div class="brand"><span class="logo" aria-hidden="true">М</span><div><h1>Магистр · Полная схема БД</h1><p>@@TABLE_COUNT@@ таблиц · @@COLUMN_COUNT@@ столбцов · @@FK_COUNT@@ внешних ключей</p></div></div>
|
||
<div class="controls">
|
||
<label for="search" class="sr-only">Найти таблицу</label><input id="search" class="search" type="search" list="table-names" placeholder="Найти таблицу…" autocomplete="off"><datalist id="table-names"></datalist>
|
||
<button id="zoom-out" aria-label="Уменьшить масштаб">−</button><output id="zoom-value" class="zoom-value" aria-live="polite">100%</output><button id="zoom-in" aria-label="Увеличить масштаб">+</button><button id="actual">100%</button><button id="fit">Вся схема</button><button id="clear" class="reset">Сбросить выделение</button>
|
||
</div>
|
||
</header>
|
||
<div class="workspace">
|
||
<main id="viewport" class="viewport" aria-label="Полотно полной схемы базы данных"><div id="canvas" class="canvas">@@SCHEMA@@</div></main>
|
||
<aside id="inspector" class="inspector" hidden aria-label="Выбранная таблица"></aside>
|
||
<div class="hint" id="hint">Перетаскивайте полотно · Ctrl + колесо — масштаб · нажмите таблицу для связей · двойной щелчок — приблизить</div>
|
||
</div>
|
||
<footer class="status"><span id="status" role="status">Все @@TABLE_COUNT@@ таблиц и @@FK_COUNT@@ связей на одном полотне. Стрелки: FK → PK/UQ. ● NOT NULL · ○ NULL</span><span class="source">V1__init.sql · @@SHA@@</span></footer>
|
||
<noscript><style>.viewport{height:90vh}.canvas{width:max-content}</style><p class="fallback">JavaScript выключен: полная статическая схема доступна с прокруткой.</p></noscript>
|
||
<script id="schema-data" type="application/json">@@DATA@@</script>
|
||
<script>
|
||
(() => {
|
||
'use strict';
|
||
const data = JSON.parse(document.getElementById('schema-data').textContent);
|
||
const svg = document.getElementById('schema');
|
||
const viewport = document.getElementById('viewport');
|
||
const canvas = document.getElementById('canvas');
|
||
const inspector = document.getElementById('inspector');
|
||
const status = document.getElementById('status');
|
||
const zoomValue = document.getElementById('zoom-value');
|
||
const search = document.getElementById('search');
|
||
const entities = [...svg.querySelectorAll('.entity')];
|
||
const relations = [...svg.querySelectorAll('.relation')];
|
||
let scale = 1, selected = null, dragging = null, moved = false;
|
||
const escape = value => String(value).replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
||
function zoom(next, x=viewport.clientWidth/2, y=viewport.clientHeight/2) {
|
||
next = Math.max(.06,Math.min(2.5,next));
|
||
const worldX = (viewport.scrollLeft+x-20)/scale;
|
||
const worldY = (viewport.scrollTop+y-20)/scale;
|
||
scale = next;
|
||
svg.style.width = `${data.width*scale}px`;
|
||
svg.style.height = `${data.height*scale}px`;
|
||
viewport.scrollLeft = worldX*scale-x+20;
|
||
viewport.scrollTop = worldY*scale-y+20;
|
||
zoomValue.value = `${Math.round(scale*100)}%`;
|
||
}
|
||
function fit() {
|
||
zoom(Math.min((viewport.clientWidth-40)/data.width,(viewport.clientHeight-40)/data.height));
|
||
viewport.scrollTo(0,0);
|
||
}
|
||
function highlight(name, edgeNumber=null) {
|
||
svg.classList.toggle('schema-focused',Boolean(name || edgeNumber));
|
||
const connected = new Set(name?[name]:[]);
|
||
relations.forEach(r=>{
|
||
const active = edgeNumber ? Number(r.dataset.number)===edgeNumber : name && (r.dataset.source===name || r.dataset.target===name);
|
||
r.classList.toggle('active',Boolean(active));
|
||
if(active){connected.add(r.dataset.source);connected.add(r.dataset.target);}
|
||
});
|
||
entities.forEach(e=>{e.classList.toggle('selected',e.dataset.table===name);e.classList.toggle('related',connected.has(e.dataset.table));});
|
||
}
|
||
function clear() {
|
||
selected = null;
|
||
inspector.hidden = true;
|
||
highlight(null);
|
||
status.textContent = 'Все @@TABLE_COUNT@@ таблиц и @@FK_COUNT@@ связей на одном полотне. Стрелки: FK → PK/UQ. ● NOT NULL · ○ NULL';
|
||
}
|
||
function focus(name) {
|
||
if(!data.tables[name])return;
|
||
select(name);
|
||
zoom(1);
|
||
const t = data.tables[name];
|
||
viewport.scrollLeft = t.x*scale-viewport.clientWidth/2+t.width*scale/2+20;
|
||
viewport.scrollTop = t.y*scale-60;
|
||
}
|
||
function edgeList(edges,direction) {
|
||
if(!edges.length)return '<p>Нет.</p>';
|
||
return '<ul class="link-list">'+edges.map(e=>`<li><button type="button" data-link="${escape(direction==='out'?e.target:e.source)}" data-edge="${e.number}"><code>FK${String(e.number).padStart(2,'0')} · ${escape(e.source)}.${escape(e.columns.join(', '))} → ${escape(e.target)}.${escape(e.keys.join(', '))}</code></button><small>ON DELETE ${escape(e.action)} · ${data.tables[e.source].columns[e.column].nn?'обязательная ссылка':'NULL допустим'}</small></li>`).join('')+'</ul>';
|
||
}
|
||
function select(name) {
|
||
selected = name;
|
||
highlight(name);
|
||
const table = data.tables[name];
|
||
const outgoing = data.edges.filter(e=>e.source===name);
|
||
const incoming = data.edges.filter(e=>e.target===name);
|
||
inspector.innerHTML = `<button class="close" type="button" aria-label="Закрыть сведения">×</button><h2>${escape(name)}</h2><p>${escape(table.label)}</p><p>${Object.keys(table.columns).length} столбцов · ${outgoing.length} исходящих FK · ${incoming.length} входящих FK</p><h3>Исходящие связи</h3>${edgeList(outgoing,'out')}<h3>Входящие связи</h3>${edgeList(incoming,'in')}<h3>Ограничения</h3>${table.constraints.length?table.constraints.map(c=>`<code class="constraint">${escape(c)}</code>`).join(''):'<p>Отдельные ограничения не объявлены.</p>'}<h3>Явные индексы</h3>${table.indexes.length?table.indexes.map(c=>`<code class="constraint">${escape(c)}</code>`).join(''):'<p>Нет. Индексы PK и UNIQUE создаются PostgreSQL автоматически.</p>'}`;
|
||
inspector.hidden = false;
|
||
inspector.querySelector('.close').addEventListener('click',clear);
|
||
inspector.querySelectorAll('[data-link]').forEach(button=>button.addEventListener('click',()=>focus(button.dataset.link)));
|
||
status.textContent = `${name} · ${outgoing.length} исходящих и ${incoming.length} входящих FK · все остальные таблицы остаются на полотне`;
|
||
}
|
||
entities.forEach(entity=>{
|
||
const name = entity.dataset.table;
|
||
entity.addEventListener('mouseenter',()=>{if(!selected && !dragging)highlight(name);});
|
||
entity.addEventListener('mouseleave',()=>{if(!selected)highlight(null);});
|
||
entity.addEventListener('click',()=>{if(!moved)select(name);});
|
||
entity.addEventListener('dblclick',()=>focus(name));
|
||
entity.addEventListener('keydown',e=>{if(e.key==='Enter'||e.key===' '){e.preventDefault();select(name);}});
|
||
});
|
||
relations.forEach(relation=>{
|
||
relation.addEventListener('mouseenter',()=>{
|
||
if(!selected)highlight(null,Number(relation.dataset.number));
|
||
const e = data.edges.find(e=>e.number===Number(relation.dataset.number));
|
||
status.textContent = `FK${String(e.number).padStart(2,'0')}: ${e.source}.${e.column} → ${e.target}.${e.key} · ON DELETE ${e.action}`;
|
||
});
|
||
relation.addEventListener('mouseleave',()=>{if(!selected){highlight(null);status.textContent='Все @@TABLE_COUNT@@ таблиц и @@FK_COUNT@@ связей на одном полотне. Стрелки: FK → PK/UQ. ● NOT NULL · ○ NULL';}});
|
||
});
|
||
document.getElementById('zoom-in').addEventListener('click',()=>zoom(scale*1.25));
|
||
document.getElementById('zoom-out').addEventListener('click',()=>zoom(scale/1.25));
|
||
document.getElementById('actual').addEventListener('click',()=>zoom(1));
|
||
document.getElementById('fit').addEventListener('click',()=>{clear();fit();});
|
||
document.getElementById('clear').addEventListener('click',clear);
|
||
viewport.addEventListener('wheel',e=>{
|
||
if(!e.ctrlKey&&!e.metaKey)return;
|
||
e.preventDefault();
|
||
const rect=viewport.getBoundingClientRect();
|
||
zoom(scale*Math.exp(-e.deltaY*.002),e.clientX-rect.left,e.clientY-rect.top);
|
||
},{passive:false});
|
||
viewport.addEventListener('pointerdown',e=>{
|
||
if(e.button!==0||e.target.closest('.entity'))return;
|
||
dragging={x:e.clientX,y:e.clientY,left:viewport.scrollLeft,top:viewport.scrollTop};
|
||
moved=false;
|
||
viewport.setPointerCapture(e.pointerId);
|
||
viewport.classList.add('dragging');
|
||
});
|
||
viewport.addEventListener('pointermove',e=>{
|
||
if(!dragging)return;
|
||
const dx=e.clientX-dragging.x,dy=e.clientY-dragging.y;
|
||
moved=moved||Math.abs(dx)+Math.abs(dy)>4;
|
||
viewport.scrollLeft=dragging.left-dx;viewport.scrollTop=dragging.top-dy;
|
||
});
|
||
function stop(){if(dragging&&!moved)clear();dragging=null;viewport.classList.remove('dragging');setTimeout(()=>{moved=false;},0);}
|
||
viewport.addEventListener('pointerup',stop);viewport.addEventListener('pointercancel',stop);
|
||
document.addEventListener('keydown',e=>{if(e.key==='Escape')clear();});
|
||
const names = document.getElementById('table-names');
|
||
Object.keys(data.tables).sort().forEach(name=>{const option=document.createElement('option');option.value=name;option.label=data.tables[name].label;names.append(option);});
|
||
function searchTable(){const term=search.value.trim().toLowerCase();const name=data.tables[term]?term:Object.keys(data.tables).find(n=>n.includes(term)||data.tables[n].label.toLowerCase().includes(term));if(term&&name)focus(name);}
|
||
search.addEventListener('change',searchTable);search.addEventListener('keydown',e=>{if(e.key==='Enter')searchTable();});
|
||
requestAnimationFrame(fit);
|
||
window.addEventListener('resize',()=>{if(scale<.3)fit();});
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|