[ai] fixed web server issues, still some quirky behaviour. More robust.

This commit is contained in:
2025-08-18 00:20:59 +02:00
parent b820f3fc8d
commit 1144b95349
4 changed files with 211 additions and 129 deletions

View File

@@ -1,8 +1,59 @@
setInterval(getState, 4000);
setInterval(updateProgress, 500); // Update progress every second
// Get the <li> elements
var liElements = document.querySelectorAll('ul li');
/* Dynamic content loaders for playlist and mapping (avoid heavy template processing on server) */
function bindPlaylistClicks() {
var container = document.getElementById('playlistContainer');
if (!container) return;
container.onclick = function(e) {
var li = e.target.closest('li');
if (!li || !container.contains(li)) return;
var id = li.dataset && li.dataset.id;
if (id) playSongById(id);
};
}
function loadDirectory() {
var container = document.getElementById('playlistContainer');
if (!container) return;
var xhr = new XMLHttpRequest();
xhr.open('GET', '/directory', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
container.innerHTML = xhr.responseText || '';
bindPlaylistClicks();
} else {
container.innerHTML = '<div class="hint">Failed to load playlist.</div>';
}
}
};
xhr.send();
}
function loadMapping() {
var el = document.getElementById('mappingList');
if (!el) return;
var xhr = new XMLHttpRequest();
xhr.open('GET', '/mapping', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status >= 200 && xhr.status < 300) {
el.innerHTML = xhr.responseText || '';
} else {
el.innerHTML = '<div class="hint">Failed to load mapping.</div>';
}
}
};
xhr.send();
}
/* Kick off dynamic loads on DOM ready */
document.addEventListener('DOMContentLoaded', function() {
loadDirectory();
loadMapping();
getState();
});
var lastChange = 0;
@@ -24,13 +75,7 @@ function formatTime(totalSec) {
}
// Add click event listener to each <li> element
liElements.forEach(function(li) {
li.addEventListener('click', function() {
//var liText = this.innerText;
var id = this.dataset.id;
playSongById(id);
});
});
/* Clicks are handled via event delegation in bindPlaylistClicks() */
function simpleGetCall(endpoint) {
var xhr = new XMLHttpRequest();