Scorch/static/scorch.js

135 lines
3.7 KiB
JavaScript

var worker;
function load() {
init_worker();
document.querySelector('#player').addEventListener('ended', function() {
if (document.querySelector('#shuffle').checked) {
ww_send_event('random_track', {});
}
});
}
function init_worker() {
worker = new Worker('/static/scorchWorker.js');
worker.onmessage = ww_onmessage;
worker.events = {};
worker.events['artist_albums'] = artist_albums_recv;
worker.events['album_tracks'] = album_tracks_recv;
worker.events['track'] = track_recv;
}
function ww_send_event(event_title, data) {
worker.postMessage([event_title, data]);
}
function ww_onmessage(event) {
let data;
let event_title;
try {
data = JSON.parse(event.data);
} catch(err) {
// not JSON
console.log(err);
console.log(event);
throw new Error("Error decoding JSON");
return;
}
if (!data.ok) {
throw new Error("Socket error: event = " + event_title + ", error = " + data.error);
}
try {
event_title = data.event;
data = data.data;
} catch(err) {
// not proper event
console.log(err);
console.log(event);
throw new Error("Event malformed");
return;
}
if (worker.events[event_title] === undefined) {
console.log("Unknown socket event: " + event_title);
return;
}
//console.log("Received `" + event_title + "` event with following data: " + JSON.stringify(data));
worker.events[event_title](data);
}
/* Websocket receive */
function artist_albums_recv(data) {
let albums_list = document.querySelector('#albumList');
while (albums_list.firstChild) {
albums_list.removeChild(albums_list.lastChild);
}
let track_list = document.querySelector('#trackList');
while (track_list.firstChild) {
track_list.removeChild(track_list.firstChild);
}
for (let album of data) {
let option = document.createElement('option');
option.value = album['album'];
option.innerText = album['album'];
option.dataset['date'] = album['date']
albums_list.appendChild(option);
}
}
function album_tracks_recv(data) {
let track_list = document.querySelector('#trackList');
while (track_list.firstChild) {
track_list.removeChild(track_list.lastChild);
}
for (let track of data) {
let option = document.createElement('option');
option.value = track['title'];
option.innerText = track['discnumber'] + '.' + track['tracknumber'] + ' - ' + track['title'];
option.dataset['date'] = track['date']
option.dataset['discnumber'] = track['discnumber']
option.dataset['tracknumber'] = track['tracknumber']
track_list.appendChild(option);
}
}
function track_recv(data) {
document.querySelector('#nowPlayingArtist').innerText = data['albumartist'];
document.querySelector('#nowPlayingAlbum').innerText = data['album'];
document.querySelector('#nowPlayingTitle').innerText = data['title'];
document.title = 'Scorch | ' + data['artist'] + ' - ' + data['title'];
let player = document.querySelector('#player');
player.firstElementChild.src = data['url'];
player.load();
player.play();
document.querySelector('#albumCover').firstChild.src = data['coverart'];
}
/* Websocket send */
function select_artist(select) {
let data = {
'artist': select.value,
};
ww_send_event('select_artist', data);
}
function select_album(select) {
let data = {
'artist': document.querySelector('#artistList').value,
'album': select.value,
'date': select.selectedOptions[0].dataset['date'],
};
ww_send_event('select_album', data);
}
function select_track(select) {
let data = {
'artist': document.querySelector('#artistList').value,
'album': document.querySelector('#albumList').value,
'date': select.selectedOptions[0].dataset['date'],
'discnumber': select.selectedOptions[0].dataset['discnumber'],
'tracknumber': select.selectedOptions[0].dataset['tracknumber'],
};
ww_send_event('select_track', data);
}