forked from flowhub/the-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo-full.html
292 lines (257 loc) · 9.2 KB
/
demo-full.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
<!DOCTYPE html>
<html>
<head>
<title>Graph Editor</title>
<meta charset="utf-8">
<!-- Libraries -->
<script src="../node_modules/react/dist/react.js"></script>
<script src="../node_modules/react-dom/dist/react-dom.js"></script>
<script src="../node_modules/klayjs-noflo/klay-noflo.js"></script>
<script src="../node_modules/hammerjs/hammer.min.js"></script>
<!-- Browserify Libraries -->
<script src="../dist/the-graph.js"></script>
<script src="../node_modules/@pleasetrythisathome/react.animate/react.animate.js"></script>
<link rel="stylesheet" href="../themes/the-graph-dark.css">
<link rel="stylesheet" href="../themes/the-graph-light.css">
<!-- Fonts -->
<link rel="stylesheet" href="../node_modules/font-awesome/css/font-awesome.min.css">
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:400' rel='stylesheet' type='text/css'>
<style>
@font-face {
/* we want the svg version */
font-family: 'FontAwesomeSVG';
src: url('../node_modules/font-awesome/fonts/fontawesome-webfont.svg?v=4.0.3#fontawesomeregular') format('svg'),
url('../node_modules/font-awesome/fonts/fontawesome-webfont.eot?#iefix&v=4.0.3') format('embedded-opentype'),
url('../node_modules/font-awesome/fonts/fontawesome-webfont.woff?v=4.0.3') format('woff'),
url('../node_modules/font-awesome/fonts/fontawesome-webfont.ttf?v=4.0.3') format('truetype');
font-weight: normal;
font-style: normal;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
<style>
html, body {
width: 100%;
height: 100%;
}
body {
background-color: hsl(189, 47%, 6%);
font-family: "SourceCodePro",Helvetica,Arial,sans-serif;
overflow: hidden;
}
#editor {
background-color: transparent;
position: absolute;
top: 0;
left: 0;
}
#nav {
position: absolute;
right: 0px;
bottom: 0px;
}
#testing {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="editor"></div>
<div id="nav"></div>
<div id="testing">
<button id="theme" style="">theme</button>
<button id="autolayout" style="display: none">autolayout</button>
<button id="focus" style="display: none">focus</button>
</div>
<div id="loading" style="position:absolute; top:10px; left:10px; background-color:white; padding:10px; border-radius:5px;">
<img src="assets/loading.gif"/>
<div id="loading-message">loading custom elements...</div>
</div>
<script type="text/javascript">
"use strict";
var fbpGraph = window.TheGraph.fbpGraph;
// Context menu specification
function deleteNode(graph, itemKey, item) {
graph.removeNode(itemKey);
}
function deleteEdge(graph, itemKey, item) {
graph.removeEdge(item.from.node, item.from.port, item.to.node, item.to.port);
}
var contextMenus = {
main: null,
selection: null,
nodeInport: null,
nodeOutport: null,
graphInport: null,
graphOutport: null,
edge: {
icon: "long-arrow-right",
s4: {
icon: "trash-o",
iconLabel: "delete",
action: deleteEdge
}
},
node: {
s4: {
icon: "trash-o",
iconLabel: "delete",
action: deleteNode
},
},
group: {
icon: "th",
s4: {
icon: "trash-o",
iconLabel: "ungroup",
action: function (graph, itemKey, item) {
graph.removeGroup(itemKey);
},
},
},
};
var appState = {
graph: new fbpGraph.Graph(),
library: {},
iconOverrides: {},
theme: 'dark',
};
// Attach nav
function fitGraphInView() {
editor.triggerFit();
}
function panEditorTo() {
}
function editorPanChanged(x, y, scale) {
appState.editorViewX = -x;
appState.editorViewY = -y;
appState.editorScale = scale;
renderNav();
}
function renderNav() {
var view = [
appState.editorViewX, appState.editorViewY,
window.innerWidth, window.innerHeight,
];
var props = {
height: 162,
width: 216,
graph: appState.graph,
onTap: fitGraphInView,
onPanTo: panEditorTo,
viewrectangle: view,
viewscale: appState.editorScale,
};
var element = React.createElement(TheGraph.nav.Component, props);
ReactDOM.render(element, document.getElementById('nav'));
}
function renderApp() {
var editor = document.getElementById('editor');
editor.className = 'the-graph-' + appState.theme;
var props = {
width: window.innerWidth,
height: window.innerWidth,
graph: appState.graph,
library: appState.library,
menus: contextMenus,
nodeIcons: appState.iconOverrides,
onPanScale: editorPanChanged,
}
console.log('render', props);
var editor = document.getElementById('editor');
editor.width = props.width;
editor.height = props.height;
var element = React.createElement(TheGraph.App, props);
ReactDOM.render(element, editor);
renderNav();
}
renderApp(); // initial
// Follow changes in window size
window.addEventListener("resize", renderApp);
// Toggle theme
var theme = "dark";
document.getElementById("theme").addEventListener("click", function () {
theme = (theme==="dark" ? "light" : "dark");
appState.theme = theme;
renderApp();
});
// Autolayout button
document.getElementById("autolayout").addEventListener("click", function () {
// TODO: support via React props
editor.triggerAutolayout();
});
// Focus a node
document.getElementById("focus").addEventListener("click", function () {
// TODO: support via React props
var nodes = appState.graph.nodes;
var randomNode = nodes[Math.floor(Math.random()*nodes.length)];
editor.focusNode(randomNode);
});
// Simulate node icon updates
var iconKeys = Object.keys(TheGraph.FONT_AWESOME);
window.setInterval(function () {
var nodes = appState.graph.nodes;
if (nodes.length>0) {
var randomNodeId = nodes[Math.floor(Math.random()*nodes.length)].id;
var randomIcon = iconKeys[Math.floor(Math.random()*iconKeys.length)];
appState.iconOverrides[randomNodeId] = randomIcon;
renderApp();
}
}, 1000);
// Simulate un/triggering errors
var errorNodeId = null;
var makeRandomError = function () {
if (errorNodeId) {
editor.removeErrorNode(errorNodeId);
}
var nodes = appState.graph.nodes;
if (nodes.length>0) {
errorNodeId = nodes[Math.floor(Math.random()*nodes.length)].id;
editor.addErrorNode(errorNodeId);
editor.updateErrorNodes();
}
};
//window.setInterval(makeRandomError, 3551); // TODO: support error nodes via React props
//makeRandomError();
// Load initial graph
var loadingMessage = document.getElementById("loading-message");
window.loadGraph = function (json) {
// Load graph
loadingMessage.innerHTML = "loading graph data...";
var graphData = json.data ? JSON.parse(json.data.files['noflo.json'].content) : json;
fbpGraph.graph.loadJSON(JSON.stringify(graphData), function(err, graph){
if (err) {
loadingMessage.innerHTML = "error loading graph: " + err.toString();
return;
}
// Synthesize component library from graph
appState.library = TheGraph.library.libraryFromGraph(graph);
// Set loaded graph
appState.graph = graph;
appState.graph.on('endTransaction', renderApp); // graph changed
renderApp();
// Remove loading message
var loading = document.getElementById("loading");
loading.parentNode.removeChild(loading);
console.log('loaded', graph);
});
};
var body = document.querySelector('body');
var script = document.createElement('script');
script.type = 'application/javascript';
// Clock
// script.src = 'https://api.github.com/gists/7135158?callback=loadGraph';
// script.src = 'clock.json.js';
script.src = 'assets/photobooth.json.js';
// Gesture object building (lots of ports!)
// script.src = 'https://api.github.com/gists/7022120?callback=loadGraph';
// Gesture data gathering (big graph)
// script.src = 'https://api.github.com/gists/7022262?callback=loadGraph';
// Edge algo test
//script.src = 'https://api.github.com/gists/6890344?callback=loadGraph';
body.appendChild(script);
</script>
</body>
</html>