-
Notifications
You must be signed in to change notification settings - Fork 179
/
demo-simple.js
128 lines (117 loc) · 3.64 KB
/
demo-simple.js
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
const fbpGraph = require('fbp-graph');
const React = require('react');
const ReactDOM = require('react-dom');
const TheGraph = require('../index.js');
require('font-awesome/css/font-awesome.css');
require('../themes/the-graph-dark.styl');
require('../themes/the-graph-light.styl');
// Remove loading message
document.body.removeChild(document.getElementById('loading'));
// The graph editor
const editor = document.getElementById('editor');
// Component library
const library = {
basic: {
name: 'basic',
description: 'basic demo component',
icon: 'eye',
inports: [
{ name: 'in0', type: 'all' },
{ name: 'in1', type: 'all' },
{ name: 'in2', type: 'all' },
],
outports: [
{ name: 'out', type: 'all' },
],
},
tall: {
name: 'tall',
description: 'tall demo component',
icon: 'cog',
inports: [
{ name: 'in0', type: 'all' },
{ name: 'in1', type: 'all' },
{ name: 'in2', type: 'all' },
{ name: 'in3', type: 'all' },
{ name: 'in4', type: 'all' },
{ name: 'in5', type: 'all' },
{ name: 'in6', type: 'all' },
{ name: 'in7', type: 'all' },
{ name: 'in8', type: 'all' },
{ name: 'in9', type: 'all' },
{ name: 'in10', type: 'all' },
{ name: 'in11', type: 'all' },
{ name: 'in12', type: 'all' },
],
outports: [
{ name: 'out0', type: 'all' },
],
},
};
// Load empty graph
let graph = new fbpGraph.Graph();
function renderEditor() {
const props = {
readonly: false,
height: window.innerHeight,
width: window.innerWidth,
graph,
library,
};
// console.log('render', props);
const editor = document.getElementById('editor');
editor.width = props.width;
editor.height = props.height;
const element = React.createElement(TheGraph.App, props);
ReactDOM.render(element, editor);
}
graph.on('endTransaction', renderEditor); // graph changed
window.addEventListener('resize', renderEditor);
// Add node button
const addnode = function () {
const id = Math.round(Math.random() * 100000).toString(36);
const component = Math.random() > 0.5 ? 'basic' : 'tall';
const metadata = {
label: component,
x: Math.round(Math.random() * 800),
y: Math.round(Math.random() * 600),
};
const newNode = graph.addNode(id, component, metadata);
return newNode;
};
document.getElementById('addnode').addEventListener('click', addnode);
// Add edge button
const addedge = function (outNodeID) {
const { nodes } = graph;
const len = nodes.length;
if (len < 1) { return; }
const node1 = outNodeID || nodes[Math.floor(Math.random() * len)].id;
const node2 = nodes[Math.floor(Math.random() * len)].id;
const port1 = `out${Math.floor(Math.random() * 3)}`;
const port2 = `in${Math.floor(Math.random() * 12)}`;
const meta = { route: Math.floor(Math.random() * 10) };
const newEdge = graph.addEdge(node1, port1, node2, port2, meta);
return newEdge;
};
document.getElementById('addedge').addEventListener('click', (event) => { addedge(); });
// Random graph button
document.getElementById('random').addEventListener('click', () => {
graph.startTransaction('randomgraph');
for (let i = 0; i < 20; i++) {
const node = addnode();
addedge(node.id);
addedge(node.id);
}
graph.endTransaction('randomgraph');
});
// Get graph button
document.getElementById('get').addEventListener('click', () => {
const graphJSON = JSON.stringify(graph.toJSON(), null, 2);
alert(graphJSON);
// you can use the var graphJSON to save the graph definition in a file/database
});
// Clear button
document.getElementById('clear').addEventListener('click', () => {
graph = new fbpGraph.Graph();
renderEditor();
});