2020-04-10 16:00:42 +00:00
|
|
|
var queueList = {}
|
|
|
|
var queue = []
|
2020-04-14 14:48:13 +00:00
|
|
|
var queueComplete = []
|
2020-04-10 14:12:21 +00:00
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('init_downloadQueue', function (data) {
|
2020-04-14 14:48:13 +00:00
|
|
|
console.log(data)
|
2020-04-16 17:57:34 +00:00
|
|
|
if (data.queueComplete.length) {
|
|
|
|
data.queueComplete.forEach(item => {
|
2020-04-14 14:48:13 +00:00
|
|
|
addToQueue(data.queueList[item])
|
|
|
|
})
|
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
if (data.currentItem) {
|
2020-04-14 10:58:31 +00:00
|
|
|
addToQueue(data['queueList'][data.currentItem])
|
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
data.queue.forEach(item => {
|
2020-04-14 14:48:13 +00:00
|
|
|
addToQueue(data.queueList[item])
|
2020-04-13 17:22:34 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
function addToQueue(queueItem) {
|
2020-04-10 16:00:42 +00:00
|
|
|
queueList[queueItem.uuid] = queueItem
|
2020-04-16 17:57:34 +00:00
|
|
|
if (queueItem.downloaded + queueItem.failed == queueItem.size) queueComplete.push(queueItem.uuid)
|
|
|
|
else queue.push(queueItem.uuid)
|
|
|
|
$('#download_list').append(
|
|
|
|
`<div class="download_object" id="download_${queueItem.uuid}" data-deezerid="${queueItem.id}">
|
2020-04-10 16:00:42 +00:00
|
|
|
<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">
|
2020-04-16 17:57:34 +00:00
|
|
|
<span class="download_line"><span class="queue_downloaded">${queueItem.downloaded + queueItem.failed}</span>/${
|
|
|
|
queueItem.size
|
|
|
|
}</span>
|
2020-04-10 16:00:42 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-11 13:43:59 +00: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-16 17:57:34 +00:00
|
|
|
</div>`
|
|
|
|
)
|
|
|
|
if (queueItem.progress > 0) {
|
|
|
|
$('#bar_' + queueItem.uuid)
|
|
|
|
.removeClass('indeterminate')
|
|
|
|
.addClass('determinate')
|
2020-04-14 14:48:13 +00:00
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#bar_' + queueItem.uuid).css('width', queueItem.progress + '%')
|
|
|
|
if (queueItem.failed >= 1) {
|
|
|
|
$('#download_' + queueItem.uuid + ' .download_info_status').append(
|
|
|
|
`<span class="secondary-text inline-flex"><span class="download_slim_separator">(</span><span class="queue_failed">${queueItem.failed}</span><i class="material-icons">error_outline</i><span class="download_slim_separator">)</span></span>`
|
|
|
|
)
|
2020-04-14 14:48:13 +00:00
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
if (queueItem.downloaded + queueItem.failed == queueItem.size) {
|
|
|
|
let result_icon = $('#download_' + queueItem.uuid).find('.queue_icon')
|
|
|
|
if (queueItem.failed == 0) {
|
|
|
|
result_icon.text('done')
|
|
|
|
} else if (queueItem.failed == queueItem.size) {
|
|
|
|
result_icon.text('error')
|
|
|
|
} else {
|
|
|
|
result_icon.text('warning')
|
2020-04-14 14:48:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-13 17:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('addedToQueue', function (queueItem) {
|
2020-04-13 17:22:34 +00:00
|
|
|
addToQueue(queueItem)
|
2020-04-10 14:12:21 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
function downloadAction(evt) {
|
2020-04-11 13:43:59 +00:00
|
|
|
let icon = $(evt.currentTarget).text()
|
2020-04-16 17:57:34 +00:00
|
|
|
let uuid = $(evt.currentTarget).data('uuid')
|
2020-04-11 13:43:59 +00:00
|
|
|
switch (icon) {
|
|
|
|
case 'remove':
|
|
|
|
socket.emit('removeFromQueue', uuid)
|
2020-04-16 17:57:34 +00:00
|
|
|
break
|
2020-04-11 13:43:59 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('removedFromQueue', function (uuid) {
|
2020-04-11 13:43:59 +00:00
|
|
|
let index = queue.indexOf(uuid)
|
2020-04-16 17:57:34 +00:00
|
|
|
if (index > -1) {
|
2020-04-11 13:43:59 +00:00
|
|
|
queue.splice(index, 1)
|
|
|
|
$(`#download_${queueList[uuid].uuid}`).remove()
|
|
|
|
delete queueList[uuid]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('startDownload', function (uuid) {
|
|
|
|
$('#bar_' + uuid)
|
|
|
|
.removeClass('indeterminate')
|
|
|
|
.addClass('determinate')
|
2020-04-10 21:04:05 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('finishDownload', function (uuid) {
|
|
|
|
if (queue.indexOf(uuid) > -1) {
|
2020-04-14 09:40:03 +00:00
|
|
|
toast(`${queueList[uuid].title} finished downloading.`, 'done')
|
|
|
|
$('#bar_' + uuid).css('width', '100%')
|
2020-04-16 17:57:34 +00:00
|
|
|
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')
|
2020-04-14 09:40:03 +00:00
|
|
|
}
|
|
|
|
let index = queue.indexOf(uuid)
|
2020-04-16 17:57:34 +00:00
|
|
|
if (index > -1) {
|
2020-04-14 09:40:03 +00:00
|
|
|
queue.splice(index, 1)
|
2020-04-14 14:48:13 +00:00
|
|
|
queueComplete.push(uuid)
|
2020-04-14 09:40:03 +00:00
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
if (queue.length <= 0) {
|
2020-04-14 10:58:31 +00:00
|
|
|
toast('All downloads completed!', 'done_all')
|
2020-04-14 09:40:03 +00:00
|
|
|
}
|
2020-04-13 18:05:26 +00:00
|
|
|
}
|
2020-04-11 10:51:22 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('removedAllDownloads', function (currentItem) {
|
2020-04-14 14:48:13 +00:00
|
|
|
queueComplete = []
|
2020-04-16 17:57:34 +00:00
|
|
|
if (currentItem == '') {
|
2020-04-14 17:14:17 +00:00
|
|
|
queue = []
|
|
|
|
queueList = {}
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#download_list').html('')
|
|
|
|
} else {
|
|
|
|
queue = [currentItem]
|
2020-04-14 17:14:17 +00:00
|
|
|
tempQueueItem = queueList[currentItem]
|
|
|
|
queueList = {}
|
|
|
|
queueList[currentItem] = tempQueueItem
|
2020-04-16 17:57:34 +00:00
|
|
|
$('.download_object').each(function (index) {
|
|
|
|
if ($(this).attr('id') != 'download_' + currentItem) $(this).remove()
|
2020-04-14 17:14:17 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 14:24:53 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('removedFinishedDownloads', function () {
|
|
|
|
queueComplete.forEach(item => {
|
|
|
|
$('#download_' + item).remove()
|
2020-04-14 14:48:13 +00:00
|
|
|
})
|
|
|
|
queueComplete = []
|
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#clean_queue').on('click', function () {
|
|
|
|
socket.emit('removeFinishedDownloads')
|
2020-04-14 14:48:13 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#cancel_queue').on('click', function () {
|
|
|
|
socket.emit('cancelAllDownloads')
|
2020-04-14 14:48:13 +00:00
|
|
|
})
|
|
|
|
|
2020-04-16 17:57:34 +00:00
|
|
|
socket.on('updateQueue', function (update) {
|
|
|
|
if (update.uuid && queue.indexOf(update.uuid) > -1) {
|
|
|
|
if (update.downloaded) {
|
2020-04-10 16:00:42 +00:00
|
|
|
queueList[update.uuid].downloaded++
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#download_' + update.uuid + ' .queue_downloaded').text(
|
|
|
|
queueList[update.uuid].downloaded + queueList[update.uuid].failed
|
|
|
|
)
|
2020-04-10 16:00:42 +00:00
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
if (update.failed) {
|
2020-04-10 16:00:42 +00:00
|
|
|
queueList[update.uuid].failed++
|
2020-04-16 17:57:34 +00:00
|
|
|
$('#download_' + update.uuid + ' .queue_downloaded').text(
|
|
|
|
queueList[update.uuid].downloaded + queueList[update.uuid].failed
|
|
|
|
)
|
|
|
|
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>`
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
$('#download_' + update.uuid + ' .queue_failed').text(queueList[update.uuid].failed)
|
2020-04-10 16:00:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-16 17:57:34 +00:00
|
|
|
if (update.progress) {
|
2020-04-10 16:00:42 +00:00
|
|
|
queueList[update.uuid].progress = update.progress
|
2020-04-10 22:21:30 +00:00
|
|
|
$('#bar_' + update.uuid).css('width', update.progress + '%')
|
2020-04-10 16:00:42 +00:00
|
|
|
}
|
2020-04-10 14:12:21 +00:00
|
|
|
}
|
|
|
|
})
|