-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgun2d3force.js
More file actions
87 lines (72 loc) · 2.03 KB
/
gun2d3force.js
File metadata and controls
87 lines (72 loc) · 2.03 KB
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
/**
* This function load a *gun* node into a data object like {nodes: [], links: []}
*
* @param {} gunref Gun reference
* @param {} data Object that store the nodes and links
* @param Function render Render function called when data changes
* @param Number debounce_time Debounce to call the render function
*/
function gun2d3force(gunref, data, render, debounce_time) {
const NODES_LIMIT = 100;
const LINKS_LIMIT = 100;
debounce_time = debounce_time || 1500;
data.nodes = [], data.links = [];
let nodes = {};
let last_render;
let superRender = function() {
if (!last_render || typeof render === 'function' && (Date.now() - last_render) > debounce_time) {
last_render = Date.now();
render();
}
};
const addNode = (at, x) => {
let soul = at.$._.link || at.$._.soul;
if (!soul) {
/// The attributes are here. Not renderized.
return;
}
if (data.nodes.length > NODES_LIMIT) {
//console.log('NODE LIMIT!!! NODE #%s NODES.LENGTH (%s) > NODE_LIMIT (%s)', soul, data.nodes.length, NODE_LIMIT);
return;
}
if (data.links.length > LINKS_LIMIT) {
//console.log('NODE LIMIT!!! NODE #%s NODES.LENGTH (%s) > NODE_LIMIT (%s)', soul, data.nodes.length, NODE_LIMIT);
return;
}
let parentSoul = null;
if (at.via && (at.via.$._.soul || at.via.$._.link)) {
parentSoul = at.via.$._.soul || at.via.$._.link;
}
if (parentSoul && !nodes[parentSoul]) {
let nodeParent = {
id: parentSoul,
group: 2,
at: at.via
};
nodes[nodeParent.id] = nodeParent;
data.nodes.push(nodeParent);
}
if (parentSoul) {
data.links.push({
source: parentSoul,
target: soul,
label: at.get,
group: 5,
value: 3
});
}
let n = nodes[soul];
if (!n) {
n = {
id: soul,
group: 3,
at
};
nodes[soul] = n;
data.nodes.push(nodes[soul]);
}
at.$.map().get(addNode);
superRender();
};
gunref.map().get(addNode);
};