deemix-webui/public/js/downloadList.js

118 lines
4.1 KiB
JavaScript
Raw Normal View History

2020-04-10 18:00:42 +02:00
var queueList = {}
var queue = []
2020-04-13 19:22:34 +02:00
socket.on("init_downloadQueue", function(data){
if (data.currentItem){
addToQueue(data['queueList'][data.currentItem])
$('#bar_' + data.currentItem).removeClass('indeterminate').addClass('determinate')
$('#bar_' + data.currentItem).css('width', data['queueList'][data.currentItem].progress + '%')
if (queueList[data.currentItem].failed >= 1){
$("#download_"+data.currentItem+" .download_info_status").append(`<span class="secondary-text inline-flex"><span class="download_slim_separator">(</span><span class="queue_failed">${queueList[data.currentItem].failed}</span><i class="material-icons">error_outline</i><span class="download_slim_separator">)</span></span>`)
}
}
2020-04-13 19:22:34 +02:00
data['queue'].forEach(item=>{
addToQueue(data['queueList'][item])
})
})
function addToQueue(queueItem){
2020-04-10 18:00:42 +02:00
queueList[queueItem.uuid] = queueItem
queue.push(queueItem.uuid)
$("#download_list").append(
`<div class="download_object" id="download_${queueItem.uuid}" data-deezerid="${queueItem.id}">
<div class="download_info">
<img width="75px" class="rounded coverart" src="${queueItem.cover}" alt="Cover ${queueItem.title}"/>
<div class="download_info_data">
<span class="download_line">${queueItem.title}</span> <span class="download_slim_separator"> - </span>
<span class="secondary-text">${queueItem.artist}</span>
</div>
<div class="download_info_status">
<span class="download_line"><span class="queue_downloaded">${queueItem.downloaded + queueItem.failed}</span>/${queueItem.size}</span>
2020-04-10 18:00:42 +02:00
</div>
</div>
2020-04-11 15:43:59 +02:00
<div class="download_bar">
<div class="progress"><div id="bar_${queueItem.uuid}" class="indeterminate"></div></div>
<i onclick="downloadAction(event)" class="material-icons queue_icon" data-uuid="${queueItem.uuid}">remove</i>
</div>
2020-04-10 18:00:42 +02:00
</div>`)
2020-04-13 19:22:34 +02:00
}
socket.on("addedToQueue", function(queueItem){
addToQueue(queueItem)
})
2020-04-11 15:43:59 +02:00
function downloadAction(evt){
let icon = $(evt.currentTarget).text()
let uuid = $(evt.currentTarget).data("uuid")
switch (icon) {
case 'remove':
socket.emit('removeFromQueue', uuid)
break;
default:
}
}
socket.on("removedFromQueue", function(uuid){
let index = queue.indexOf(uuid)
if (index > -1){
queue.splice(index, 1)
$(`#download_${queueList[uuid].uuid}`).remove()
delete queueList[uuid]
}
})
2020-04-10 23:04:05 +02:00
socket.on("startDownload", function(uuid){
$('#bar_' + uuid).removeClass('indeterminate').addClass('determinate')
})
2020-04-11 12:51:22 +02:00
socket.on("finishDownload", function(uuid){
2020-04-14 11:40:03 +02:00
if (queue.indexOf(uuid) > -1){
toast(`${queueList[uuid].title} finished downloading.`, 'done')
$('#bar_' + uuid).css('width', '100%')
let result_icon = $('#download_'+uuid).find('.queue_icon')
if (queueList[uuid].failed == 0){
result_icon.text("done")
}else if (queueList[uuid].failed >= queueList[uuid].size){
result_icon.text("error")
}else{
result_icon.text("warning")
}
let index = queue.indexOf(uuid)
if (index > -1){
queue.splice(index, 1)
delete queueList[uuid]
}
if (queue.length <= 0){
toast('All downloads completed!', 'done_all')
2020-04-14 11:40:03 +02:00
}
2020-04-13 20:05:26 +02:00
}
2020-04-11 12:51:22 +02:00
})
socket.on("removedAllDownloads", function(){
queue = []
queueList = {}
$("#download_list").html("")
})
socket.on("updateQueue", function(update){
2020-04-10 18:00:42 +02:00
if (update.uuid && queue.indexOf(update.uuid) > -1){
if (update.downloaded){
queueList[update.uuid].downloaded++
$("#download_"+update.uuid+" .queue_downloaded").text(queueList[update.uuid].downloaded + queueList[update.uuid].failed)
2020-04-10 18:00:42 +02:00
}
if (update.failed){
queueList[update.uuid].failed++
$("#download_"+update.uuid+" .queue_downloaded").text(queueList[update.uuid].downloaded + queueList[update.uuid].failed)
2020-04-10 18:00:42 +02:00
if (queueList[update.uuid].failed == 1){
$("#download_"+update.uuid+" .download_info_status").append(`<span class="secondary-text inline-flex"><span class="download_slim_separator">(</span><span class="queue_failed">1</span> <i class="material-icons">error_outline</i><span class="download_slim_separator">)</span></span>`)
2020-04-10 18:00:42 +02:00
}else{
$("#download_"+update.uuid+" .queue_failed").text(queueList[update.uuid].failed)
}
}
if (update.progress){
queueList[update.uuid].progress = update.progress
2020-04-11 00:21:30 +02:00
$('#bar_' + update.uuid).css('width', update.progress + '%')
2020-04-10 18:00:42 +02:00
}
}
})