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'
|
|
|
|
import { showErrors } from '@/js/tabs.js'
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
/* ===== Locals ===== */
|
|
|
|
const tabMinWidth = 250
|
|
|
|
const tabMaxWidth = 500
|
2020-04-26 19:32:49 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
let cachedTabWidth = parseInt(localStorage.getItem('downloadTabWidth')) || 300
|
2020-04-22 20:06:59 +00:00
|
|
|
let queueList = {}
|
|
|
|
let queue = []
|
|
|
|
let queueComplete = []
|
2020-04-26 17:33:09 +00:00
|
|
|
let tabContainerEl
|
|
|
|
let listEl
|
|
|
|
let dragHandlerEl
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
function init() {
|
|
|
|
// Find download DOM elements
|
|
|
|
tabContainerEl = document.getElementById('download_tab_container')
|
|
|
|
listEl = document.getElementById('download_list')
|
|
|
|
dragHandlerEl = document.getElementById('download_tab_drag_handler')
|
|
|
|
|
2020-05-15 20:21:31 +00:00
|
|
|
// Check if download tab has slim entries
|
|
|
|
if ('true' === localStorage.getItem('slimDownloads')) {
|
|
|
|
listEl.classList.add('slim')
|
|
|
|
}
|
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
// Check if download tab should be open
|
|
|
|
if ('true' === localStorage.getItem('downloadTabOpen')) {
|
|
|
|
tabContainerEl.classList.remove('tab_hidden')
|
|
|
|
|
|
|
|
setTabWidth(cachedTabWidth)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkListeners()
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
|
|
|
|
function linkListeners() {
|
2020-04-26 17:33:09 +00:00
|
|
|
listEl.addEventListener('click', handleListClick)
|
2020-04-22 20:06:59 +00:00
|
|
|
document.getElementById('toggle_download_tab').addEventListener('click', toggleDownloadTab)
|
2020-04-23 19:03:12 +00:00
|
|
|
|
|
|
|
// Queue buttons
|
|
|
|
document.getElementById('clean_queue').addEventListener('click', () => {
|
|
|
|
socket.emit('removeFinishedDownloads')
|
|
|
|
})
|
|
|
|
|
|
|
|
document.getElementById('cancel_queue').addEventListener('click', () => {
|
|
|
|
socket.emit('cancelAllDownloads')
|
|
|
|
})
|
2020-04-26 17:33:09 +00:00
|
|
|
|
2020-05-22 22:15:29 +00:00
|
|
|
document.getElementById('open_downloads_folder').addEventListener('click', () => {
|
2020-05-19 16:30:18 +00:00
|
|
|
if (window.clientMode) socket.emit('openDownloadsFolder')
|
|
|
|
})
|
|
|
|
|
2020-05-22 22:15:29 +00:00
|
|
|
// Downloads tab drag handling
|
2020-04-26 17:33:09 +00:00
|
|
|
dragHandlerEl.addEventListener('mousedown', event => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
document.addEventListener('mousemove', handleDrag)
|
|
|
|
})
|
|
|
|
|
|
|
|
document.addEventListener('mouseup', () => {
|
|
|
|
document.removeEventListener('mousemove', handleDrag)
|
|
|
|
})
|
|
|
|
|
|
|
|
tabContainerEl.addEventListener('transitionend', () => {
|
|
|
|
tabContainerEl.style.transition = ''
|
|
|
|
})
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', () => {
|
|
|
|
localStorage.setItem('downloadTabWidth', cachedTabWidth)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTabWidth(newWidth) {
|
|
|
|
if (undefined === newWidth) {
|
|
|
|
tabContainerEl.style.width = ''
|
|
|
|
listEl.style.width = ''
|
|
|
|
} else {
|
|
|
|
tabContainerEl.style.width = newWidth + 'px'
|
|
|
|
listEl.style.width = newWidth + 'px'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleDrag(event) {
|
|
|
|
let newWidth = window.innerWidth - event.pageX + 2
|
|
|
|
|
|
|
|
if (newWidth < tabMinWidth) {
|
|
|
|
newWidth = tabMinWidth
|
|
|
|
} else if (newWidth > tabMaxWidth) {
|
|
|
|
newWidth = tabMaxWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedTabWidth = newWidth
|
|
|
|
|
|
|
|
setTabWidth(newWidth)
|
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 })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToQueue(queueItem, current = false) {
|
|
|
|
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-06-24 17:10:10 +00:00
|
|
|
result_icon.bind('click', { item: queueItem }, showErrors)
|
|
|
|
failed_button.bind('click', { item: queueItem }, showErrors)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-05-22 22:15:29 +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-05-22 22:15:29 +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-05-22 22:15:29 +00:00
|
|
|
addToQueue(queueList[item])
|
2020-04-22 20:06:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function startDownload(uuid) {
|
|
|
|
$('#bar_' + uuid)
|
|
|
|
.removeClass('indeterminate')
|
|
|
|
.addClass('determinate')
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('startDownload', startDownload)
|
|
|
|
|
|
|
|
function handleListClick(event) {
|
2020-05-22 22:15:29 +00:00
|
|
|
const { target } = event
|
|
|
|
|
|
|
|
if (!target.matches('.queue_icon[data-uuid]')) {
|
2020-04-22 20:06:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-22 22:15:29 +00:00
|
|
|
let icon = target.innerText
|
|
|
|
let uuid = $(target).data('uuid')
|
2020-04-22 20:06:59 +00:00
|
|
|
|
|
|
|
switch (icon) {
|
|
|
|
case 'remove':
|
|
|
|
socket.emit('removeFromQueue', uuid)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show/Hide Download Tab
|
2020-05-22 22:15:29 +00:00
|
|
|
function toggleDownloadTab(clickEvent) {
|
|
|
|
clickEvent.preventDefault()
|
2020-04-22 20:06:59 +00:00
|
|
|
|
2020-04-26 17:33:09 +00:00
|
|
|
setTabWidth()
|
|
|
|
|
|
|
|
tabContainerEl.style.transition = 'all 250ms ease-in-out'
|
|
|
|
|
|
|
|
// Toggle returns a Boolean based on the action it performed
|
|
|
|
let isHidden = tabContainerEl.classList.toggle('tab_hidden')
|
|
|
|
|
|
|
|
if (!isHidden) {
|
|
|
|
setTabWidth(cachedTabWidth)
|
|
|
|
}
|
2020-04-22 20:06:59 +00:00
|
|
|
|
|
|
|
localStorage.setItem('downloadTabOpen', !isHidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('init_downloadQueue', initQueue)
|
|
|
|
socket.on('addedToQueue', addToQueue)
|
|
|
|
|
|
|
|
function removeFromQueue(uuid) {
|
|
|
|
let index = queue.indexOf(uuid)
|
|
|
|
if (index > -1) {
|
|
|
|
queue.splice(index, 1)
|
|
|
|
$(`#download_${queueList[uuid].uuid}`).remove()
|
|
|
|
delete queueList[uuid]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('removedFromQueue', removeFromQueue)
|
|
|
|
|
|
|
|
function finishDownload(uuid) {
|
|
|
|
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-06-24 17:10:10 +00:00
|
|
|
result_icon.bind('click', { item: queueList[uuid] }, showErrors)
|
|
|
|
failed_button.bind('click', { item: queueList[uuid] }, showErrors)
|
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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('finishDownload', finishDownload)
|
|
|
|
|
|
|
|
function removeAllDownloads(currentItem) {
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('removedAllDownloads', removeAllDownloads)
|
|
|
|
|
|
|
|
function removedFinishedDownloads() {
|
|
|
|
queueComplete.forEach(item => {
|
|
|
|
$('#download_' + item).remove()
|
|
|
|
})
|
|
|
|
queueComplete = []
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('removedFinishedDownloads', removedFinishedDownloads)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
socket.on('updateQueue', updateQueue)
|
|
|
|
|
|
|
|
export default {
|
2020-04-26 17:33:09 +00:00
|
|
|
init,
|
2020-04-22 20:06:59 +00:00
|
|
|
sendAddToQueue,
|
|
|
|
addToQueue
|
|
|
|
}
|