-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (220 loc) · 8.03 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
import { parseNodes,
parseLinks,
formatSats } from './utils.js';
import { drag,
redrawNodes,
simTick } from './d3.helper.js';
import { longPressLink,
longPressNode } from './event.handlers.js';
let firstResponse;
//const serviceBase = 'http://localhost:8089/graph/nodes';
const serviceBase = 'http://206.189.143.99:8089/graph/nodes';
const WIDTH = document.getElementById('renderBox').clientWidth;
const HEIGHT = document.getElementById('renderBox').clientHeight;
const CENTERX = WIDTH / 2;
const CENTERY = HEIGHT / 2;
const isMobile = WIDTH < 640;
let nodes = [];
let links = [];
let vis = null;
let depth = 1
const lastDepth = 3;
let sim;
let apiResp;
let graphResp;
let breadCrumb = [''];
document.getElementById('resetBtn').addEventListener('click', () => {
depth = 1;
nodes = [];
links = [];
graphResp = firstResponse;
nodes.push(...graphResp.nodes.map(n => {
return {
...n,
salt: Math.random()
}
}));
breadCrumb = [''];
document.getElementById('breadCrumb').innerHTML = breadCrumb.join(' <b> > </b> ');
vis.selectAll('.node').data([]).exit().remove();
vis.selectAll('.link').data([]).exit().remove();
vis.selectAll('.lineText').data([]).exit().remove();
setTimeout(() => {
const simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength( 0 ))
.force('link', d3.forceLink(links))
.force('center', d3.forceCenter(CENTERX, CENTERY));
sim = simulation;
const { node, container, link, line, lineText } = redrawNodes(vis, simulation, nodes, links,
isMobile, depth, lastDepth, radius,
circleStroke, circleColor, primaryColor, grayColor,
genNodeText, genNodeCountText, click);
simTick(simulation, container, line, lineText);
}, 100);
});
const click = async (d) => {
const selectedNode = d.srcElement.__data__;
const pub_key = selectedNode.pub_key;
//console.log('click: ', selected, selectedNode, index);
nodes = [];
links = [];
if (depth === 1) {
graphResp = {
nodes: parseNodes(selectedNode.children),
links: []
};
nodes.push(...graphResp.nodes);
links.push(...graphResp.links);
depth++;
breadCrumb.push(`${formatSats(selectedNode.startSats)} to ${formatSats(selectedNode.endSats)}`);
document.getElementById('breadCrumb').innerHTML = breadCrumb.join(' <b> > </b> ');
} else if (depth < lastDepth) {
graphResp = {
nodes: parseNodes(selectedNode.children),
links: []
};
nodes.push(...graphResp.nodes);
links.push(...graphResp.links);
depth++;
breadCrumb.push(`${formatSats(selectedNode.startSats)} to ${formatSats(selectedNode.endSats)}`);
document.getElementById('breadCrumb').innerHTML = breadCrumb.join(' <b> > </b> ');
} else if (depth <= lastDepth) {
graphResp = {
nodes: selectedNode.children,
links: []
};
nodes.push(...graphResp.nodes);
links.push(...graphResp.links);
depth++;
breadCrumb.push(`${formatSats(selectedNode.startSats)} to ${formatSats(selectedNode.endSats)}`);
document.getElementById('breadCrumb').innerHTML = breadCrumb.join(' <b> > </b> ');
} else if (depth === lastDepth + 1) {
document.getElementById('spinOverlay').style.display = 'block';
document.getElementById('spinSpinner').style.display = 'block';
const apiRawResp = await fetch(serviceBase, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"public_keys": [selectedNode.public_key]
})
});
const endNodeResp = await apiRawResp.json();
document.getElementById('spinOverlay').style.display = 'none';
document.getElementById('spinSpinner').style.display = 'none';
const lnks = parseLinks(endNodeResp);
nodes = [];
links = [];
graphResp = {
nodes: endNodeResp.nodes,
links: lnks
};
nodes.push(...graphResp.nodes);
links.push(...graphResp.links);
const selectedN = nodes.find(n => n.public_key === selectedNode.public_key);
selectedN.isCenter = true;
}
//console.log('click: ', nodes, links);
vis.selectAll('.node').data([]).exit().remove();
vis.selectAll('.link').data([]).exit().remove();
vis.selectAll('.lineText').data([]).exit().remove();
setTimeout(() => {
const simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength( strength ))
.force('link', d3.forceLink(links).distance(d => depth < lastDepth + 1 ? 30 : isMobile ? (60 + d.index * 3) : (90 + d.index * 4) ))
.force('center', d3.forceCenter(CENTERX, CENTERY));
sim = simulation;
const { node, container, link, line, lineText } = redrawNodes(vis, simulation, nodes, links,
isMobile, depth, lastDepth, radius,
circleStroke, circleColor, primaryColor, grayColor,
genNodeText, genNodeCountText, click);
simTick(simulation, container, line, lineText);
}, 100);
}
const selected = [];
const strength = (d) => {
if (depth > lastDepth) {
return isMobile ? -10 : -20;
} else {
return isMobile ? -25 : -50;
}
}
const radius = (d) => {
if (isMobile) {
return depth === lastDepth + 1 ? 15 : 35
} else {
return depth === lastDepth + 1 ? 28 : 45
}
};
const circleStroke = (d) => {
if (depth > lastDepth) {
return '#1EB5D2';
} else {
return '#BBBBBB';
}
}
const circleColor = (d) => {
if (depth > lastDepth) {
if (d.isCenter) {
return '#F79767';
} else {
return '#57C8E3';
}
} else {
return bucketColor;
}
};
const primaryColor = (d) => '#0d6efd';
const grayColor = (d) => '#666666';
const nodeColor = '#FFBBBB';
const bucketColor = '#DDDDDD';
const genNodeText = (d, st) => {
if (st === 0) {
if (depth === lastDepth + 1) {
return d.alias || d.public_key.substring(0, 12);
} else {
return formatSats(d.startSats);
}
} else {
if (depth === lastDepth + 1) {
return `${formatSats(d.capacity)} sats`;
} else {
return formatSats(d.endSats);
}
}
}
const genNodeCountText = (d) => {
if (depth === lastDepth + 1) {
return 'Capacity: ' + d.capacity;
} else {
return 'Count: ' + d.peers;
}
}
function init() {
vis = d3.select('#renderBox');
console.log('vis', vis);
const simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength( strength ))
.force('link', d3.forceLink(links))
.force('center', d3.forceCenter(CENTERX, CENTERY));
sim = simulation;
const { node, container, link, line, lineText } = redrawNodes(vis, simulation, nodes, links,
isMobile, depth, lastDepth, radius,
circleStroke, circleColor, primaryColor, grayColor,
genNodeText, genNodeCountText, click);
simTick(simulation, container, line, lineText);
}
setTimeout(async () => {
const lnNetworkRaw = await fetch('http://206.189.143.99:8089/graph/nodes');
firstResponse = {
nodes: parseNodes((await lnNetworkRaw.json()).nodes),
links: []
};
graphResp = firstResponse;
nodes.push(...graphResp.nodes);
links.push(...graphResp.links);
document.getElementById('spinOverlay').style.display = 'none';
document.getElementById('spinSpinner').style.display = 'none';
init();
}, 100);