mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-09-26 20:31:11 +08:00
Code refactoring after #1196
This commit is contained in:
@@ -25,73 +25,56 @@
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="network"></div>
|
||||
<script src="main.js"></script>
|
||||
<script>
|
||||
<div id="network"></div>
|
||||
<script src="main.js"></script>
|
||||
<script>
|
||||
/* global vis */
|
||||
window.addEventListener('load', () => {
|
||||
const url = new URL('api/streams.dot' + location.search, location.href);
|
||||
|
||||
const container = document.getElementById('network');
|
||||
const options = {
|
||||
edges: {
|
||||
font: {align: 'middle'},
|
||||
smooth: false,
|
||||
},
|
||||
nodes: {shape: 'box'},
|
||||
physics: false,
|
||||
};
|
||||
|
||||
let network;
|
||||
let nodes = new vis.DataSet();
|
||||
let edges = new vis.DataSet();
|
||||
let seed = "";
|
||||
/* global vis */
|
||||
window.addEventListener('load', () => {
|
||||
const url = new URL('api/streams.dot' + location.search, location.href);
|
||||
const options = {
|
||||
edges: {
|
||||
font: { align: 'middle' },
|
||||
smooth: false,
|
||||
},
|
||||
nodes: { shape: 'box' },
|
||||
physics: {
|
||||
enabled: false,
|
||||
stabilization: {
|
||||
enabled: false,
|
||||
}
|
||||
},
|
||||
autoResize: true,
|
||||
locale: navigator.language.toLowerCase().split('-').slice(0, 2).join('-'),
|
||||
};
|
||||
const container = document.getElementById('network');
|
||||
|
||||
async function update() {
|
||||
try {
|
||||
const response = await fetch(url, {cache: 'no-cache'});
|
||||
const dotData = await response.text();
|
||||
const data = vis.parseDOTNetwork(dotData);
|
||||
|
||||
async function fetchDataAndUpdateNetwork() {
|
||||
try {
|
||||
const response = await fetch(url, { cache: 'no-cache' });
|
||||
const dotData = await response.text();
|
||||
const data = vis.parseDOTNetwork(dotData);
|
||||
if (!network) {
|
||||
network = new vis.Network(container, data, options);
|
||||
network.storePositions();
|
||||
} else {
|
||||
const positions = network.getPositions();
|
||||
const viewPosition = network.getViewPosition();
|
||||
const scale = network.getScale();
|
||||
|
||||
if (!network) {
|
||||
nodes = new vis.DataSet(data.nodes);
|
||||
edges = new vis.DataSet(data.edges);
|
||||
network = new vis.Network(container, { nodes, edges }, options);
|
||||
network.storePositions();
|
||||
seed = network.getSeed();
|
||||
} else {
|
||||
const positions = network.getPositions();
|
||||
const viewState = network.getViewPosition();
|
||||
const scale = network.getScale();
|
||||
network.setOptions({layout: {
|
||||
randomSeed: seed
|
||||
}})
|
||||
network.setData(data);
|
||||
network.setData(data);
|
||||
|
||||
for (const nodeId in positions) {
|
||||
if (positions.hasOwnProperty(nodeId)) {
|
||||
network.moveNode(nodeId, Math.floor(positions[nodeId].x), Math.floor(positions[nodeId].y));
|
||||
}
|
||||
}
|
||||
|
||||
network.moveTo({ position: viewState, scale: scale });
|
||||
for (const nodeId in positions) {
|
||||
network.moveNode(nodeId, positions[nodeId].x, positions[nodeId].y);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching or updating network data:', error);
|
||||
network.moveTo({position: viewPosition, scale: scale});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching or updating network data:', error);
|
||||
}
|
||||
|
||||
fetchDataAndUpdateNetwork();
|
||||
setTimeout(update, 5000);
|
||||
}
|
||||
|
||||
setInterval(fetchDataAndUpdateNetwork, 5000);
|
||||
});
|
||||
</script>
|
||||
update();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user