forked from anvaka/ngraph.pixel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
486 lines (390 loc) · 12.4 KB
/
index.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
var THREE = require('three');
module.exports = pixel;
/**
* Expose to the outter world instance of three.js
* so that they can use it if they need it
*/
module.exports.THREE = THREE;
var eventify = require('ngraph.events');
var createNodeView = require('./lib/nodeView.js');
var createEdgeView = require('./lib/edgeView.js');
var createTooltipView = require('./lib/tooltip.js');
var createAutoFit = require('./lib/autoFit.js');
var createInput = require('./lib/input.js');
var validateOptions = require('./options.js');
var flyTo = require('./lib/flyTo.js');
var makeActive = require('./lib/makeActive.js');
function pixel(graph, options) {
// This is our public API.
var api = {
/**
* attempts to fit graph into available screen size
*/
autoFit: autoFit,
/**
* Returns current layout manager
*/
layout: getLayout,
/**
* Gets or sets value which indicates whether layout is stable. When layout
* is stable, then no additional layout iterations are required. The renderer
* will stop calling `layout.step()`, which in turn will save CPU cycles.
*
* @param {boolean+} stableValue if this value is not specified, then current
* value of `isStable` will be returned. Otherwise the simulator stable flag
* will be forcefully set to the given value.
*/
stable: stable,
/**
* Gets or sets graph that is rendered now
*
* @param {ngraph.graph+} graphValue if this value is not specified then current
* graph is returned. Otherwise renderer destroys current scene, and starts
* render new graph.
*/
graph: graphInternal,
/**
* Attempts to give keyboard input focuse to the scene
*/
focus: focus,
/**
* Requests renderer to move camera and focus on given node id.
*
* @param {string} nodeId identifier of the node to show
*/
showNode: showNode,
/**
* Allows clients to provide a callback function, which is invoked before
* each rendering frame
*
* @param {function} newBeforeFrameCallback the callback function. This
* argument is not chained, and any new value overwrites the old one
*/
beforeFrame: beforeFrame,
/**
* Returns instance of the three.js camera
*/
camera: getCamera,
/**
* Allows clients to set/get current clear color of the scene (the background)
*
* @param {number+} color if specified, then new color is set. Otherwise
* returns current clear color.
*/
clearColor: clearColor,
/**
* Allows clients to set/get current clear color opacity
*
* @param {number+} alpha if specified, then new alpha opacity is set. Otherwise
* returns current clear color alpha.
*/
clearAlpha: clearAlpha,
/**
* Synonmim for `clearColor`. Sets the background color of the scene
*
* @param {number+} color if specified, then new color is set. Otherwise
* returns current clear color.
*/
background: clearColor,
/**
* Gets UI for a given node id. If node creation function decided not to
* return UI for this node, falsy object is returned.
*
* @param {string} nodeId - identifier of the node
* @returns Object that represents UI for the node
*/
getNode: getNode,
/**
* Gets UI for a given link id. If link creation function decided not to
* return UI for this link, falsy object is returned.
*
* @param {string} linkId - identifier of the link
* @returns Object that represents UI for the link
*/
getLink: getLink,
/**
* Iterates over every link UI element
*
* @param {Function} cb - link visitor. Accepts one argument, which is linkUI
*/
forEachLink: forEachLink,
/**
* Iterates over every node UI element
*
* @param {Function} cb - node visitor. Accepts one argument, which is nodeUI
*/
forEachNode: forEachNode,
/**
* Gets three.js scene where current graph is rendered
*/
scene: getScene,
/**
* Forces renderer to update scene, without waiting for notifications
* from layouter
*/
redraw: redraw,
// Low level methods to get edgeView/nodeView.
// TODO: update docs if this sticks.
edgeView: getEdgeView,
nodeView: getNodeView
};
eventify(api);
options = validateOptions(options);
var beforeFrameCallback;
var container = options.container;
verifyContainerDimensions(container);
var layout = options.createLayout(graph, options);
if (layout && typeof layout.on === 'function') {
layout.on('reset', layoutReset);
}
var isStable = false;
var nodeIdToIdx = new Map();
var edgeIdToIndex = new Map();
var scene, camera, renderer;
var nodeView, edgeView, autoFitController, input;
var nodes, edges;
var tooltipView = createTooltipView(container);
init();
run();
focus();
return api;
function layoutReset() {
initPositions();
stable(false);
}
function getCamera() {
return camera;
}
function getEdgeView() {
return edgeView;
}
function getNodeView() {
return nodeView;
}
function redraw() {
edgeView.refresh();
nodeView.refresh();
}
function clearColor(newColor) {
newColor = normalizeColor(newColor);
if (typeof newColor !== 'number') return renderer.getClearColor();
renderer.setClearColor(newColor);
}
function clearAlpha(newAlpha) {
if (typeof newAlpha !== 'number') return renderer.getClearAlpha();
renderer.setClearAlpha(newAlpha);
}
function run() {
requestAnimationFrame(run);
if (beforeFrameCallback) {
beforeFrameCallback();
}
if (!isStable) {
isStable = layout.step();
updatePositions();
nodeView.update();
edgeView.update();
} else {
// we may not want to change positions, but colors/size could be changed
// at this moment, so let's take care of that:
if (nodeView.needsUpdate()) nodeView.update();
if (edgeView.needsUpdate()) edgeView.update();
}
if (isStable) api.fire('stable', true);
input.update();
if (autoFitController) {
autoFitController.update();
input.adjustSpeed(autoFitController.lastRadius());
}
renderer.render(scene, camera);
}
function getScene() {
return scene;
}
function beforeFrame(newBeforeFrameCallback) {
beforeFrameCallback = newBeforeFrameCallback;
}
function init() {
initScene();
initPositions();
listenToGraph();
}
function listenToGraph() {
// TODO: this is not efficient at all. We are recreating view from scratch on
// every single change.
graph.on('changed', initPositions);
}
function updatePositions() {
if (!nodes) return;
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i];
node.position = layout.getNodePosition(node.id);
}
}
function initPositions() {
edges = [];
nodes = [];
nodeIdToIdx = new Map();
edgeIdToIndex = new Map();
graph.forEachNode(addNodePosition);
graph.forEachLink(addEdgePosition);
nodeView.init(nodes);
edgeView.init(edges);
if (input) input.reset();
function addNodePosition(node) {
var nodeModel = options.node(node);
if (!nodeModel) return;
var idx = nodes.length;
var position = layout.getNodePosition(node.id);
if (typeof position.z !== 'number') position.z = 0;
nodeModel.id = node.id;
nodeModel.position = position;
nodeModel.idx = idx;
if (options.activeNode) {
nodes.push(makeActive(nodeModel));
} else {
nodes.push(nodeModel);
}
nodeIdToIdx.set(node.id, idx);
}
function addEdgePosition(edge) {
var edgeModel = options.link(edge);
if (!edgeModel) return;
var fromNode = nodes[nodeIdToIdx.get(edge.fromId)];
if (!fromNode) return; // cant have an edge that doesn't have a node
var toNode = nodes[nodeIdToIdx.get(edge.toId)];
if (!toNode) return;
edgeModel.idx = edges.length;
edgeModel.from = fromNode;
edgeModel.to = toNode;
edgeIdToIndex.set(edge.id, edgeModel.idx);
if (options.activeLink) {
edges.push(makeActive(edgeModel));
} else {
edges.push(edgeModel);
}
}
}
function initScene() {
scene = new THREE.Scene();
scene.sortObjects = false;
camera = new THREE.PerspectiveCamera(75, container.clientWidth / container.clientHeight, 0.1, 20000);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 200;
scene.add(camera);
nodeView = createNodeView(scene, options);
edgeView = createEdgeView(scene, options);
if (options.autoFit) autoFitController = createAutoFit(nodeView, camera);
var glOptions = {
antialias: false,
};
if (options.clearAlpha !== 1) {
glOptions.alpha = true;
}
renderer = new THREE.WebGLRenderer(glOptions);
renderer.setClearColor(options.clearColor, options.clearAlpha);
if (window && window.devicePixelRatio) renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(container.clientWidth, container.clientHeight);
container.appendChild(renderer.domElement);
input = createInput(camera, graph, renderer.domElement);
input.on('move', stopAutoFit);
input.on('nodeover', setTooltip);
input.on('nodeclick', passthrough('nodeclick'));
input.on('nodedblclick', passthrough('nodedblclick'));
window.addEventListener('resize', onWindowResize, false);
}
function getNode(nodeId) {
var idx = nodeIdToIdx.get(nodeId);
if (idx === undefined) return;
return nodes[idx];
}
function getLink(linkId) {
var idx = edgeIdToIndex.get(linkId);
if (idx === undefined) return;
return edges[idx];
}
function forEachLink(cb) {
if (typeof cb !== 'function') throw new Error('link visitor should be a function');
edges.forEach(cb);
}
function forEachNode(cb) {
if (typeof cb !== 'function') throw new Error('node visitor should be a function');
nodes.forEach(cb);
}
function setTooltip(e) {
var node = getNodeByIndex(e.nodeIndex);
if (node !== undefined) {
tooltipView.show(e, node);
} else {
tooltipView.hide(e);
}
api.fire('nodehover', node);
}
function passthrough(name) {
return function (e) {
var node = getNodeByIndex(e.nodeIndex);
if (node) api.fire(name, node);
};
}
function getNodeByIndex(nodeIndex) {
var nodeUI = nodes[nodeIndex];
return nodeUI && graph.getNode(nodeUI.id);
}
function stopAutoFit() {
input.off('move');
autoFitController = null;
}
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
function autoFit() {
if (autoFitController) return; // we are already auto-fitting the graph.
// otherwise fire and forget autofit:
createAutoFit(nodeView, camera).update();
}
function getLayout() {
return layout;
}
function stable(stableValue) {
if (stableValue === undefined) return isStable;
isStable = stableValue;
api.fire('stable', isStable);
}
function graphInternal(newGraph) {
if (newGraph !== undefined) throw new Error('Not implemented, Anvaka, do it!');
return graph;
}
function normalizeColor(color) {
if (color === undefined) return color;
var colorType = typeof color;
if (colorType === 'number') return color;
if (colorType === 'string') return parseStringColor(color);
if (color.length === 3) return (color[0] << 16) | (color[1] << 8) | (color[2]);
throw new Error('Unrecognized color type: ' + color);
}
function parseStringColor(color) {
if (color[0] === '#') {
return Number.parseInt(color.substring(1), 16);
}
return Number.parseInt(color, 16);
}
function focus() {
var sceneElement = renderer && renderer.domElement;
if (sceneElement && typeof sceneElement.focus === 'function') sceneElement.focus();
}
function showNode(nodeId, stopDistance) {
stopDistance = typeof stopDistance === 'number' ? stopDistance : 100;
flyTo(camera, layout.getNodePosition(nodeId), stopDistance);
}
}
function verifyContainerDimensions(container) {
if (!container) {
throw new Error('container is required for the renderer');
}
if (container.clientWidth <= 0 || container.clientHeight <= 0) {
console.warn('Container is not visible. Make sure to set width/height to see the graph');
}
}