-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
399 lines (364 loc) · 12 KB
/
index.html
File metadata and controls
399 lines (364 loc) · 12 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Polycule Visualiser</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="alternate"
type="application/rss+xml"
title="polycule visualiser feed"
href="https://server.alifeee.co.uk/polycule/rss"
/>
<script>
// use the file polycule.yaml instead if this website is on a server
// if you open index.html locally as a file you can change the below
const polycule_yaml = `
nodes:
- name: This
- name: is
- name: sourcecode
- name: website
- name: available
- name: an example
edges:
- from: This
to: website
- from: website
to: is
- from: an example
to: is
- from: website
to: sourcecode
type: dashed
- from: sourcecode
to: is
type: dashed
- from: is
to: available
type: dashed
`;
</script>
<script src="./public/jquery.min.js"></script>
<script src="./public/js-yaml.min.js"></script>
<script src="./springy/springy.js"></script>
<script src="./springy/springyui.js"></script>
<script>
let lastClicked = null;
function clearForm(form) {
let inputs = document.querySelectorAll("input");
for (input of inputs) {
input.value = "";
}
}
function pathCallBack(node1, node2) {
return function (ev) {
let form = document.getElementById("form");
clearForm(form);
let dc1 = document.getElementById("dcedge1");
let dc2 = document.getElementById("dcedge2");
dc1.value = node1;
dc2.value = node2;
lastClicked = null;
};
}
function nodeCallBack(node) {
return function (ev) {
let form = document.getElementById("form");
clearForm(form);
if (!lastClicked || lastClicked == node) {
let removenode = document.getElementById("removenode");
removenode.value = node;
} else {
let addedge1 = document.getElementById("addedge1");
let addedge2 = document.getElementById("addedge2");
addedge1.value = lastClicked;
addedge2.value = node;
}
lastClicked = node;
};
}
function make_graph(json) {
let graph = new Springy.Graph();
const num_nodes = json["nodes"].length;
const stroke_width = num_nodes > 5 ? 10 / num_nodes : 2;
const text_size = num_nodes > 5 ? 40 / num_nodes : 8;
json["nodes"].forEach((n) => {
graph.addNodes(n.name);
});
json["edges"].forEach((e) => {
graph.addEdges([e.from, e.to, { type: e.type }]);
});
// graph.loadJSON(json);
// var springy = jQuery("#springydemo").springy({
// graph: graph,
// });
let layout = new Springy.Layout.ForceDirected(graph, 400.0, 400.0, 0.5);
let svg = document.getElementById("svg");
let rects = svg.getElementById("rects");
let clickrects = svg.getElementById("clickrects");
let edges = svg.getElementById("edges");
let nodes = svg.getElementById("nodes");
let svg_bbox = {
bottomleft: { x: 10, y: 90 },
topright: { x: 90, y: 10 },
};
function transform_coordinates(fromcorners, tocorners, point) {
b1x = fromcorners.bottomleft.x;
b1y = fromcorners.bottomleft.y;
b2x = fromcorners.topright.x;
b2y = fromcorners.topright.y;
s1x = tocorners.bottomleft.x;
s1y = tocorners.bottomleft.y;
s2x = tocorners.topright.x;
s2y = tocorners.topright.y;
let transfomed = {
x: s1x + ((s2x - s1x) / (b2x - b1x)) * (point.x - b1x),
y: s1y + ((s2y - s1y) / (b2y - b1y)) * (point.y - b1y),
};
return transfomed;
}
function makePathBig(ev) {
ev.target.setAttributeNS(null, "stroke-width", stroke_width * 2);
}
function makePathSmall(ev) {
ev.target.setAttributeNS(null, "stroke-width", stroke_width);
}
let renderer = new Springy.Renderer(
layout,
function clear() {
for (group of [nodes, edges, rects, clickrects]) {
while (group.lastChild) {
group.removeChild(group.lastChild);
}
}
},
function drawEdge(edge, p1, p2) {
// draw edge
let boundingbox = layout.getBoundingBox();
a1 = transform_coordinates(boundingbox, svg_bbox, p1);
a2 = transform_coordinates(boundingbox, svg_bbox, p2);
let NS = "http://www.w3.org/2000/svg";
let path = document.createElementNS(NS, "path");
path.setAttributeNS(
null,
"d",
`M ${a1.x} ${a1.y} L ${a2.x} ${a2.y}`
);
path.setAttributeNS(null, "stroke", "#8878BC");
if (edge.data?.type == "dashed") {
path.setAttributeNS(null, "stroke-dasharray", stroke_width / 2);
}
path.setAttributeNS(null, "stroke-width", stroke_width);
edges.appendChild(path);
path.addEventListener("mouseenter", makePathBig);
path.addEventListener("mouseleave", makePathSmall);
path.addEventListener(
"click",
pathCallBack(edge.source.id, edge.target.id)
);
},
function drawNode(node, p) {
let boundingbox = layout.getBoundingBox();
a = transform_coordinates(boundingbox, svg_bbox, p);
let NS = "http://www.w3.org/2000/svg";
let text = document.createElementNS(NS, "text");
text.setAttributeNS(null, "x", a.x);
text.setAttributeNS(null, "y", a.y);
text.setAttributeNS(null, "font-size", text_size);
text.setAttributeNS(null, "text-anchor", "middle");
text.setAttributeNS(null, "alignment-baseline", "central");
text.setAttributeNS(null, "dominant-baseline", "central");
text.setAttributeNS(null, "fill", "#FFFFFF");
let textNode = document.createTextNode(node.id);
text.appendChild(textNode);
nodes.appendChild(text);
let bbox = text.getBBox();
let width = bbox.width * 1.1;
let height = bbox.height * 1.2;
let rect = document.createElementNS(NS, "rect");
rect.setAttributeNS(null, "x", a.x - width / 2);
rect.setAttributeNS(null, "y", a.y - height / 2);
rect.setAttributeNS(null, "width", width);
rect.setAttributeNS(null, "height", height);
rect.setAttributeNS(null, "fill", "#962727");
rect.setAttributeNS(null, "rx", 1);
rects.appendChild(rect);
let clickrect = document.createElementNS(NS, "rect");
clickrect.setAttributeNS(null, "x", a.x - width / 2);
clickrect.setAttributeNS(null, "y", a.y - height / 2);
clickrect.setAttributeNS(null, "width", width);
clickrect.setAttributeNS(null, "height", height);
clickrect.setAttributeNS(null, "fill", "#0000");
clickrect.setAttributeNS(null, "rx", 1);
clickrects.appendChild(clickrect);
clickrect.addEventListener("click", nodeCallBack(node.id));
}
);
renderer.start();
}
function show_error(error) {
console.log("error !");
console.log(error);
const errorsec = document.querySelector(".errors");
const pre = document.createElement("pre");
pre.innerText =
"something went wrong with loading yaml\n" + error.message;
errorsec.appendChild(pre);
errorsec.classList.remove("invisible");
}
document.addEventListener("DOMContentLoaded", (e) => {
switch (window.location.protocol) {
case "http:":
case "https:":
fetch("polycule.yaml", { cache: "no-store" })
.then((data) => data.text())
.then((text) => {
const obj = jsyaml.load(text);
return obj;
})
.catch(show_error)
.then(make_graph);
break;
case "file:":
let json;
try {
json = jsyaml.load(polycule_yaml);
} catch (error) {
show_error(error);
}
make_graph(json);
break;
default:
console.log(
"what kind of weird way did you find to open this file"
);
}
});
</script>
<style>
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
max-height: 100vh;
}
@font-face {
font-family: "Hockey is Life";
src: url("./public/hockey.ttf");
}
body {
font-family: "Hockey is Life", "BogFace", sans-serif;
background: conic-gradient(
from 45deg,
#c44b4477 25%,
#bc524d77 0% 50%,
#af4d4877 0% 75%,
#db797477 0%
)
50%/ 40px 60px;
background-position: top;
}
button {
font-family: "Hockey is Life", "BogFace", sans-serif;
}
#svg {
width: 100%;
max-width: 100%;
max-height: 100%;
}
#svg > #edges > path {
cursor: pointer;
}
#svg > #clickrects > rect {
cursor: pointer;
}
.errors {
position: absolute;
top: 0;
color: darkred;
background: lightgray;
border: 3px dashed red;
margin: 1rem;
padding: 1rem;
}
.errors.invisible {
display: none;
}
section.edit {
display: flex;
flex-direction: column;
justify-content: center;
}
section.edit > button {
width: max-content;
margin: auto;
}
#form {
display: none;
margin: auto;
}
#form input {
max-width: 6rem;
}
footer {
margin-top: 1rem;
text-align: center;
}
</style>
</head>
<body>
<svg
id="svg"
width="500"
height="500"
viewBox="0 0 100 100"
xmlns="http://www.w3.org/2000/svg"
>
<g id="edges"></g>
<g id="rects"></g>
<g id="nodes"></g>
<g id="clickrects"></g>
</svg>
<section class="errors invisible"></section>
<section class="edit">
<button onclick='$("#form").toggle()'>edit...</button>
<form action="./edit" method="post" id="form" autocomplete="off">
<label for="addnode">Add new node</label>
<input type="text" id="addnode" name="addnode" />
<br />
<label for="removenode">Remove node</label>
<input type="text" id="removenode" name="removenode" />
<br />
<label for="addedge1">Connect</label>
<input type="text" id="addedge1" name="addedge1" />
<label for="addedge2">to</label>
<input type="text" id="addedge2" name="addedge2" />
<br />
<label for="dashed">Dashed?</label>
<input type="checkbox" id="dashed" name="dashed" />
<br />
<label for="dcedge1">Disconnect</label>
<input type="text" id="dcedge1" name="dcedge1" />
<label for="dcedge2">from</label>
<input type="text" id="dcedge2" name="dcedge2" />
<br />
<label for="renamenode1">Rename node</label>
<input type="text" id="renamenode1" name="renamenode1" />
<label for="renamenode2">to</label>
<input type="text" id="renamenode2" name="renamenode2" />
<br />
<button role="form">Change!</button>
</form>
</section>
</body>
<footer>
by <a href="https://alifeee.co.uk">alifeee</a> · using
<a href="http://getspringy.com/">Springy</a> ·
<a href="https://github.com/alifeee/polycule-visualiser/">source code</a> ·
<a href="./rss">RSS feed</a>
</footer>
</html>