2020-05-07 20:10:13 +00:00
|
|
|
import $ from 'jquery'
|
2020-06-24 17:10:10 +00:00
|
|
|
import { socket } from '@/js/socket.js'
|
|
|
|
import { toast } from '@/js/toasts.js'
|
2020-07-14 20:27:48 +00:00
|
|
|
import EventBus from '@/js/EventBus'
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
/* ===== Locals ===== */
|
2020-04-26 19:32:49 +00:00
|
|
|
|
2020-04-22 20:06:59 +00:00
|
|
|
let queueList = {}
|
|
|
|
let queue = []
|
|
|
|
let queueComplete = []
|
2020-04-26 17:33:09 +00:00
|
|
|
let listEl
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
function init() {
|
|
|
|
// Find download DOM elements
|
|
|
|
listEl = document.getElementById('download_list')
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendAddToQueue(url, bitrate = null) {
|
2020-06-19 09:10:47 +00:00
|
|
|
if (url != '') {
|
2020-04-22 20:06:59 +00:00
|
|
|
socket.emit('addToQueue', { url: url, bitrate: bitrate })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _addToQueue(queueItem, current = false) {
|
2020-04-22 20:06:59 +00:00
|
|
|
queueList[queueItem.uuid] = queueItem
|
|
|
|
if (queueItem.downloaded + queueItem.failed == queueItem.size) {
|
2020-05-14 11:32:02 +00:00
|
|
|
if (queueComplete.indexOf(queueItem.uuid) == -1) queueComplete.push(queueItem.uuid)
|
2020-04-22 20:06:59 +00:00
|
|
|
} else {
|
2020-05-14 11:32:02 +00:00
|
|
|
if (queue.indexOf(queueItem.uuid) == -1) queue.push(queueItem.uuid)
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-05-15 20:21:31 +00:00
|
|
|
let queueDOM = document.getElementById('download_' + queueItem.uuid)
|
|
|
|
if (typeof queueDOM == 'undefined' || queueDOM == null) {
|
2020-05-14 11:32:02 +00:00
|
|
|
$(listEl).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>
|
|
|
|
</div>
|
2020-04-22 20:06:59 +00:00
|
|
|
</div>
|
2020-05-14 11:32:02 +00:00
|
|
|
<div class="download_bar">
|
|
|
|
<div class="progress"><div id="bar_${queueItem.uuid}" class="indeterminate"></div></div>
|
|
|
|
<i class="material-icons queue_icon" data-uuid="${queueItem.uuid}">remove</i>
|
2020-04-22 20:06:59 +00:00
|
|
|
</div>
|
2020-05-14 11:32:02 +00:00
|
|
|
</div>`
|
|
|
|
)
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
if (queueItem.progress > 0 || current) {
|
|
|
|
$('#bar_' + queueItem.uuid)
|
|
|
|
.removeClass('indeterminate')
|
|
|
|
.addClass('determinate')
|
|
|
|
}
|
|
|
|
$('#bar_' + queueItem.uuid).css('width', queueItem.progress + '%')
|
2020-05-17 19:24:46 +00:00
|
|
|
if (queueItem.failed >= 1 && $('#download_' + queueItem.uuid + ' .queue_failed').length == 0) {
|
2020-04-22 20:06:59 +00:00
|
|
|
$('#download_' + queueItem.uuid + ' .download_info_status').append(
|
2020-06-24 17:10:10 +00:00
|
|
|
`<span class="secondary-text inline-flex"><span class="download_slim_separator">(</span><span class="queue_failed_button inline-flex"><span class="queue_failed">${
|
|
|
|
queueItem.failed
|
|
|
|
}</span><i class="material-icons">error_outline</i></span><span class="download_slim_separator">)</span></span>`
|
2020-04-22 20:06:59 +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 {
|
2020-06-16 15:27:16 +00:00
|
|
|
let failed_button = $('#download_' + queueItem.uuid).find('.queue_failed_button')
|
|
|
|
result_icon.addClass('clickable')
|
|
|
|
failed_button.addClass('clickable')
|
2020-07-14 20:27:48 +00:00
|
|
|
result_icon.bind('click', { item: queueItem }, showErrorsTab)
|
|
|
|
failed_button.bind('click', { item: queueItem }, showErrorsTab)
|
2020-06-16 15:27:16 +00:00
|
|
|
if (queueItem.failed >= queueItem.size) {
|
|
|
|
result_icon.text('error')
|
|
|
|
} else {
|
|
|
|
result_icon.text('warning')
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-05 08:46:22 +00:00
|
|
|
if (!queueItem.init) toast(`${queueItem.title} added to queue`, 'playlist_add_check')
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
// ? Temporary?
|
|
|
|
function showErrorsTab(clickEvent) {
|
|
|
|
EventBus.$emit('showTabErrors', clickEvent.data.item, clickEvent.target)
|
|
|
|
}
|
|
|
|
|
|
|
|
function _initQueue(data) {
|
2020-05-22 22:15:29 +00:00
|
|
|
const { queue, queueComplete, currentItem, queueList } = data
|
|
|
|
|
|
|
|
if (queueComplete.length) {
|
|
|
|
queueComplete.forEach(item => {
|
2020-06-05 08:46:22 +00:00
|
|
|
queueList[item].init = true
|
2020-07-14 20:27:48 +00:00
|
|
|
_addToQueue(queueList[item])
|
2020-04-22 20:06:59 +00:00
|
|
|
})
|
|
|
|
}
|
2020-05-22 22:15:29 +00:00
|
|
|
|
|
|
|
if (currentItem) {
|
2020-06-05 08:46:22 +00:00
|
|
|
queueList[currentItem].init = true
|
2020-07-14 20:27:48 +00:00
|
|
|
_addToQueue(queueList[currentItem], true)
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-05-22 22:15:29 +00:00
|
|
|
|
|
|
|
queue.forEach(item => {
|
2020-06-05 08:46:22 +00:00
|
|
|
queueList[item].init = true
|
2020-07-14 20:27:48 +00:00
|
|
|
_addToQueue(queueList[item])
|
2020-04-22 20:06:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _startDownload(uuid) {
|
2020-04-22 20:06:59 +00:00
|
|
|
$('#bar_' + uuid)
|
|
|
|
.removeClass('indeterminate')
|
|
|
|
.addClass('determinate')
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('startDownload', _startDownload)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('init_downloadQueue', _initQueue)
|
|
|
|
socket.on('addedToQueue', _addToQueue)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _removeFromQueue(uuid) {
|
2020-04-22 20:06:59 +00:00
|
|
|
let index = queue.indexOf(uuid)
|
|
|
|
if (index > -1) {
|
|
|
|
queue.splice(index, 1)
|
|
|
|
$(`#download_${queueList[uuid].uuid}`).remove()
|
|
|
|
delete queueList[uuid]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('removedFromQueue', _removeFromQueue)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _finishDownload(uuid) {
|
2020-04-22 20:06:59 +00: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 {
|
2020-06-16 15:27:16 +00:00
|
|
|
let failed_button = $('#download_' + uuid).find('.queue_failed_button')
|
|
|
|
result_icon.addClass('clickable')
|
|
|
|
failed_button.addClass('clickable')
|
2020-07-14 20:27:48 +00:00
|
|
|
result_icon.bind('click', { item: queueList[uuid] }, showErrorsTab)
|
|
|
|
failed_button.bind('click', { item: queueList[uuid] }, showErrorsTab)
|
2020-06-16 15:27:16 +00:00
|
|
|
if (queueList[uuid].failed >= queueList[uuid].size) {
|
|
|
|
result_icon.text('error')
|
|
|
|
} else {
|
|
|
|
result_icon.text('warning')
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-06-16 15:27:16 +00:00
|
|
|
|
2020-04-22 20:06:59 +00:00
|
|
|
let index = queue.indexOf(uuid)
|
|
|
|
if (index > -1) {
|
|
|
|
queue.splice(index, 1)
|
|
|
|
queueComplete.push(uuid)
|
|
|
|
}
|
|
|
|
if (queue.length <= 0) {
|
|
|
|
toast('All downloads completed!', 'done_all')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('finishDownload', _finishDownload)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _removeAllDownloads(currentItem) {
|
2020-04-22 20:06:59 +00:00
|
|
|
queueComplete = []
|
|
|
|
if (currentItem == '') {
|
|
|
|
queue = []
|
|
|
|
queueList = {}
|
2020-04-26 17:33:09 +00:00
|
|
|
$(listEl).html('')
|
2020-04-22 20:06:59 +00:00
|
|
|
} else {
|
|
|
|
queue = [currentItem]
|
|
|
|
let tempQueueItem = queueList[currentItem]
|
|
|
|
queueList = {}
|
|
|
|
queueList[currentItem] = tempQueueItem
|
2020-06-24 17:10:10 +00:00
|
|
|
$('.download_object').each(function(index) {
|
2020-04-22 20:06:59 +00:00
|
|
|
if ($(this).attr('id') != 'download_' + currentItem) $(this).remove()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('removedAllDownloads', _removeAllDownloads)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _removedFinishedDownloads() {
|
2020-04-22 20:06:59 +00:00
|
|
|
queueComplete.forEach(item => {
|
|
|
|
$('#download_' + item).remove()
|
|
|
|
})
|
|
|
|
queueComplete = []
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('removedFinishedDownloads', _removedFinishedDownloads)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
function _updateQueue(update) {
|
2020-05-22 22:15:29 +00:00
|
|
|
// downloaded and failed default to false?
|
|
|
|
const { uuid, downloaded, failed, progress } = update
|
|
|
|
|
|
|
|
if (uuid && queue.indexOf(uuid) > -1) {
|
|
|
|
if (downloaded) {
|
|
|
|
queueList[uuid].downloaded++
|
|
|
|
$('#download_' + uuid + ' .queue_downloaded').text(queueList[uuid].downloaded + queueList[uuid].failed)
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-05-22 22:15:29 +00:00
|
|
|
if (failed) {
|
|
|
|
queueList[uuid].failed++
|
|
|
|
$('#download_' + uuid + ' .queue_downloaded').text(queueList[uuid].downloaded + queueList[uuid].failed)
|
|
|
|
if (queueList[uuid].failed == 1 && $('#download_' + uuid + ' .queue_failed').length == 0) {
|
|
|
|
$('#download_' + uuid + ' .download_info_status').append(
|
2020-06-16 15:27:16 +00:00
|
|
|
`<span class="secondary-text inline-flex"><span class="download_slim_separator">(</span><span class="queue_failed_button inline-flex"><span class="queue_failed">1</span> <i class="material-icons">error_outline</i></span><span class="download_slim_separator">)</span></span>`
|
2020-04-22 20:06:59 +00:00
|
|
|
)
|
|
|
|
} else {
|
2020-05-22 22:15:29 +00:00
|
|
|
$('#download_' + uuid + ' .queue_failed').text(queueList[uuid].failed)
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-06-24 17:10:10 +00:00
|
|
|
queueList[uuid].errors.push({ message: update.error, data: update.data })
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
2020-05-22 22:15:29 +00:00
|
|
|
if (progress) {
|
|
|
|
queueList[uuid].progress = progress
|
|
|
|
$('#bar_' + uuid).css('width', progress + '%')
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:48 +00:00
|
|
|
socket.on('updateQueue', _updateQueue)
|
2020-04-22 20:06:59 +00:00
|
|
|
|
|
|
|
export default {
|
2020-04-26 17:33:09 +00:00
|
|
|
init,
|
2020-07-14 20:27:48 +00:00
|
|
|
sendAddToQueue
|
2020-04-22 20:06:59 +00:00
|
|
|
}
|