-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_edit.html
350 lines (310 loc) · 8.9 KB
/
graph_edit.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Graph Programming with P-GP 2</title>
<style>
#app {
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#header {
flex: 0 0 auto;
-webkit-flex: 0 0 auto;
line-height: 1.3;
}
#panes {
display: flex;
display: -webkit-flex;
flex: 1 1 auto;
-webkit-flex: 1 1 auto;
}
#graph {
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
}
#options {
flex: 0 0 auto;
-webkit-flex: 0 0 auto;
}
#output {
flex: 1 1 auto;
-webkit-flex: 1 1 auto;
position: relative;
overflow: auto;
}
#editor {
border-right: 1px solid #ccc;
}
#header {
background: #eee;
border-bottom: 1px solid #ccc;
padding: 8px;
text-align: center;
}
#header b {
font-size: 18px;
}
#options {
background: #eee;
border-bottom: 1px solid #ccc;
padding: 8px;
}
#options label {
margin-right: 8px;
}
#options #raw.disabled {
opacity: 0.5;
}
#output svg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#output #text {
font-size: 12px;
font-family: monaco, courier, monospace;
white-space: pre;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: auto;
}
#output img {
display: block;
margin: 0 auto;
}
#output.working svg, #output.error svg,
#output.working #text, #output.error #text,
#output.working img, #output.error img {
opacity: 0.4;
}
#output.error #error {
display: inherit;
}
#output #error {
display: none;
position: absolute;
top: 20px;
left: 20px;
margin-right: 20px;
background: red;
color: white;
z-index: 1;
}
.gutter {
background-color: #eee;
background-repeat: no-repeat;
background-position: 50%;
}
.gutter.gutter-horizontal {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==');
cursor: ew-resize;
}
.split {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
overflow-y: auto;
overflow-x: hidden;
}
.split.split-horizontal, .gutter.gutter-horizontal {
height: 100%;
float: left;
}
</style>
</head>
<body>
<div id="app">
<div id="header">
<b>GP 2 Online Graph Visualiser</b> — Powered by <a href="http://viz-js.com/">viz-js</a>.<br/>
<a href = "http://timothyatkinson.github.io">Return to home page</a>.
</div>
<div id="panes" class="split split-horizontal">
<div id="editor" class="split">// Paste your GP 2 graph here.
[
(0, "A")
(1, 1:"C")
(2, "B")
(3, 2:"D")
|
(4, 0, 1, empty)
(5, 1, 2, empty)
(6, 2, 3, empty)
(7, 3, 0, empty)
]
</div>
<div id="graph" class="split">
<div id="options">
<label id="engine">
Engine:
<select>
<option selected>dot</option>
</select>
</label>
<label id="format">
Format:
<select>
<option selected>svg</option>
<option>png-image-element</option>
<option>json</option>
<option>xdot</option>
<option>plain</option>
<option>ps</option>
</select>
</label>
<label id="raw">
<input type="checkbox"> Show raw output
</label>
</div>
<div id="output">
<div id="error"></div>
</div>
</div>
</div>
</div>
<script src="./js/ace/ace.js"></script>
<script src="./bower_components/viz.js/viz.js"></script>
<script src="./bower_components/fabric.js/dist/fabric.min.js"></script>
<script src="./bower_components/Split.js/split.min.js"></script>
<script src="./bower_components/svg-pan-zoom/dist/svg-pan-zoom.min.js"></script>
<script>
var beforeUnloadMessage = null;
var resizeEvent = new Event("paneresize");
Split(['#editor', '#graph'], {
sizes: [25, 75],
onDragEnd: function() {
var svgOutput = document.getElementById("svg_output");
if (svgOutput != null) {
svgOutput.dispatchEvent(resizeEvent);
}
}
});
var editor = ace.edit("editor");
editor.getSession().setMode("ace/mode/dot");
var parser = new DOMParser();
var worker;
var result;
function updateGraph() {
if (worker) {
worker.terminate();
}
document.querySelector("#output").classList.add("working");
document.querySelector("#output").classList.remove("error");
worker = new Worker("./worker.js");
worker.onmessage = function(e) {
document.querySelector("#output").classList.remove("working");
document.querySelector("#output").classList.remove("error");
result = e.data;
updateOutput();
}
worker.onerror = function(e) {
document.querySelector("#output").classList.remove("working");
document.querySelector("#output").classList.add("error");
var message = e.message === undefined ? "An error occurred while processing the graph input." : e.message;
var error = document.querySelector("#error");
while (error.firstChild) {
error.removeChild(error.firstChild);
}
document.querySelector("#error").appendChild(document.createTextNode(message));
console.error(e);
e.preventDefault();
}
var params = {
src: editor.getSession().getDocument().getValue(),
options: {
engine: document.querySelector("#engine select").value,
format: document.querySelector("#format select").value
}
};
// Instead of asking for png-image-element directly, which we can't do in a worker,
// ask for SVG and convert when updating the output.
if (params.options.format == "png-image-element") {
params.options.format = "svg";
}
worker.postMessage(params);
}
function updateOutput() {
var graph = document.querySelector("#output");
var svg = graph.querySelector("svg");
if (svg) {
graph.removeChild(svg);
}
var text = graph.querySelector("#text");
if (text) {
graph.removeChild(text);
}
var img = graph.querySelector("img");
if (img) {
graph.removeChild(img);
}
if (!result) {
return;
}
if (document.querySelector("#format select").value == "svg" && !document.querySelector("#raw input").checked) {
var svg = parser.parseFromString(result, "image/svg+xml").documentElement;
svg.id = "svg_output";
graph.appendChild(svg);
panZoom = svgPanZoom(svg, {
zoomEnabled: true,
controlIconsEnabled: true,
fit: true,
center: true,
minZoom: 0.1
});
svg.addEventListener('paneresize', function(e) {
panZoom.resize();
}, false);
window.addEventListener('resize', function(e) {
panZoom.resize();
});
} else if (document.querySelector("#format select").value == "png-image-element") {
var image = Viz.svgXmlToPngImageElement(result);
graph.appendChild(image);
} else {
var text = document.createElement("div");
text.id = "text";
text.appendChild(document.createTextNode(result));
graph.appendChild(text);
}
}
editor.on("change", function() {
updateGraph();
beforeUnloadMessage = "Your changes will not be saved.";
});
window.addEventListener("beforeunload", function(e) {
return beforeUnloadMessage;
});
document.querySelector("#engine select").addEventListener("change", function() {
updateGraph();
});
document.querySelector("#format select").addEventListener("change", function() {
if (document.querySelector("#format select").value === "svg") {
document.querySelector("#raw").classList.remove("disabled");
document.querySelector("#raw input").disabled = false;
} else {
document.querySelector("#raw").classList.add("disabled");
document.querySelector("#raw input").disabled = true;
}
updateGraph();
});
document.querySelector("#raw input").addEventListener("change", function() {
updateOutput();
});
updateGraph();
</script>
</body>
</html>