-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagram.html
More file actions
616 lines (522 loc) · 30.8 KB
/
Copy pathdiagram.html
File metadata and controls
616 lines (522 loc) · 30.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Architecture Canvas</title>
<script src="https://cdn.tailwindcss.com"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;900&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
margin: 0;
overflow: hidden;
background-color: #f8fafc; /* slate-50 */
user-select: none;
-webkit-user-select: none;
}
#canvas-container {
position: absolute;
inset: 0;
cursor: grab;
overflow: hidden;
}
#canvas-container:active {
cursor: grabbing;
}
#grid-background {
position: absolute;
inset: 0;
z-index: 0;
pointer-events: none;
will-change: background-position, background-size;
}
#transform-layer {
position: absolute;
inset: 0;
transform-origin: 0 0;
will-change: transform;
z-index: 1;
}
.node-element {
position: absolute;
width: 180px;
height: 76px;
border-width: 2px;
border-style: solid;
border-radius: 0.75rem;
padding: 0.75rem;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
transition: box-shadow 0.2s ease-out, border-color 0.2s ease-out, scale 0.2s ease-out;
will-change: left, top, transform;
z-index: 10;
}
.node-element:hover {
transform: scale(1.05);
z-index: 15;
}
.node-element.dragging {
transform: scale(1.05);
z-index: 20 !important;
transition: none; /* Disable transition during drag for smoothness */
}
.node-element.selected {
box-shadow: 0 0 0 4px #60a5fa, 0 10px 15px -3px rgb(0 0 0 / 0.1);
border-color: #3b82f6;
transform: scale(1.05);
z-index: 20;
}
#svg-layer {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: visible;
}
#sidebar {
position: absolute;
right: 0;
top: 0;
bottom: 0;
width: 20rem;
background-color: white;
border-left: 1px solid #e2e8f0;
box-shadow: -10px 0 15px -3px rgb(0 0 0 / 0.1);
transform: translateX(100%);
transition: transform 0.3s ease-in-out;
z-index: 40;
display: flex;
flex-direction: column;
}
#sidebar.open {
transform: translateX(0);
}
</style>
</head>
<body>
<div id="app" class="flex h-screen w-full relative">
<!-- Main Canvas Area -->
<div id="canvas-container">
<div id="grid-background"></div>
<div id="transform-layer">
<!-- SVG Layer for Edges -->
<svg id="svg-layer">
<defs>
<marker id="arrowhead" markerWidth="8" markerHeight="6" refX="18" refY="3" orient="auto">
<polygon points="0 0, 8 3, 0 6" fill="#cbd5e1" />
</marker>
</defs>
<g id="edges-group"></g>
</svg>
<!-- HTML Layer for Nodes -->
<div id="nodes-group"></div>
</div>
</div>
<!-- Floating Zoom Controls -->
<div class="absolute bottom-6 left-6 flex bg-white rounded-lg shadow-md border border-slate-200 p-1 z-30">
<button id="btn-zoom-in" class="p-2 hover:bg-slate-100 rounded text-slate-600 transition-colors" title="Zoom In">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line><line x1="11" y1="8" x2="11" y2="14"></line><line x1="8" y1="11" x2="14" y2="11"></line></svg>
</button>
<button id="btn-zoom-out" class="p-2 hover:bg-slate-100 rounded text-slate-600 transition-colors" title="Zoom Out">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line><line x1="8" y1="11" x2="14" y2="11"></line></svg>
</button>
<div class="w-px bg-slate-200 mx-1 my-2"></div>
<button id="btn-fit-view" class="p-2 hover:bg-slate-100 rounded text-slate-600 transition-colors" title="Fit to Screen">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M8 3H5a2 2 0 0 0-2 2v3"></path><path d="M21 8V5a2 2 0 0 0-2-2h-3"></path><path d="M3 16v3a2 2 0 0 0 2 2h3"></path><path d="M16 21h3a2 2 0 0 0 2-2v-3"></path></svg>
</button>
</div>
<!-- Detail Sidebar -->
<div id="sidebar">
<div class="flex items-center justify-between p-4 border-b border-slate-100">
<h2 class="font-bold text-lg text-slate-800 flex items-center gap-2">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-blue-500"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="16" x2="12" y2="12"></line><line x1="12" y1="8" x2="12.01" y2="8"></line></svg>
Node Details
</h2>
<button id="btn-close-sidebar" class="p-1 hover:bg-slate-100 rounded-full text-slate-400 hover:text-slate-600 transition-colors">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
</button>
</div>
<div class="p-6 flex-1 overflow-y-auto" id="sidebar-content">
<!-- Content injected via JS -->
</div>
</div>
</div>
<script>
const NODE_WIDTH = 180;
const NODE_HEIGHT = 76;
// SVG Icons
const icons = {
client: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="2" y="3" width="20" height="14" rx="2" ry="2"></rect><line x1="8" y1="21" x2="16" y2="21"></line><line x1="12" y1="17" x2="12" y2="21"></line></svg>`,
frontend: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><line x1="3" y1="9" x2="21" y2="9"></line><line x1="9" y1="21" x2="9" y2="9"></line></svg>`,
gateway: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" y1="3" x2="14" y2="10"></line><line x1="3" y1="21" x2="10" y2="14"></line></svg>`,
service: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"></path><polyline points="3.27 6.96 12 12.01 20.73 6.96"></polyline><line x1="12" y1="22.08" x2="12" y2="12"></line></svg>`,
worker: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2v20M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6"></path></svg>`,
database: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><ellipse cx="12" cy="5" rx="9" ry="3"></ellipse><path d="M21 12c0 1.66-4 3-9 3s-9-1.34-9-3"></path><path d="M3 5v14c0 1.66 4 3 9 3s9-1.34 9-3V5"></path></svg>`,
external: `<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17.5 19H9a7 7 0 1 1 6.71-9h1.79a4.5 4.5 0 1 1 0 9Z"></path></svg>`
};
const typeConfig = {
client: { icon: icons.client, classes: 'bg-white border-slate-300 text-slate-700 shadow-sm hover:border-slate-400' },
frontend: { icon: icons.frontend, classes: 'bg-blue-50 border-blue-400 text-blue-900 shadow-blue-100/50 hover:border-blue-500' },
gateway: { icon: icons.gateway, classes: 'bg-purple-50 border-purple-400 text-purple-900 shadow-purple-100/50 hover:border-purple-500' },
service: { icon: icons.service, classes: 'bg-emerald-50 border-emerald-400 text-emerald-900 shadow-emerald-100/50 hover:border-emerald-500' },
worker: { icon: icons.worker, classes: 'bg-amber-50 border-amber-400 text-amber-900 shadow-amber-100/50 hover:border-amber-500' },
database: { icon: icons.database, classes: 'bg-indigo-50 border-indigo-400 text-indigo-900 shadow-indigo-100/50 hover:border-indigo-500' },
external: { icon: icons.external, classes: 'bg-rose-50 border-rose-400 text-rose-900 shadow-rose-100/50 hover:border-rose-500' }
};
const nodesData = [
// Top — browser stack, centred on x=650
{ id: 'browser', label: 'Browser', sub: 'Client', x: 560, y: 40, type: 'client', desc: 'End-user web browser interacting with the application.' },
{ id: 'web', label: 'Web', sub: ':3000 • React SPA', x: 560, y: 160, type: 'frontend', desc: 'Main frontend single-page application built in React.' },
{ id: 'bff', label: 'BFF', sub: ':8090 • Auth • Proxy', x: 560, y: 290, type: 'gateway', desc: 'Backend-for-Frontend serving as the entry point. Handles authentication, CORS, and reverse proxy routing.' },
// MCP to the right of BFF
{ id: 'mcp', label: 'MCP', sub: 'IDE / Agent', x: 980, y: 290, type: 'client', desc: 'Model Context Protocol or Machine Control client interacting with the AI subsystem via stdio or HTTP.' },
// Services row — 5 nodes, 200 px apart, centred under BFF
// ConfigService and FileService were both merged into Data (typed config +
// PDF/Excel/CSV/JSON/sitemap export).
// span = 4 × 200 = 800 → start at 650 − 400 = 250... (kept fastapi anchor at 50)
{ id: 'fastapi', label: 'FastAPI', sub: ':8096 • Crawl/Jobs', x: 50, y: 470, type: 'service', desc: 'Core Python service handling crawling and jobs. Acts as the strangler proxy target.' },
{ id: 'report', label: 'Report', sub: ':8094 • Build', x: 250, y: 470, type: 'service', desc: 'Service dedicated to report building and orchestrating complex multi-step processes.' },
{ id: 'integrations', label: 'Integrations', sub: ':8093 • APIs', x: 450, y: 470, type: 'service', desc: 'Manages third-party connections including Google/Bing APIs, GSC, GA4, and keyword processing.' },
{ id: 'ai', label: 'AI', sub: ':8092 • Chat/LLM', x: 650, y: 470, type: 'service', desc: 'AI orchestration service managing chat interfaces, LLM connections, secrets, and MCP.' },
{ id: 'data', label: 'Data', sub: ':8091 • Portfolio · Config · Export', x: 850, y: 470, type: 'service', desc: 'Data access layer for report reads, portfolio management, system issue tracking, typed configuration (pipeline settings, UI/client preferences), and PDF/Excel/CSV/JSON/sitemap report export — absorbed the former ConfigService and FileService.' },
// Middle-lower row — Worker under FastAPI/Report, external services under Integrations/AI
{ id: 'worker', label: 'Worker', sub: 'Crawl + Lighthouse', x: 150, y: 660, type: 'worker', desc: 'Background asynchronous pipeline worker executing heavy crawl and Lighthouse audit jobs.' },
{ id: 'google', label: 'Google / Bing APIs', sub: 'External', x: 450, y: 660, type: 'external', desc: 'External search and analytics APIs consumed by the Integrations service.' },
{ id: 'llm', label: 'LLM', sub: 'Ollama', x: 650, y: 660, type: 'external', desc: 'Large Language Model provider, heavily utilized for text and chat generation.' },
// Bottom — Postgres centred
{ id: 'postgres', label: 'Postgres', sub: 'Audit • Settings', x: 560, y: 860, type: 'database', desc: 'Primary relational data store holding audit data and typed system settings.' },
];
const edgesData = [
{ id: 'e1', source: 'browser', target: 'web' },
{ id: 'e2', source: 'web', target: 'bff', label: '"/api/*"' },
{ id: 'e3', source: 'bff', target: 'fastapi' },
{ id: 'e4', source: 'bff', target: 'report' },
{ id: 'e5', source: 'bff', target: 'integrations' },
{ id: 'e6', source: 'bff', target: 'ai' },
{ id: 'e7', source: 'bff', target: 'data' },
{ id: 'e9', source: 'worker', target: 'report', label: 'REPORT_SERVICE_URL' },
{ id: 'e10', source: 'worker', target: 'integrations', label: 'INTEGRATIONS_SERVICE_URL' },
{ id: 'e11', source: 'worker', target: 'ai', label: 'AI_SERVICE_URL' },
{ id: 'e12', source: 'report', target: 'fastapi', label: 'Python bridge', dashed: true },
{ id: 'e13', source: 'report', target: 'integrations', label: 'GET enrichment', dashed: true },
{ id: 'e14', source: 'integrations', target: 'fastapi', label: 'keyword/GSC bridge', dashed: true },
{ id: 'e15', source: 'ai', target: 'fastapi', label: 'audit-tool bridge', dashed: true },
{ id: 'e16', source: 'data', target: 'fastapi', label: 'branding lookup', dashed: true },
{ id: 'e17', source: 'fastapi', target: 'postgres' },
{ id: 'e18', source: 'worker', target: 'postgres' },
{ id: 'e19', source: 'report', target: 'postgres' },
{ id: 'e20', source: 'integrations', target: 'postgres' },
{ id: 'e21', source: 'ai', target: 'postgres' },
{ id: 'e22', source: 'data', target: 'postgres' },
{ id: 'e23', source: 'integrations', target: 'google' },
{ id: 'e24', source: 'ai', target: 'llm' },
{ id: 'e25', source: 'mcp', target: 'ai', label: 'MCP stdio/HTTP', dashed: true },
];
let transform = { x: 0, y: 0, scale: 0.8 };
let isPanning = false;
let isDraggingNode = false;
let draggedNode = null;
let selectedNode = null;
let lastMousePos = null;
const domRefs = {
canvasContainer: document.getElementById('canvas-container'),
transformLayer: document.getElementById('transform-layer'),
gridBackground: document.getElementById('grid-background'),
nodesGroup: document.getElementById('nodes-group'),
edgesGroup: document.getElementById('edges-group'),
sidebar: document.getElementById('sidebar'),
sidebarContent: document.getElementById('sidebar-content')
};
// Cache DOM elements in objects for fast updates
const nodeElements = {};
const edgeElements = {};
function initNodes() {
nodesData.forEach(node => {
const config = typeConfig[node.type];
const el = document.createElement('div');
el.className = `node-element ${config.classes}`;
el.style.left = `${node.x}px`;
el.style.top = `${node.y}px`;
el.id = `node-${node.id}`;
el.innerHTML = `
<div class="flex items-center gap-2 mb-1 w-full justify-center">
<div class="shrink-0 text-current">${config.icon}</div>
<span class="font-bold text-sm truncate">${node.label}</span>
</div>
<span class="text-xs opacity-80 font-medium truncate w-full text-center">
${node.sub}
</span>
`;
// Node Events
el.addEventListener('pointerdown', (e) => {
e.stopPropagation();
isDraggingNode = true;
draggedNode = node;
lastMousePos = { x: e.clientX, y: e.clientY };
el.classList.add('dragging');
el.setPointerCapture(e.pointerId);
});
el.addEventListener('click', (e) => {
e.stopPropagation();
selectNode(node);
});
domRefs.nodesGroup.appendChild(el);
nodeElements[node.id] = el;
});
}
function getEdgePath(sourceNode, targetNode) {
const sx = sourceNode.x + NODE_WIDTH / 2;
const sy = sourceNode.y + NODE_HEIGHT / 2;
const tx = targetNode.x + NODE_WIDTH / 2;
const ty = targetNode.y + NODE_HEIGHT / 2;
const isVertical = Math.abs(ty - sy) > Math.abs(tx - sx);
const controlOffset = isVertical ? Math.abs(ty - sy) / 3 : Math.abs(tx - sx) / 3;
if (isVertical) {
return `M ${sx} ${sy} C ${sx} ${sy + controlOffset}, ${tx} ${ty - controlOffset}, ${tx} ${ty}`;
} else {
return `M ${sx} ${sy} C ${sx + controlOffset} ${sy}, ${tx - controlOffset} ${ty}, ${tx} ${ty}`;
}
}
function initEdges() {
edgesData.forEach(edge => {
const sourceNode = nodesData.find(n => n.id === edge.source);
const targetNode = nodesData.find(n => n.id === edge.target);
if (!sourceNode || !targetNode) return;
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.id = `edge-${edge.id}`;
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("fill", "none");
path.setAttribute("stroke", edge.dashed ? "#94a3b8" : "#cbd5e1");
path.setAttribute("stroke-width", "2");
if (edge.dashed) path.setAttribute("stroke-dasharray", "5,5");
path.setAttribute("marker-end", "url(#arrowhead)");
path.setAttribute("d", getEdgePath(sourceNode, targetNode));
g.appendChild(path);
let labelGroup = null;
if (edge.label) {
labelGroup = document.createElementNS("http://www.w3.org/2000/svg", "g");
const rect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
rect.setAttribute("x", "-60");
rect.setAttribute("y", "-12");
rect.setAttribute("width", "120");
rect.setAttribute("height", "24");
rect.setAttribute("rx", "4");
rect.setAttribute("fill", "white");
rect.setAttribute("fill-opacity", "0.8");
const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
text.setAttribute("text-anchor", "middle");
text.setAttribute("alignment-baseline", "middle");
text.setAttribute("font-size", "10");
text.setAttribute("fill", "#64748b");
text.setAttribute("class", "font-medium");
text.textContent = edge.label;
text.style.fontFamily = "Inter, sans-serif";
labelGroup.appendChild(rect);
labelGroup.appendChild(text);
const sx = sourceNode.x + NODE_WIDTH / 2;
const sy = sourceNode.y + NODE_HEIGHT / 2;
const tx = targetNode.x + NODE_WIDTH / 2;
const ty = targetNode.y + NODE_HEIGHT / 2;
labelGroup.setAttribute("transform", `translate(${(sx+tx)/2}, ${(sy+ty)/2})`);
g.appendChild(labelGroup);
}
domRefs.edgesGroup.appendChild(g);
edgeElements[edge.id] = { path, labelGroup, sourceNode, targetNode };
});
}
function updateEdges() {
// Only update edges connected to the currently dragged node to optimize performance
if (!draggedNode) return;
edgesData.forEach(edge => {
if (edge.source === draggedNode.id || edge.target === draggedNode.id) {
const els = edgeElements[edge.id];
els.path.setAttribute("d", getEdgePath(els.sourceNode, els.targetNode));
if (els.labelGroup) {
const sx = els.sourceNode.x + NODE_WIDTH / 2;
const sy = els.sourceNode.y + NODE_HEIGHT / 2;
const tx = els.targetNode.x + NODE_WIDTH / 2;
const ty = els.targetNode.y + NODE_HEIGHT / 2;
els.labelGroup.setAttribute("transform", `translate(${(sx+tx)/2}, ${(sy+ty)/2})`);
}
}
});
}
function updateTransform() {
domRefs.transformLayer.style.transform = `translate(${transform.x}px, ${transform.y}px) scale(${transform.scale})`;
domRefs.gridBackground.style.backgroundSize = `${20 * transform.scale}px ${20 * transform.scale}px`;
domRefs.gridBackground.style.backgroundImage = `radial-gradient(circle, #cbd5e1 1.5px, transparent 1.5px)`;
domRefs.gridBackground.style.backgroundPosition = `${transform.x}px ${transform.y}px`;
}
function centerView() {
const rect = domRefs.canvasContainer.getBoundingClientRect();
transform = {
x: (rect.width - 1200 * 0.8) / 2,
y: (rect.height - 900 * 0.8) / 2,
scale: 0.8
};
updateTransform();
}
function fitView() {
const rect = domRefs.canvasContainer.getBoundingClientRect();
transform = {
x: (rect.width - 1200 * 0.7) / 2,
y: (rect.height - 900 * 0.7) / 2,
scale: 0.7
};
updateTransform();
}
domRefs.canvasContainer.addEventListener('pointerdown', (e) => {
if (e.target.closest('.node-element') || e.target.closest('#sidebar') || e.target.closest('button')) return;
isPanning = true;
lastMousePos = { x: e.clientX, y: e.clientY };
domRefs.canvasContainer.setPointerCapture(e.pointerId);
// Deselect node on background click
if (selectedNode) {
selectNode(null);
}
});
window.addEventListener('pointermove', (e) => {
if (!lastMousePos) return;
const dx = e.clientX - lastMousePos.x;
const dy = e.clientY - lastMousePos.y;
if (isPanning) {
transform.x += dx;
transform.y += dy;
updateTransform();
} else if (isDraggingNode && draggedNode) {
draggedNode.x += dx / transform.scale;
draggedNode.y += dy / transform.scale;
const nodeEl = nodeElements[draggedNode.id];
nodeEl.style.left = `${draggedNode.x}px`;
nodeEl.style.top = `${draggedNode.y}px`;
updateEdges();
}
lastMousePos = { x: e.clientX, y: e.clientY };
});
window.addEventListener('pointerup', (e) => {
if (isPanning) {
domRefs.canvasContainer.releasePointerCapture(e.pointerId);
isPanning = false;
}
if (isDraggingNode) {
if (draggedNode && nodeElements[draggedNode.id]) {
nodeElements[draggedNode.id].classList.remove('dragging');
}
isDraggingNode = false;
draggedNode = null;
}
lastMousePos = null;
});
// Prevent context menu to allow smooth right-click panning if desired in future
domRefs.canvasContainer.addEventListener('contextmenu', e => e.preventDefault());
domRefs.canvasContainer.addEventListener('wheel', (e) => {
e.preventDefault();
const scaleDelta = -e.deltaY * 0.001;
const newScale = Math.min(Math.max(0.2, transform.scale + scaleDelta), 2.5);
const rect = domRefs.canvasContainer.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const xs = (mouseX - transform.x) / transform.scale;
const ys = (mouseY - transform.y) / transform.scale;
transform.scale = newScale;
transform.x = mouseX - xs * newScale;
transform.y = mouseY - ys * newScale;
updateTransform();
}, { passive: false });
function selectNode(node) {
// Remove previous selection styling
if (selectedNode && nodeElements[selectedNode.id]) {
nodeElements[selectedNode.id].classList.remove('selected');
}
selectedNode = node;
if (node) {
nodeElements[node.id].classList.add('selected');
renderSidebarContent();
domRefs.sidebar.classList.add('open');
} else {
domRefs.sidebar.classList.remove('open');
}
}
function renderSidebarContent() {
if (!selectedNode) return;
const config = typeConfig[selectedNode.type];
const iconColorClass = config.classes.split(' ')[0]; // Extracting background color class for icon box
const outgoingEdges = edgesData.filter(e => e.source === selectedNode.id);
let connectionsHtml = outgoingEdges.length > 0
? outgoingEdges.map(e => {
const targetNode = nodesData.find(n => n.id === e.target);
return `
<li class="text-sm flex items-center gap-2 text-slate-600">
<div class="w-1.5 h-1.5 rounded-full bg-blue-400"></div>
${targetNode ? targetNode.label : 'Unknown'}
${e.label ? `<span class="text-xs text-slate-400 italic">(${e.label})</span>` : ''}
</li>
`;
}).join('')
: `<li class="text-sm text-slate-400 italic">No outgoing connections</li>`;
domRefs.sidebarContent.innerHTML = `
<div class="flex items-center gap-3 mb-6">
<div class="p-3 rounded-lg ${iconColorClass} text-current">
${config.icon.replace('<svg', '<svg width="24" height="24" class="opacity-80"')}
</div>
<div>
<h3 class="font-black text-xl text-slate-900">${selectedNode.label}</h3>
<span class="text-xs font-medium text-slate-500 bg-slate-100 px-2 py-0.5 rounded-full uppercase tracking-wide">
${selectedNode.type}
</span>
</div>
</div>
<div class="space-y-4">
<div>
<label class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-1 block">Component</label>
<p class="text-slate-800 font-medium bg-slate-50 p-2 rounded border border-slate-100">
${selectedNode.sub}
</p>
</div>
<div>
<label class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-1 block">Description</label>
<p class="text-sm text-slate-600 leading-relaxed bg-slate-50 p-3 rounded-lg border border-slate-100 shadow-inner">
${selectedNode.desc}
</p>
</div>
<div class="pt-4 border-t border-slate-100">
<label class="text-xs font-bold text-slate-400 uppercase tracking-wider mb-2 block">Connected To</label>
<ul class="space-y-2">
${connectionsHtml}
</ul>
</div>
</div>
`;
}
document.getElementById('btn-close-sidebar').addEventListener('click', () => selectNode(null));
document.getElementById('btn-zoom-in').addEventListener('click', () => {
transform.scale = Math.min(transform.scale + 0.2, 2.5);
updateTransform();
});
document.getElementById('btn-zoom-out').addEventListener('click', () => {
transform.scale = Math.max(transform.scale - 0.2, 0.2);
updateTransform();
});
document.getElementById('btn-fit-view').addEventListener('click', fitView);
// Initialization
window.addEventListener('load', () => {
initNodes();
initEdges();
centerView();
});
// Handle window resize
window.addEventListener('resize', () => {
// Keep bounds somewhat sane on resize
if (!isPanning && !isDraggingNode) {
updateTransform();
}
});
</script>
</body>
</html>