Skip to content

Commit e6fa97c

Browse files
committed
Code refactoring after #1196
1 parent 5b481a2 commit e6fa97c

File tree

1 file changed

+41
-58
lines changed

1 file changed

+41
-58
lines changed

www/network.html

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -25,73 +25,56 @@
2525
</style>
2626
</head>
2727
<body>
28-
<div id="network"></div>
29-
<script src="main.js"></script>
30-
<script>
31-
let network;
32-
let nodes = new vis.DataSet();
33-
let edges = new vis.DataSet();
34-
let seed = "";
35-
/* global vis */
36-
window.addEventListener('load', () => {
37-
const url = new URL('api/streams.dot' + location.search, location.href);
38-
const options = {
39-
edges: {
40-
font: { align: 'middle' },
41-
smooth: false,
42-
},
43-
nodes: { shape: 'box' },
44-
physics: {
45-
enabled: false,
46-
stabilization: {
47-
enabled: false,
48-
}
49-
},
50-
autoResize: true,
51-
locale: navigator.language.toLowerCase().split('-').slice(0, 2).join('-'),
52-
};
53-
const container = document.getElementById('network');
28+
<div id="network"></div>
29+
<script src="main.js"></script>
30+
<script>
31+
/* global vis */
32+
window.addEventListener('load', () => {
33+
const url = new URL('api/streams.dot' + location.search, location.href);
5434

35+
const container = document.getElementById('network');
36+
const options = {
37+
edges: {
38+
font: {align: 'middle'},
39+
smooth: false,
40+
},
41+
nodes: {shape: 'box'},
42+
physics: false,
43+
};
5544

56-
async function fetchDataAndUpdateNetwork() {
57-
try {
58-
const response = await fetch(url, { cache: 'no-cache' });
59-
const dotData = await response.text();
60-
const data = vis.parseDOTNetwork(dotData);
45+
let network;
6146

62-
if (!network) {
63-
nodes = new vis.DataSet(data.nodes);
64-
edges = new vis.DataSet(data.edges);
65-
network = new vis.Network(container, { nodes, edges }, options);
66-
network.storePositions();
67-
seed = network.getSeed();
68-
} else {
69-
const positions = network.getPositions();
70-
const viewState = network.getViewPosition();
71-
const scale = network.getScale();
72-
network.setOptions({layout: {
73-
randomSeed: seed
74-
}})
75-
network.setData(data);
47+
async function update() {
48+
try {
49+
const response = await fetch(url, {cache: 'no-cache'});
50+
const dotData = await response.text();
51+
const data = vis.parseDOTNetwork(dotData);
7652

77-
for (const nodeId in positions) {
78-
if (positions.hasOwnProperty(nodeId)) {
79-
network.moveNode(nodeId, Math.floor(positions[nodeId].x), Math.floor(positions[nodeId].y));
80-
}
81-
}
53+
if (!network) {
54+
network = new vis.Network(container, data, options);
55+
network.storePositions();
56+
} else {
57+
const positions = network.getPositions();
58+
const viewPosition = network.getViewPosition();
59+
const scale = network.getScale();
8260

83-
network.moveTo({ position: viewState, scale: scale });
61+
network.setData(data);
62+
63+
for (const nodeId in positions) {
64+
network.moveNode(nodeId, positions[nodeId].x, positions[nodeId].y);
8465
}
8566

86-
} catch (error) {
87-
console.error('Error fetching or updating network data:', error);
67+
network.moveTo({position: viewPosition, scale: scale});
8868
}
69+
} catch (error) {
70+
console.error('Error fetching or updating network data:', error);
8971
}
9072

91-
fetchDataAndUpdateNetwork();
73+
setTimeout(update, 5000);
74+
}
9275

93-
setInterval(fetchDataAndUpdateNetwork, 5000);
94-
});
95-
</script>
76+
update();
77+
});
78+
</script>
9679
</body>
97-
</html>
80+
</html>

0 commit comments

Comments
 (0)