-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
290 lines (268 loc) · 9.16 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Polycule Visualiser</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="jquery.min.js"></script>
<script src="springy.js"></script>
<script src="springyui.js"></script>
<script>
let lastClicked = null;
function clearForm(form) {
let inputs = document.querySelectorAll("input");
for (input of inputs) {
input.value = "";
}
}
function makePathBig(ev) {
ev.target.setAttributeNS(null, "stroke-width", "2");
}
function makePathSmall(ev) {
ev.target.setAttributeNS(null, "stroke-width", "1");
}
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;
};
}
fetch("polycule.json", { cache: "no-store" })
.then((data) => data.json())
.then((json) => {
let graph = new Springy.Graph();
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;
}
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");
path.setAttributeNS(null, "stroke-width", "1");
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", 5);
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();
});
</script>
<style>
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
padding: 0;
max-height: 100vh;
}
@font-face {
font-family: "Hockey is Life";
src: url("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;
}
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="edit">
<button onclick='$("#form").toggle()'>edit...</button>
<form
action="{{URIs.root}}/polycule/edit.cgi"
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="dcedge1">Disconnect</label>
<input type="text" id="dcedge1" name="dcedge1" />
<label for="dcedge2">from</label>
<input type="text" id="dcedge2" name="dcedge2" />
<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>
</footer>
</html>