deemix-webui/src/app.js

221 lines
5.7 KiB
JavaScript
Raw Normal View History

import Vue from 'vue'
import '@/plugins/composition-api'
import '@/styles/vendor/material-icons.css'
import '@/styles/vendor/OpenSans.css'
import '@/styles/css/tailwind.css'
import '@/styles/css/normalize.css'
import '@/styles/css/base.css'
import '@/styles/css/components.css'
import '@/styles/css/helpers.css'
import '@/styles/css/typography.css'
import '@/styles/scss/tables.scss'
import App from '@/App.vue'
import i18n from '@/plugins/i18n'
import router from '@/router'
import store from '@/store'
import { socket } from '@/utils/socket'
import { fetchData } from '@/utils/api'
import { toast } from '@/utils/toasts'
import { isValidURL } from '@/utils/utils'
import { sendAddToQueue } from '@/utils/downloads'
/* ===== App initialization ===== */
async function startApp() {
new Vue({
store,
2020-07-28 21:39:44 +02:00
router,
i18n,
render: h => h(App)
}).$mount('#app')
const connectResponse = await (await fetch('connect')).json()
store.dispatch('setAppInfo', connectResponse.update)
if (connectResponse.autologin) {
console.info('Autologin')
let arl = localStorage.getItem('arl')
const accountNum = localStorage.getItem('accountNum')
if (arl) {
arl = arl.trim()
let result
if (accountNum !== 0) {
result = await fetchData('login', { arl, force: true, child: accountNum || 0 })
} else {
result = await fetchData('login', { arl })
}
loggedIn(result)
}
}
}
function initClient() {
store.dispatch('setClientMode', true)
setClientModeKeyBindings()
}
document.addEventListener('DOMContentLoaded', startApp)
window.addEventListener('pywebviewready', initClient)
/* ===== Global shortcuts ===== */
document.addEventListener('paste', pasteEvent => {
if (pasteEvent.target.localName === 'input') return
let pastedText = pasteEvent.clipboardData.getData('Text')
if (isValidURL(pastedText)) {
if (router.currentRoute.name === 'Link Analyzer') {
socket.emit('analyzeLink', pastedText)
2020-09-17 22:43:52 +02:00
} else {
if (pastedText.indexOf('\n') != -1) pastedText = pastedText.replace(/\n/g, ';')
sendAddToQueue(pastedText)
}
} else {
let searchbar = document.querySelector('#searchbar')
searchbar.select()
searchbar.setSelectionRange(0, 99999)
}
})
/**
* 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 22:02:06 +02:00
/* ===== Socketio listeners ===== */
// Debug messages for socketio
socket.on('message', function (msg) {
2020-04-19 22:02:06 +02:00
console.log(msg)
})
function loggedIn(data) {
2020-09-17 22:43:52 +02:00
const { status, user } = data
switch (status) {
2020-04-19 22:02:06 +02:00
case 1:
case 3:
2020-09-17 22:43:52 +02:00
// Login ok
toast(i18n.t('toasts.loggedIn'), 'done', true, 'login-toast')
2020-09-17 22:43:52 +02:00
store.dispatch('login', data)
2020-04-19 22:02:06 +02:00
break
case 2:
2020-09-17 22:43:52 +02:00
// Already logged in
toast(i18n.t('toasts.alreadyLogged'), 'done', true, 'login-toast')
2020-09-17 22:43:52 +02:00
store.dispatch('setUser', user)
2020-04-19 22:02:06 +02:00
break
case 0:
2020-09-17 22:43:52 +02:00
// Login failed
toast(i18n.t('toasts.loginFailed'), 'close', true, 'login-toast')
2020-09-17 22:43:52 +02:00
store.dispatch('removeARL')
2020-04-19 22:02:06 +02:00
break
case -1:
toast(i18n.t('toasts.deezerNotAvailable'), 'close', true, 'login-toast')
return
// 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 22:02:06 +02:00
}
2021-02-25 22:26:05 +01:00
}
/*
socket.on('logging_in', function() {
toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')
})
socket.on('logged_in', function(data) {
2020-04-19 22:02:06 +02:00
})
socket.on('logged_out', function() {
toast(i18n.t('toasts.loggedOut'), 'done', true, 'login-toast')
2020-09-17 22:43:52 +02:00
store.dispatch('logout')
2020-04-19 22:02:06 +02:00
})
2021-02-25 22:26:05 +01:00
*/
socket.on('restoringQueue', function () {
toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue')
})
socket.on('cancellingCurrentItem', function (uuid) {
toast(i18n.t('toasts.cancellingCurrentItem'), 'loading', false, 'cancelling_' + uuid)
})
socket.on('currentItemCancelled', function (uuid) {
toast(i18n.t('toasts.currentItemCancelled'), 'done', true, 'cancelling_' + uuid)
})
socket.on('startAddingArtist', function (data) {
2020-09-17 22:43:52 +02:00
toast(i18n.t('toasts.startAddingArtist', { artist: data.name }), 'loading', false, 'artist_' + data.id)
})
socket.on('finishAddingArtist', function (data) {
2020-09-17 22:43:52 +02:00
toast(i18n.t('toasts.finishAddingArtist', { artist: data.name }), 'done', true, 'artist_' + data.id)
})
socket.on('startConvertingSpotifyPlaylist', function (id) {
toast(i18n.t('toasts.startConvertingSpotifyPlaylist'), 'loading', false, 'spotifyplaylist_' + id)
})
socket.on('finishConvertingSpotifyPlaylist', function (id) {
toast(i18n.t('toasts.finishConvertingSpotifyPlaylist'), 'done', true, 'spotifyplaylist_' + id)
})
socket.on('errorMessage', function (error) {
toast(error, 'error')
})
socket.on('queueError', function (queueItem) {
if (queueItem.errid) {
toast(queueItem.link + ' - ' + i18n.t(`errors.ids.${queueItem.errid}`), 'error')
} else {
toast(queueItem.link + ' - ' + queueItem.error, 'error')
}
})
socket.on('alreadyInQueue', function (data) {
2020-09-17 22:43:52 +02:00
toast(i18n.t('toasts.alreadyInQueue', { item: data.title }), 'playlist_add_check')
})
socket.on('loginNeededToDownload', function (data) {
toast(i18n.t('toasts.loginNeededToDownload'), 'report')
})
socket.on('startGeneratingItems', function (data) {
toast(i18n.t('toasts.startGeneratingItems', { n: data.total }), 'loading', false, 'batch_' + data.uuid)
})
socket.on('finishGeneratingItems', function (data) {
toast(i18n.t('toasts.finishGeneratingItems', { n: data.total }), 'done', true, 'batch_' + data.uuid)
})