-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexTemplate.html
504 lines (480 loc) · 15.2 KB
/
indexTemplate.html
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<html>
<head>
<title>VisJS Network Graph</title>
<script src="vis-network.min.js"></script>
<style type="text/css">
html, body, .maindiv, svg { width: 100%; height: 100%; background-color: black; overflow: hidden; overflow-x: hidden}
#mynetwork {
width: 100%;
height: 100vh;
background-color: #000;
}
#searchdiv {
z-index: 20;
position: absolute;
padding: 10px;
text-align: center;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
max-width: max-content;
min-width: min(410px, 90%);
overflow-y: auto;
justify-content: center;
top: 10px;
background-color: rgba(0, 0, 0, 0.8);
}
#searchresults {
display: block;
max-height: 325px;
overflow-y: auto;
padding-left: 10px;
padding-right: 10px;
}
#infodiv {
z-index: 10;
position: absolute;
padding: 10px;
left: 30px;
bottom: 10px;
background-color: rgba(0, 0, 0, 0.8);
margin-right: 70vw;
}
/* Media query for phone screens */
@media (max-width: 600px) {
#infodiv {
margin-right: 0;
}
}
/* Solid border */
hr.solid {
border-top: 3px solid #fff;
}
.description-container {
max-height: 40vh;
overflow-y: auto;
padding-right: 25px;
overflow-wrap: break-word;
color: white;
text-align: left;
font-family: 'Courier New', Courier, monospace;
white-space: pre-line;
}
p, span, ul, li, a {
white-space: pre-line;
color: white;
text-align: left;
font-family: 'Courier New', Courier, monospace;
}
.progress-bar {
position: absolute;
display: none;
top: 45%;
bottom: 45%;
left: 30%;
right: 30%;
width: 40%;
height: 10%;
z-index: 10;
}
.vis-label {
position: absolute;
z-index: 1;
text-align: center;
bottom: -10px;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div class="maindiv" id="mynetwork"></div>
<div id="searchdiv">
<input style="width: min(400px, 80%); margin: 5px" id="searchinput" type="text" placeholder="Search nodes">
<div id="searchresults" style="display: block; max-height: 325px; overflow-y: auto; padding-left: 10px; padding-right: 10px;"></div>
</div>
<div id="infodiv" style="display: inline;">
<p id="infotext"></p>
</div>
<progress class="progress-bar" id="loadingbar" value="0" max="10000" ellipse=""></progress>
<script type="text/javascript">
// Sample data
const nodes = {{ nodes | safe }};
const edges = {{ edges | safe }};
// Create a new network instance
var container = document.getElementById("mynetwork");
var data = {
nodes: nodes,
edges: edges
};
var options = {
layout: {
randomSeed: 0,
//clusterThreshold: 25
},
interaction: {
hideEdgesOnZoom: true,
hideEdgesOnDrag: true,
selectConnectedEdges: false
},
nodes: {
shape: "square",
size: 10,
font: {
face: 'courier new',
size: 5,
color: '#ffffff',
strokeWidth: 0.5,
strokeColor: '#000000'
}
},
edges: {
width: 1.25,
selectionWidth: 4,
smooth: false
},
physics: {
enabled: true, // Enable physics simulation initially
barnesHut: {
gravitationalConstant: -2000,
centralGravity: 1.1,
springLength: 80,
springConstant: 0.03,
damping: 0.10,
avoidOverlap: 1 // Adjust the overlap avoidance strength as needed
},
stabilization: {
enabled: true,
iterations: 500,
updateInterval: 10
},
repulsion: {
nodeDistance: 200, // Increase the repulsion distance between nodes
springLength: 100, // Maintain spring length for other nodes
centralGravity: 0.1,
damping: 0.09
},
},
groups: {
user: {
shape: "image",
borderWidth: 2,
size: 15,
shapeProperties: {
useBorderWithImage: true,
},
},
video: {
shape: "square",
size: 20,
//color: "#2B7CE9", // blue
},
thread: {
shape: "triangle",
//color: "#5A1E5C", // purple
},
},
};
network = new vis.Network(container, data, options);
// Update progress bar
network.on("stabilizationProgress", function (params) {
document.getElementById("loadingbar").setAttribute('value', (10000 * params.iterations) / params.total);
});
// Disable progress bar after load
network.on("stabilizationIterationsDone", function (params) {
document.getElementById("loadingbar").style.display = 'none';
document.getElementById('infodiv').style.display = 'none';
network.setOptions({
physics: {
enabled: false
}
});
});
// Disable selecting edges
network.on('click', handleSelect);
network.on('dragStart', handleDrag);
network.on('dragEnd', handleDragEnd);
network.on('oncontext', (event) => {
event.event.preventDefault();
if(lastSelectedNode === undefined) {
return;
}
const node = network.getNodeAt(event.pointer.DOM);
if(node === undefined || node.id === lastSelectedNode) {
return;
}
selectShortestPath(lastSelectedNode, node);
})
// Add event listener for node click
network.on("click", function (params) {
const infodiv = document.getElementById("infodiv");
const infotext = document.getElementById("infotext");
if (params.nodes.length > 0) {
const nodeId = params.nodes[0];
const node = nodes.find((n) => n.id === nodeId);
if (node) {
//console.log(nodeId)
setNodeInformation(nodeId)
//infotext.textContent = node.title;
infodiv.style.display = "inline";
}
} else {
infodiv.style.display = "none";
}
});
var lastSelectedNode = undefined;
function handleDrag(event) {
var nodeID = network.getNodeAt(event.pointer.DOM);
if (nodeID !== undefined) {
lastSelectedNode = nodeID;
network.setSelection({
nodes: [nodeID]
});
}
}
function handleDragEnd(event) {
if(event.nodes.length > 0) {
lastSelectedNode = undefined;
handleSelect(event);
}
}
function handleSelect(event) {
var nodeID = network.getNodeAt(event.pointer.DOM);
if (nodeID === undefined || (event.nodes.length === 0 && event.edges.length > 0)) {
lastSelectedNode = undefined;
network.setSelection({
nodes: [],
edges: []
})
setNodeInformation(false);
} else {
var nodeEdges = network.nodesHandler.body.nodes[event.nodes[0]].edges;
lastSelectedNode = nodeID;
network.setSelection({
nodes: nodeEdges.flatMap(e => [e.fromId, e.toId]).concat([nodeID]),
edges: nodeEdges.map(e => e.id)
})
setNodeInformation(event.nodes[0]);
}
}
function getShortestPathNodes(start, end) {
const startMap = new Map([[start, null]]);
const endMap = new Map([[end, null]]);
let startNodesToProcess = [start];
let endNodesToProcess = [end];
let link = undefined;
let i = 1;
const updateMap = (nodeID, ourMap, theirMap) => {
const node = network.nodesHandler.body.nodes[nodeID];
let newNodes = []
for(const edge of node.edges) {
const otherNodeID = edge.fromId === nodeID ? edge.toId : edge.fromId;
if(ourMap.get(otherNodeID) === undefined) {
ourMap.set(otherNodeID, nodeID);
newNodes.push(otherNodeID);
}
}
return newNodes;
}
const getOutput = (treeMidID) => {
let output = [];
let start = [];
let end = []
for(let n = treeMidID; n !== undefined; n = startMap.get(n)) {
if(n !== null) {
output.unshift(n);
}
}
for(let n = endMap.get(treeMidID); n !== undefined; n = endMap.get(n)) {
if(n !== null) {
output.push(n);
}
}
return output;
}
while(startNodesToProcess.length > 0 || endNodesToProcess.length > 0) {
let newStartNodes = [];
let newEndNodes = [];
for(const nodeID of startNodesToProcess) {
newStartNodes.push(...updateMap(nodeID, startMap, endMap))
}
for(const nodeID of newStartNodes) {
if(endMap.get(nodeID) !== undefined) {
let output = [];
output.push(...getOutput(nodeID));
return output;
}
}
for(const nodeID of endNodesToProcess) {
newEndNodes.push(...updateMap(nodeID, endMap, startMap));
}
for(const nodeID of newEndNodes) {
if(startMap.get(nodeID) !== undefined) {
let output = [];
output.push(...getOutput(nodeID));
return output;
}
}
startNodesToProcess = newStartNodes;
endNodesToProcess = newEndNodes;
i++;
}
return null;
}
function selectShortestPath(start, end) {
const nodeIDs = getShortestPathNodes(start, end);
if(nodeIDs === null) {
return;
}
const edgeIDs = [];
for(let i = 0; i < nodeIDs.length - 1; i++) {
const node = network.nodesHandler.body.nodes[nodeIDs[i]];
const nextNode = network.nodesHandler.body.nodes[nodeIDs[i+1]];
const startNode = (node.edges.length <= nextNode.edges.length ? node : nextNode);
const endNode = (node.edges.length <= nextNode.edges.length ? nextNode : node);
const edge = startNode.edges.find(edge => {
return edge.fromId === endNode.id || edge.toId === endNode.id;
})
edgeIDs.push(edge.id);
}
network.setSelection({
nodes: nodeIDs,
edges: edgeIDs
})
}
function setNodeInformation(nodeID) {
if(nodeID === false) {
document.getElementById('infodiv').style.display = 'none';
return;
}
document.getElementById('infodiv').style.display = 'inline';
var nodeOptions = network.nodesHandler.body.nodes[nodeID].options;
var edges = network.nodesHandler.body.nodes[nodeID].edges;
var textElement = document.getElementById('infotext');
var string = "";
//console.log(nodeOptions) // user, thread, video TODO: ADD IF STATEMENT AND ADD ALL THE NODE INFO AND LINKS HERE
if(nodeOptions.group === "user") {
string += `<a href="${nodeOptions.url}" target="_blank">${nodeOptions.label}</a>`;
string += `<p>Pictures:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.profilepictures.join("<hr class=\"solid\">\n\t");
string += `</span></div></p>`;
string += `<p>Usernames:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.usernames.join("\n\t");
string += `</span></div></p>`;
if (nodeOptions.description !== "") {
string += `<p>Description:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.description;
string += `</span></div></p>`;
}
} else if(nodeOptions.group === "thread"){
string += `<a href="${nodeOptions.url}" target="_blank">${nodeOptions.label}</a>`;
if (nodeOptions.description !== "") {
string += `<p>Description:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.description;
string += `</span></div></p>`;
}
if (nodeOptions.tags[0] !== undefined) {
string += `<p>Tags:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.tags.join("\n\t");
string += `</span></div></p>`;
}
} else if(nodeOptions.group === "video"){
string += `<a href="${nodeOptions.url}" target="_blank">${nodeOptions.label}</a>`;
if (nodeOptions.titles[1] !== null) {
string += `<p>Titles:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.titles.join("\n\t");
string += `</span></div></p>`;
}
string += `<p>Total Views:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.views;
string += `</span></div></p>`;
string += `<p>Total Comments:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.commentcount;
string += `</span></div></p>`;
string += `<p>Threads:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.threadids.join("\n\t");
string += `</span></div></p>`;
if (nodeOptions.description[1] !== undefined) {
string += `<p>Descriptions:</p>`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.description.join("<hr class=\"solid\">\n\t");
string += `</span></div>`;
} else{
string += `<p>Description:\n`;
string += `<div style="max-height: min(600px, 40vh, 30vw); overflow-y: auto; padding-right: 25px"><span>`;
string += "\t" + nodeOptions.description.join("\n\t");
string += `</span></div></p>`;
}
} else{
string += `<p>${nodeOptions.label}</p>`;
}
textElement.innerHTML = string;
}
function focusOn(nodeID) {
var nodeEdges = network.nodesHandler.body.nodes[nodeID].edges;
var position = network.getPositions([nodeID])[nodeID];
lastSelectedNode = nodeID;
network.setSelection({
nodes: nodeEdges.flatMap(e => [e.fromId, e.toId]).concat([nodeID]),
edges: nodeEdges.map(e => e.id)
})
setNodeInformation(nodeID);
network.focus(nodeID, {locked: false, scale: 4, animation: true, offset: {x: 0, y: 0}});
//network.moveTo({ position, scale: 5, animation: true });
}
// Search functionality
const searchInput = document.getElementById("searchinput");
const searchResults = document.getElementById("searchresults");
const myNetworkDiv = document.getElementById("mynetwork");
window.addEventListener("load", function() {
performSearch(); // Trigger the search functionality when the page finishes loading
});
searchInput.addEventListener("input", function (event) {
performSearch();
});
myNetworkDiv.addEventListener("focusin", function () {
searchResults.style.display = "none";
});
searchInput.addEventListener("focus", function () {
if (searchInput.value) {
searchResults.style.display = "block";
}
});
function performSearch() {
const searchText = searchInput.value.toLowerCase();
const filteredNodes = nodes.filter(
(node) =>
node.label.toLowerCase().includes(searchText) ||
node.id.toLowerCase().includes(searchText)
);
displaySearchResults(filteredNodes);
}
function displaySearchResults(filteredNodes) {
searchResults.innerHTML = "";
filteredNodes.forEach(function (node) {
const resultItem = document.createElement("div");
resultItem.textContent = node.label;
resultItem.style.color = "#ffffff" // TODO: Change result color depending on type
resultItem.addEventListener("click", function () {
const nodeId = node.id;
focusOn(nodeId); // Select, Move to, Zoom in
//network.moveTo({ position, scale: 5, animation: true });
});
searchResults.appendChild(resultItem);
});
searchResults.style.display = filteredNodes.length > 0 ? "block" : "none";
}
</script>
</body>
</html>