Musik/static/musik.js

82 lines
2.9 KiB
JavaScript

function load() {
// track has ended
document.getElementById('player').addEventListener('ended', function() {
// shuffle
if (document.getElementById('shuffle').checked) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText);
change_track(track);
};
httpRequest.open('GET', api_uri + '/random', true);
httpRequest.send();
}
});
}
function select_artist(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option value="' + nav_items[i] + '">' + nav_items[i] + '</option>';
}
document.getElementById('albumList').innerHTML = html_str;
document.getElementById('trackList').innerHTML = '';
};
httpRequest.open('GET', api_uri + '?artist=' + select.value, true);
httpRequest.send();
}
function select_album(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += '<option value="' + nav_items[i].replace(/\d* - /, '') + '">' + nav_items[i] + '</option>';
}
document.getElementById('trackList').innerHTML = html_str;
};
httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + select.value, true);
httpRequest.send();
}
function select_track(select) {
var httpRequest;
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = JSON.parse(httpRequest.responseText);
change_track(track);
};
httpRequest.open('GET', api_uri + '?artist=' + document.getElementById('artistList').value + '&album=' + document.getElementById('albumList').value + '&track=' + select.value, true);
httpRequest.send();
}
function change_track(track) {
let source = document.getElementById('stream');
source.src = track.streampath;
let player = document.getElementById('player');
player.load();
player.play();
document.getElementById('nowPlayingArtist').innerHTML = track.artist;
document.getElementById('nowPlayingAlbum').innerHTML = track.album;
document.getElementById('nowPlayingTitle').innerHTML = track.title;
document.getElementById('albumCover').firstChild.src = track.coverart;
}