-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
409 lines (387 loc) · 15.1 KB
/
Copy pathindex.html
File metadata and controls
409 lines (387 loc) · 15.1 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Graph Sparrow — Interactive Graph Explorer</title>
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
<link rel="alternate icon" href="favicon.png" />
<script>
(function () {
var saved = localStorage.getItem("theme");
var theme =
saved ||
(window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light");
document.documentElement.setAttribute("data-theme", theme);
})();
</script>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;600;700&family=JetBrains+Mono:wght@400;600&family=Space+Grotesk:wght@500;700&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<header>
<div class="brand">
<div class="brand-logo">
<svg viewBox="0 0 24 24" width="22" height="22" fill="none">
<!-- Edges -->
<line
x1="6"
y1="6"
x2="12"
y2="12"
stroke="#0F172A"
stroke-width="1.8"
stroke-linecap="round"
/>
<line
x1="18"
y1="6"
x2="12"
y2="12"
stroke="#0F172A"
stroke-width="1.8"
stroke-linecap="round"
/>
<line
x1="12"
y1="12"
x2="7"
y2="18"
stroke="#0F172A"
stroke-width="1.8"
stroke-linecap="round"
/>
<line
x1="12"
y1="12"
x2="17"
y2="18"
stroke="#0F172A"
stroke-width="1.8"
stroke-linecap="round"
/>
<line
x1="6"
y1="6"
x2="18"
y2="6"
stroke="#0F172A"
stroke-width="1.6"
stroke-dasharray="2,1.5"
/>
<line
x1="7"
y1="18"
x2="17"
y2="18"
stroke="#0F172A"
stroke-width="1.8"
stroke-linecap="round"
/>
<!-- Nodes -->
<circle cx="6" cy="6" r="2.2" fill="#0F172A" />
<circle cx="18" cy="6" r="2.2" fill="#0F172A" />
<circle cx="12" cy="12" r="3" fill="#0F172A" />
<circle cx="12" cy="12" r="1.1" fill="#FFFFFF" />
<circle cx="7" cy="18" r="2.2" fill="#0F172A" />
<circle cx="17" cy="18" r="2.2" fill="#0F172A" />
</svg>
</div>
<div>
<div class="brand-title">Graph Sparrow</div>
<div class="brand-sub">
Interactive graph explorer · force-directed layout · jQuery Arrows
</div>
</div>
</div>
<div class="header-actions">
<button class="btn" id="btn-guide">📖 Guide</button>
<button class="btn" id="reload-graph-layout">🎯 Re-layout</button>
<button class="btn" id="export-graph">⬇ Export JSON</button>
<button class="btn" id="import-graph">⬆ Import JSON</button>
<button class="btn" id="btn-theme">☀️/🌙 Theme</button>
</div>
</header>
<main>
<aside class="sidebar">
<section class="panel">
<div class="panel-title">➕ Add Node</div>
<input id="node-id" placeholder="unique id" />
<input id="node-text" placeholder="label (optional)" />
<select id="node-class">
<option value="green-node">green-node</option>
<option value="blue-node">blue-node</option>
<option value="red-node">red-node</option>
<option value="cyan-node">cyan-node</option>
<option value="amber-node">amber-node</option>
</select>
<button class="btn btn-add" id="add-node">Add node</button>
</section>
<section class="panel">
<div class="panel-title">🔗 Add Edge</div>
<select id="edge-src-node-id"></select>
<select id="edge-dst-node-id"></select>
<select id="edge-class">
<option value="blue-dashed-edge">blue-dashed-edge</option>
<option value="purple-edge">purple-edge</option>
<option value="orange-edge">orange-edge</option>
<option value="green-edge">green-edge</option>
<option value="rose-edge">rose-edge</option>
</select>
<input id="edge-text" placeholder="label (optional)" />
<button class="btn btn-add" id="add-edge">Add edge</button>
</section>
<section class="panel">
<div class="panel-title">🧲 Physics</div>
<label class="slider-label">Stiffness <span id="stiffness-val">500</span></label>
<input type="range" id="stiffness" min="100" max="1200" step="50" value="500" />
<label class="slider-label">Repulsion <span id="repulsion-val">2000</span></label>
<input type="range" id="repulsion" min="500" max="5000" step="100" value="2000" />
<label class="slider-label">Damping <span id="damping-val">0.5</span></label>
<input type="range" id="damping" min="0.1" max="0.9" step="0.05" value="0.5" />
</section>
</aside>
<section class="graph-stage">
<div id="svg-arrows"></div>
<div id="graph-viewer"></div>
<div id="hidden-nodes" class="hidden-pool"></div>
<div class="hint">drag nodes · toggle categories · re-layout with spring physics</div>
</section>
<aside class="sidebar sidebar-right">
<section class="panel" id="node-toggles-panel">
<div class="panel-title">
<span>👁️ Node Categories</span>
<span class="panel-subtitle">click to hide/show</span>
</div>
<div id="node-toggles"></div>
</section>
<section class="panel" id="edge-toggles-panel">
<div class="panel-title">
<span>👁️ Edge Categories</span>
<span class="panel-subtitle">click to hide/show</span>
</div>
<div id="edge-toggles"></div>
</section>
</aside>
</main>
<div id="guide" class="modal hidden">
<div class="modal-backdrop" data-close-guide></div>
<div class="modal-card">
<button class="modal-close" data-close-guide title="Close">✕</button>
<div class="modal-title">📖 Graph Sparrow — Guide</div>
<section class="guide-section">
<h3>What it is</h3>
<p>
<b>Graph Sparrow</b> is an interactive <b>graph editor and visualizer</b>: build
networks of nodes and edges, let a <b>force-directed physics simulation</b> lay them
out, group them by category, and share them as JSON. Connectors are drawn by the
<a href="https://github.com/alejp1998/jquery-arrows" target="_blank" rel="noopener"
>jQuery Arrows</a
>
plugin and follow the nodes as you drag them.
</p>
</section>
<section class="guide-section">
<h3>How it works</h3>
<ul>
<li>
<b>Force-directed layout</b> — the Springy physics engine simulates springs (edges)
and repulsion (nodes); <b>Stiffness</b>, <b>Repulsion</b>, and <b>Damping</b> sliders
tune the feel, and 🎯 Re-layout runs 400 deterministic ticks.
</li>
<li>
<b>Live SVG arrows</b> — colored, labeled connectors updated at 60 FPS and instantly
during drags.
</li>
<li>
<b>Category system</b> — nodes and edges carry color classes; the right panel toggles
entire categories with one click (live count badges).
</li>
<li>
<b>JSON serialization</b> — export/import the whole graph for sharing or backup.
</li>
<li>
<b>Node lifecycle</b> — hide/show moves nodes to a hidden pool; removing a node
cascades to its incident edges.
</li>
<li>
<b>Cockpit UI</b> — 100vh locked layout with system-aware theme, no page scrollbars.
</li>
</ul>
</section>
<section class="guide-section">
<h3>How to use this demo</h3>
<ul>
<li><b>Drag nodes</b> — arrows follow in real time.</li>
<li>
<b>🎯 Re-layout</b> — re-run the physics simulation; tune the sliders first for
different spreads.
</li>
<li>
<b>➕ Add Node / 🔗 Add Edge</b> — extend the graph through the left sidebar forms.
</li>
<li>
<b>👁️ Node / Edge Categories</b> — the right panel hides or shows whole categories.
</li>
<li><b>⬇ Export / ⬆ Import</b> — save the graph as JSON and reload it later.</li>
<li><b>🌙/☀️ Theme</b> — follows your system preference by default.</li>
</ul>
</section>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.3/jquery-ui.min.js"></script>
<script src="jquery.arrows.js"></script>
<script src="springy.js"></script>
<script src="addnodesedges.js"></script>
<script src="hideshownodes.js"></script>
<script src="edgenodetoggles.js"></script>
<script src="reloadgraphlayout.js"></script>
<script>
$(document).ready(function () {
// Seed an example graph
[
{ id: "sensor-1", category: "cyan-node", text: "Temp sensor" },
{ id: "sensor-2", category: "cyan-node", text: "Humidity sensor" },
{ id: "edge-gw", category: "blue-node", text: "Edge gateway" },
{ id: "cloud", category: "purple-node", text: "Cloud broker" },
{ id: "dt", category: "green-node", text: "Digital Twin" },
].forEach(addNode);
[
{
srcNodeId: "sensor-1",
dstNodeId: "edge-gw",
category: "blue-dashed-edge",
text: "MQTT pub",
},
{
srcNodeId: "sensor-2",
dstNodeId: "edge-gw",
category: "blue-dashed-edge",
text: "MQTT pub",
},
{
srcNodeId: "edge-gw",
dstNodeId: "cloud",
category: "purple-edge",
text: "MQTT bridge",
},
{ srcNodeId: "cloud", dstNodeId: "dt", category: "green-edge", text: "state sync" },
{ srcNodeId: "dt", dstNodeId: "sensor-1", category: "orange-edge", text: "control" },
].forEach(addEdge);
// Auto-run the force-directed layout so nodes distribute immediately
// (must run AFTER edges exist — reloadGraphLayout hides unconnected nodes)
reloadGraphLayout();
// Node / edge add buttons
$("#add-node").on("click", function () {
var id = $("#node-id").val().trim();
if (!id) return alert("Node id is empty! Write a unique node id.");
if (window.nodes[id]) return alert("Node id already exists!");
addNode({
id: id,
category: $("#node-class").val(),
text: $("#node-text").val(),
});
$("#node-id").val("");
$("#node-text").val("");
});
$("#add-edge").on("click", function () {
var src = $("#edge-src-node-id").val();
var dst = $("#edge-dst-node-id").val();
if (!src || !dst) return alert("Both endpoints are required.");
if (src === dst) return alert("Source and destination must differ.");
if (
window.edges.some(function (e) {
return e.id === src + "-" + dst || e.id === dst + "-" + src;
})
) {
return alert("Edge already exists!");
}
addEdge({
srcNodeId: src,
dstNodeId: dst,
category: $("#edge-class").val(),
text: $("#edge-text").val(),
});
$("#edge-text").val("");
});
// Export / import
$("#export-graph").on("click", function () {
var blob = new Blob([exportGraph()], { type: "application/json" });
var a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "graph-sparrow-export.json";
a.click();
URL.revokeObjectURL(a.href);
});
$("#import-graph").on("click", function () {
var input = document.createElement("input");
input.type = "file";
input.accept = "application/json";
input.onchange = function (e) {
var file = e.target.files[0];
if (!file) return;
var reader = new FileReader();
reader.onload = function () {
try {
importGraph(reader.result);
alert("Graph imported successfully!");
} catch (err) {
alert("Invalid graph JSON: " + err.message);
}
};
reader.readAsText(file);
};
input.click();
});
// Physics sliders
["stiffness", "repulsion", "damping"].forEach(function (key) {
$("#" + key).on("input", function () {
$("#" + key + "-val").text($(this).val());
});
});
window.springParams = { stiffness: 500, repulsion: 2000, damping: 0.5 };
// System theme change listener (follows OS if user hasn't manually locked a theme)
if (window.matchMedia) {
window
.matchMedia("(prefers-color-scheme: dark)")
.addEventListener("change", function (e) {
if (!localStorage.getItem("theme")) {
document.documentElement.setAttribute("data-theme", e.matches ? "dark" : "light");
}
});
}
// Guide modal
var guideOpen = false;
$("#btn-guide").on("click", function () {
guideOpen = true;
$("#guide").removeClass("hidden");
});
$("[data-close-guide]").on("click", function () {
guideOpen = false;
$("#guide").addClass("hidden");
});
$(document).on("keydown", function (e) {
if (e.key === "Escape" && guideOpen) {
guideOpen = false;
$("#guide").addClass("hidden");
}
});
// Theme toggle
$("#btn-theme").on("click", function () {
var current = document.documentElement.getAttribute("data-theme") || "dark";
var next = current === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", next);
localStorage.setItem("theme", next);
});
});
</script>
</body>
</html>