Newer
Older
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
import PropTypes, { InferType } from "prop-types";
import { State } from "../state";
import * as Interactions from "../interactions";
import { Graph } from "../structures/graph/graph";
import ForceGraph from "force-graph";
import { loadGraphJson } from "../../../datasets";
export class Editor extends React.PureComponent<
InferType<typeof Editor.propTypes>,
InferType<typeof Editor.stateTypes>
> {
static propTypes = {};
static stateTypes = {
state: State,
graph: Graph,
renderer: PropTypes.any,
};
// TODO: Not a long term solution!
public static globalState: State;
public static globalGraph: Graph;
public static globalRenderer: any;
constructor(props: InferType<typeof Editor.propTypes>) {
super(props);
Interactions.initInteractions();
}
/**
* Loads a space from the database to the editor.
* @param spaceId Id of space to load.
* @returns Promise with boolean value that is true, if successful.
*/
public loadSpace(spaceId: string): any {
return loadGraphJson(spaceId).then(this.loadGraph);
}
/**
* Loads another graph based on the data supplied. Note: Naming currently suggests that this only loads a GRAPH, not a SPACE. Needs further work and implementation to see if that makes sense or not.
* @param data Serialized graph data.
* @returns True, if successful.
*/
public loadGraph(data: any): boolean {
// Create global objects
const state = new State();
const graph = Graph.parse(data);
// Is valid and parsed successfully?
if (graph == undefined) {
return false;
}
// Create renderer
const renderTarget = document.getElementById("2d-graph");
const renderWidth = renderTarget.offsetWidth;
const renderer = ForceGraph()(renderTarget);
// Subscribe to interactions
renderer
.height(600)
.width(renderWidth)
.graphData(graph.data)
.nodeLabel("label")
.linkColor((link) => state.linkColor(link))
.nodeColor((node) => state.nodeColor(node))
.onNodeClick((node) => state.onNodeClick(node))
.onNodeDragEnd((node, translate) =>
state.onNodeDragEnd(node, translate)
)
.autoPauseRedraw(false) // keep redrawing after engine has stopped
.linkWidth((link) => state.linkWidth(link))
.linkDirectionalParticles(state.linkDirectionalParticles())
.linkDirectionalParticleWidth((link) =>
state.linkDirectionalParticleWidth(link)
)
.onBackgroundClick((event) =>
state.onBackgroundClick(event, this.extractPositions(event))
)
.nodeCanvasObjectMode((node) => state.nodeCanvasObjectMode(node))
.nodeCanvasObject((node, ctx, globalScale) =>
state.nodeCanvasObject(node, ctx, globalScale)
)
.linkCanvasObjectMode((link) => state.linkCanvasObjectMode(link))
.linkCanvasObject((link, ctx, globalScale) =>
state.linkCanvasObject(link, ctx, globalScale)
)
.onLinkClick((link) => state.onLinkClick(link));
// Connect update event
graph.onChangeCallbacks.push((data) => {
renderer.graphData(data);
});
// Set as new state
this.setState({
state: state,
graph: graph,
renderer: renderer,
});
Editor.globalState = state;
Editor.globalGraph = graph;
Editor.globalRenderer = renderer;
// Subscribe to global key-press events
document.onkeydown = this.state.state.onKeyDown;
document.onkeyup = this.state.state.onKeyUp;
return true;
}
/**
* Calculates the corresponding coordinates for a click event for easier further processing.
* @param event The corresponding click event.
* @returns Coordinates in graph and coordinates in browser window.
*/
private extractPositions(event: any): {
graph: { x: number; y: number };
window: { x: number; y: number };
} {
return {
graph: this.state.renderer.screen2GraphCoords(
event.layerX,
event.layerY
),
window: { x: event.clientX, y: event.clientY },
};
}
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
render(): React.ReactNode {
// The id "ks-editor" indicates, that the javascript associated with this should automatically be executed
return (
<div id="ks-editor">
<h1>Interface</h1>
<div id="box-select-layer">
<div id="2d-graph"></div>
</div>
<section id="toolbar"></section>
<section id="tool-menu">
<div id="delete-menu" className="hidden">
<p>
Drag and drop while pressing SHIFT to delete all the
nodes that are being selected.
</p>
</div>
<div id="collect-menu" className="hidden">
<h3>Collected items</h3>
<button id="clear-collection">Clear</button>
<ul id="selected-items"></ul>
</div>
<div id="select-menu" className="">
<p id="nothing-selected">Nothing selected</p>
<div id="node-selected" className="hidden">
<label htmlFor="node-name" hidden>
Name
</label>
<br />
<input
type="text"
id="node-name"
name="node-name"
placeholder="Enter name"
className="bottom-space"
></input>
<br />
<label htmlFor="node-description">
Description
</label>
<br />
<textarea
id="node-description"
name="node-description"
className="bottom-space"
></textarea>
<br />
<label htmlFor="node-image">Node Image</label>
<br />
<img
id="node-image-preview"
className="preview-image"
src=""
/>
<br />
<input
type="text"
id="node-image"
name="node-image"
placeholder="Enter file name or URL"
className="bottom-space"
/>
<br />
<label htmlFor="node-detail-image">
Info Image
</label>
<br />
<img
id="node-detail-image-preview"
className="preview-image"
src=""
/>
<br />
<input
type="text"
id="node-detail-image"
name="node-detail-image"
placeholder="Enter file name or URL"
className="bottom-space"
/>
<br />
<label htmlFor="node-type">Type</label>
<br />
<select
id="node-type"
name="node-type"
className="bottom-space"
>
<option value="Vorlesung">Vorlesung</option>
<option value="Algorithmus">Algorithmus</option>
<option value="Definition">Definition</option>
<option value="Beispiel">Beispiel</option>
<option value="Übung">Übung</option>
<option value="Kapitel">Kapitel</option>
</select>
<br />
<label htmlFor="node-video">Video</label>
<br />
<input
type="text"
placeholder="Video URL"
id="node-video"
name="node-video"
></input>
<br />
<label htmlFor="node-references">
References
</label>{" "}
<small>One URL per line</small>
<br />
<textarea
id="node-references"
name="node-references"
className="bottom-space"
></textarea>
</div>
<div id="link-selected" className="hidden">
<h3 id="link-name"></h3>
</div>
</div>
<div id="settings-menu" className="hidden">
<label htmlFor="label-toggle" className="bottom-space">
<input
type="checkbox"
checked
id="label-toggle"
name="label-toggle"
></input>
Show labels in graph
</label>
<br />
<br />
<h3>Space</h3>
<label htmlFor="space-id-select">Currently open</label>
<br />
<select
id="space-id-select"
name="space-id-select"
className="bottom-space"
></select>
<br />
<br />
<h3>Physics Simulation</h3>
<button
id="reanimate-button"
name="reanimate-button"
className="bottom-space"
>
Re-simulate
</button>
<br />
<label htmlFor="stop-physics-delay">
Amount of time [in seconds] after which the physics
simulation is stopped
</label>
<br />
<input
type="number"
onKeyPress={(event) =>
(event.charCode != 8 && event.charCode == 0) ||
(event.charCode >= 48 && event.charCode <= 57)
}
value="5"
id="stop-physics-delay"
name="stop-physics-delay"
className="small-width"
></input>
<br />
<br />
<h3>Import Space</h3>
<label htmlFor="import-space-area">Space JSON</label>
<br />
<textarea
id="import-space-area"
name="import-space-area"
className="bottom-space"
></textarea>
<br />
<label htmlFor="import-space-name-text">
Space Name
</label>
<br />
<input
type="text"
id="import-space-name-text"
name="import-space-name-text"
className="bottom-space"
></input>
<br />
<button
id="import-space-btn"
name="import-space-btn"
className="bottom-space"
>
Import
</button>
<br />
<br />
</div>
</section>
</div>
);
}
}