Musik/static/musik.js

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-09-14 08:02:24 -04:00
function load() {
2018-09-20 10:39:23 -04:00
// track has ended
2018-09-14 08:02:24 -04:00
document.getElementById('player').addEventListener('ended', function() {
2018-09-20 10:39:23 -04:00
// shuffle
if (document.getElementById('shuffle').checked) {
2019-01-11 11:27:25 -05:00
ajax('/musik/get_shuffle', function() {
2018-09-20 10:39:23 -04:00
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
let track = httpRequest.responseText;
change_track(track);
2019-01-11 11:27:25 -05:00
});
2018-09-14 08:02:24 -04:00
}
});
}
2019-01-11 11:27:25 -05:00
var httpRequest;
function ajax(url, func) {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = func;
httpRequest.open('GET', url, true);
httpRequest.send();
}
2018-09-13 14:22:27 -04:00
function navigate(item) {
if (/\..{3,5}$/.test(item)) {
2018-09-14 08:02:24 -04:00
select_track(item);
2018-09-13 14:22:27 -04:00
}
else {
get_dir(item);
}
}
2018-09-14 08:02:24 -04:00
function select_track(item) {
2018-09-13 14:22:27 -04:00
let cd = document.getElementById('currentDirectory').innerText;
let track = cd + item;
2018-09-14 08:02:24 -04:00
change_track(track);
}
function change_track(track) {
2018-09-13 14:22:27 -04:00
let source = document.getElementById('stream');
2019-01-11 11:27:25 -05:00
source.src = '/musik/stream' + encodeURIComponent(track);
2018-09-13 14:22:27 -04:00
let player = document.getElementById('player');
player.load();
player.play();
document.getElementById('nowPlaying').innerHTML = track;
2018-09-20 10:39:23 -04:00
let arr = track.split('/');
2018-09-20 10:39:23 -04:00
let art = document.getElementById('albumCover');
art.firstChild.src = '/musik/album_cover' + arr.slice(0, arr.length-1).join("/") + '/folder.jpg';
2018-09-13 14:22:27 -04:00
}
function get_dir(item) {
let cd = document.getElementById('currentDirectory').innerText;
2019-01-11 11:27:25 -05:00
if (item === "../") {
if (cd !== "/") {
item = cd.slice(0, cd.slice(0, -1).lastIndexOf("/")+1);
}
else {
return;
}
}
else {
item = cd + item;
}
ajax('/musik/get_dir/' + item, update_nav_items);
2018-09-13 14:22:27 -04:00
}
function update_nav_items() {
if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; }
if (httpRequest.status !== 200) { return; }
nav_items = JSON.parse(httpRequest.responseText);
document.getElementById('currentDirectory').innerText = nav_items.shift();
let html_str = '';
for (let i = 0; i < nav_items.length; i++) {
html_str += `<li><a href="javascript:void(0);" onclick="navigate(\`${nav_items[i]}\`)">${nav_items[i]}</a></li>`;
2018-09-13 14:22:27 -04:00
}
document.getElementById('navItems').innerHTML = html_str;
2018-09-12 13:04:00 -04:00
}