2020-06-24 20:54:36 +00:00
|
|
|
import Vue from 'vue'
|
2020-06-19 19:53:32 +00:00
|
|
|
|
2021-05-05 18:59:27 +00:00
|
|
|
import '@/plugins/composition-api'
|
|
|
|
|
2020-11-07 18:00:34 +00:00
|
|
|
import '@/styles/vendor/material-icons.css'
|
|
|
|
import '@/styles/vendor/OpenSans.css'
|
2020-11-02 11:25:08 +00:00
|
|
|
|
2020-11-10 19:14:03 +00:00
|
|
|
import '@/styles/css/tailwind.css'
|
|
|
|
|
2020-11-10 18:48:02 +00:00
|
|
|
import '@/styles/css/normalize.css'
|
|
|
|
import '@/styles/css/base.css'
|
2020-10-10 18:03:19 +00:00
|
|
|
import '@/styles/css/components.css'
|
2020-11-02 11:25:08 +00:00
|
|
|
import '@/styles/css/helpers.css'
|
2020-11-07 18:00:34 +00:00
|
|
|
import '@/styles/css/typography.css'
|
2020-10-10 18:03:19 +00:00
|
|
|
|
2020-11-10 19:14:03 +00:00
|
|
|
import '@/styles/scss/tables.scss'
|
|
|
|
|
2020-09-25 18:43:23 +00:00
|
|
|
import App from '@/App.vue'
|
2020-07-16 22:11:28 +00:00
|
|
|
import i18n from '@/plugins/i18n'
|
2020-08-22 22:39:31 +00:00
|
|
|
import router from '@/router'
|
|
|
|
import store from '@/store'
|
2020-06-19 19:53:32 +00:00
|
|
|
|
2020-07-16 22:11:28 +00:00
|
|
|
import { socket } from '@/utils/socket'
|
2021-05-28 13:09:55 +00:00
|
|
|
import { fetchData, postToServer } from '@/utils/api'
|
2020-07-16 22:11:28 +00:00
|
|
|
import { toast } from '@/utils/toasts'
|
2020-08-25 13:29:11 +00:00
|
|
|
import { isValidURL } from '@/utils/utils'
|
2020-09-26 15:53:25 +00:00
|
|
|
import { sendAddToQueue } from '@/utils/downloads'
|
2021-08-11 13:23:41 +00:00
|
|
|
import { SPOTIFY_STATUS } from '@/constants'
|
2020-08-25 13:29:11 +00:00
|
|
|
|
2021-10-05 10:14:59 +00:00
|
|
|
/* ===== Random utils ===== */
|
|
|
|
|
|
|
|
/* eslint-disable no-extend-native */
|
|
|
|
String.prototype.capitalize = function () {
|
|
|
|
return this.charAt(0).toUpperCase() + this.slice(1)
|
|
|
|
}
|
|
|
|
|
2020-05-15 20:21:31 +00:00
|
|
|
/* ===== App initialization ===== */
|
2021-03-01 20:58:43 +00:00
|
|
|
async function startApp() {
|
2020-07-16 22:11:28 +00:00
|
|
|
new Vue({
|
2020-08-22 22:39:31 +00:00
|
|
|
store,
|
2020-07-28 19:39:44 +00:00
|
|
|
router,
|
2020-07-16 22:11:28 +00:00
|
|
|
i18n,
|
2020-06-29 18:23:56 +00:00
|
|
|
render: h => h(App)
|
|
|
|
}).$mount('#app')
|
2021-03-01 20:22:32 +00:00
|
|
|
|
2021-03-01 20:58:43 +00:00
|
|
|
const connectResponse = await (await fetch('connect')).json()
|
2021-08-11 13:23:41 +00:00
|
|
|
const spotifyStatus = connectResponse.spotifyEnabled ? SPOTIFY_STATUS.ENABLED : SPOTIFY_STATUS.DISABLED
|
|
|
|
|
|
|
|
if (!connectResponse.deezerAvailable) {
|
|
|
|
document.getElementById('deezer_not_available').classList.remove('hide')
|
|
|
|
}
|
2021-03-01 20:58:43 +00:00
|
|
|
|
2021-05-20 19:22:27 +00:00
|
|
|
store.dispatch('setAppInfo', connectResponse.update).catch(console.error)
|
2021-08-11 13:23:41 +00:00
|
|
|
store.dispatch('setSpotifyStatus', spotifyStatus).catch(console.error)
|
2021-05-20 19:22:27 +00:00
|
|
|
|
2021-05-06 09:51:33 +00:00
|
|
|
let arl = localStorage.getItem('arl')
|
2021-05-28 13:09:55 +00:00
|
|
|
const accessToken = localStorage.getItem('accessToken')
|
2021-03-01 20:58:43 +00:00
|
|
|
|
|
|
|
if (connectResponse.autologin) {
|
2021-03-01 22:09:14 +00:00
|
|
|
console.info('Autologin')
|
2021-03-01 20:58:43 +00:00
|
|
|
const accountNum = localStorage.getItem('accountNum')
|
|
|
|
|
2021-05-28 13:09:55 +00:00
|
|
|
async function login(arl, accountNum) {
|
|
|
|
toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')
|
2021-03-01 20:58:43 +00:00
|
|
|
arl = arl.trim()
|
|
|
|
let result
|
|
|
|
|
|
|
|
if (accountNum !== 0) {
|
2021-05-06 06:56:03 +00:00
|
|
|
result = await fetchData('login-arl', { arl, force: true, child: accountNum || 0 }, 'POST')
|
2021-03-01 20:58:43 +00:00
|
|
|
} else {
|
2021-05-06 06:56:03 +00:00
|
|
|
result = await fetchData('login-arl', { arl }, 'POST')
|
2021-03-01 20:22:32 +00:00
|
|
|
}
|
2021-03-01 20:58:43 +00:00
|
|
|
|
2021-05-28 13:09:55 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arl) {
|
|
|
|
let result = await login(arl, accountNum)
|
|
|
|
if (result.status === 0 && accessToken) {
|
|
|
|
const { arl: newArl } = await postToServer('loginWithCredentials', { accessToken })
|
|
|
|
if (newArl && newArl !== arl) {
|
|
|
|
arl = newArl
|
|
|
|
store.dispatch('setARL', { arl })
|
|
|
|
}
|
|
|
|
result = await login(newArl, accountNum)
|
|
|
|
}
|
2021-03-01 21:32:35 +00:00
|
|
|
loggedIn(result)
|
2021-03-01 20:58:43 +00:00
|
|
|
}
|
2021-05-06 09:51:33 +00:00
|
|
|
} else {
|
|
|
|
loggedIn({ status: 3, user: connectResponse.currentUser, arl })
|
2021-03-01 20:58:43 +00:00
|
|
|
}
|
2020-06-24 20:54:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:15:29 +00:00
|
|
|
function initClient() {
|
2020-09-22 20:40:41 +00:00
|
|
|
store.dispatch('setClientMode', true)
|
2020-09-24 16:33:56 +00:00
|
|
|
setClientModeKeyBindings()
|
2020-05-19 16:30:18 +00:00
|
|
|
}
|
2020-05-15 20:21:31 +00:00
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', startApp)
|
2021-05-28 13:09:55 +00:00
|
|
|
if (window.api) {
|
2021-05-28 10:39:00 +00:00
|
|
|
initClient()
|
|
|
|
}
|
2020-05-15 20:21:31 +00:00
|
|
|
|
2020-08-25 13:29:11 +00:00
|
|
|
/* ===== Global shortcuts ===== */
|
|
|
|
|
|
|
|
document.addEventListener('paste', pasteEvent => {
|
2020-09-26 15:53:25 +00:00
|
|
|
if (pasteEvent.target.localName === 'input') return
|
|
|
|
|
|
|
|
let pastedText = pasteEvent.clipboardData.getData('Text')
|
2020-09-17 21:55:57 +00:00
|
|
|
|
2020-09-26 15:53:25 +00:00
|
|
|
if (isValidURL(pastedText)) {
|
|
|
|
if (router.currentRoute.name === 'Link Analyzer') {
|
|
|
|
socket.emit('analyzeLink', pastedText)
|
2020-09-17 20:43:52 +00:00
|
|
|
} else {
|
2021-05-23 16:46:03 +00:00
|
|
|
if (pastedText.includes('\n')) pastedText = pastedText.replace(/\n/g, ';')
|
2020-09-26 15:53:25 +00:00
|
|
|
sendAddToQueue(pastedText)
|
2020-08-25 13:29:11 +00:00
|
|
|
}
|
2020-09-26 15:53:25 +00:00
|
|
|
} else {
|
2021-05-23 16:46:03 +00:00
|
|
|
const searchbar = document.querySelector('#searchbar')
|
2020-09-26 15:53:25 +00:00
|
|
|
searchbar.select()
|
|
|
|
searchbar.setSelectionRange(0, 99999)
|
2020-09-23 13:44:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-09-24 16:33:56 +00:00
|
|
|
/**
|
|
|
|
* Sets up key bindings that already work in the browser (server mode)
|
|
|
|
*/
|
|
|
|
function setClientModeKeyBindings() {
|
|
|
|
document.addEventListener('keyup', keyEvent => {
|
|
|
|
// ALT + left
|
|
|
|
if (keyEvent.altKey && keyEvent.key === 'ArrowLeft') {
|
|
|
|
router.back()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ALT + right
|
|
|
|
if (keyEvent.altKey && keyEvent.key === 'ArrowRight') {
|
|
|
|
router.forward()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:02:06 +00:00
|
|
|
/* ===== Socketio listeners ===== */
|
|
|
|
|
|
|
|
// Debug messages for socketio
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('message', function (msg) {
|
2020-04-19 20:02:06 +00:00
|
|
|
console.log(msg)
|
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
function loggedIn(data) {
|
2020-09-17 20:43:52 +00:00
|
|
|
const { status, user } = data
|
|
|
|
|
|
|
|
switch (status) {
|
2020-04-19 20:02:06 +00:00
|
|
|
case 1:
|
|
|
|
case 3:
|
2020-09-17 20:43:52 +00:00
|
|
|
// Login ok
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.loggedIn'), 'done', true, 'login-toast')
|
2020-08-22 21:34:16 +00:00
|
|
|
|
2020-09-17 20:43:52 +00:00
|
|
|
store.dispatch('login', data)
|
2020-04-19 20:02:06 +00:00
|
|
|
break
|
|
|
|
case 2:
|
2020-09-17 20:43:52 +00:00
|
|
|
// Already logged in
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.alreadyLogged'), 'done', true, 'login-toast')
|
2020-08-22 21:34:16 +00:00
|
|
|
|
2020-09-17 20:43:52 +00:00
|
|
|
store.dispatch('setUser', user)
|
2020-04-19 20:02:06 +00:00
|
|
|
break
|
|
|
|
case 0:
|
2020-09-17 20:43:52 +00:00
|
|
|
// Login failed
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.loginFailed'), 'close', true, 'login-toast')
|
2020-09-17 20:43:52 +00:00
|
|
|
|
|
|
|
store.dispatch('removeARL')
|
2020-04-19 20:02:06 +00:00
|
|
|
break
|
2020-09-20 10:32:53 +00:00
|
|
|
case -1:
|
|
|
|
toast(i18n.t('toasts.deezerNotAvailable'), 'close', true, 'login-toast')
|
2021-05-23 16:46:03 +00:00
|
|
|
|
2020-09-23 16:15:59 +00:00
|
|
|
// TODO
|
|
|
|
// $('#open_login_prompt').show()
|
|
|
|
// document.getElementById('logged_in_info').classList.add('hide')
|
|
|
|
// $('#settings_username').text('Not Logged')
|
|
|
|
// $('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
|
|
|
|
// document.getElementById('home_not_logged_in').classList.remove('hide')
|
2020-04-19 20:02:06 +00:00
|
|
|
}
|
2021-02-25 21:26:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('restoringQueue', function () {
|
2020-08-15 23:34:55 +00:00
|
|
|
toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue')
|
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('cancellingCurrentItem', function (uuid) {
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.cancellingCurrentItem'), 'loading', false, 'cancelling_' + uuid)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('currentItemCancelled', function (uuid) {
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.currentItemCancelled'), 'done', true, 'cancelling_' + uuid)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('startAddingArtist', function (data) {
|
2020-09-17 20:43:52 +00:00
|
|
|
toast(i18n.t('toasts.startAddingArtist', { artist: data.name }), 'loading', false, 'artist_' + data.id)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('finishAddingArtist', function (data) {
|
2020-09-17 20:43:52 +00:00
|
|
|
toast(i18n.t('toasts.finishAddingArtist', { artist: data.name }), 'done', true, 'artist_' + data.id)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('startConvertingSpotifyPlaylist', function (id) {
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.startConvertingSpotifyPlaylist'), 'loading', false, 'spotifyplaylist_' + id)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('finishConvertingSpotifyPlaylist', function (id) {
|
2020-07-21 09:09:47 +00:00
|
|
|
toast(i18n.t('toasts.finishConvertingSpotifyPlaylist'), 'done', true, 'spotifyplaylist_' + id)
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('errorMessage', function (error) {
|
2020-06-05 08:46:22 +00:00
|
|
|
toast(error, 'error')
|
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('queueError', function (queueItem) {
|
2020-09-17 21:55:57 +00:00
|
|
|
if (queueItem.errid) {
|
2020-11-02 11:25:08 +00:00
|
|
|
toast(queueItem.link + ' - ' + i18n.t(`errors.ids.${queueItem.errid}`), 'error')
|
2020-09-17 21:55:57 +00:00
|
|
|
} else {
|
2020-11-02 11:25:08 +00:00
|
|
|
toast(queueItem.link + ' - ' + queueItem.error, 'error')
|
2020-09-17 21:55:57 +00:00
|
|
|
}
|
2020-07-23 14:43:20 +00:00
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('alreadyInQueue', function (data) {
|
2020-09-17 20:43:52 +00:00
|
|
|
toast(i18n.t('toasts.alreadyInQueue', { item: data.title }), 'playlist_add_check')
|
2020-06-05 08:46:22 +00:00
|
|
|
})
|
2020-07-29 20:24:44 +00:00
|
|
|
|
2021-05-28 13:09:55 +00:00
|
|
|
socket.on('loginNeededToDownload', function () {
|
2020-07-29 20:24:44 +00:00
|
|
|
toast(i18n.t('toasts.loginNeededToDownload'), 'report')
|
|
|
|
})
|
2020-10-14 21:29:24 +00:00
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('startGeneratingItems', function (data) {
|
2020-10-14 21:29:24 +00:00
|
|
|
toast(i18n.t('toasts.startGeneratingItems', { n: data.total }), 'loading', false, 'batch_' + data.uuid)
|
|
|
|
})
|
|
|
|
|
2021-03-01 20:22:32 +00:00
|
|
|
socket.on('finishGeneratingItems', function (data) {
|
2020-10-14 21:29:24 +00:00
|
|
|
toast(i18n.t('toasts.finishGeneratingItems', { n: data.total }), 'done', true, 'batch_' + data.uuid)
|
|
|
|
})
|