deemixer/public/js/app.js

97 lines
2.7 KiB
JavaScript
Raw Normal View History

import { socket } from './modules/socket.js'
import { toast } from './modules/toasts.js'
import Downloads from './modules/downloads.js'
import QualityModal from './modules/quality-modal.js'
2020-04-23 19:03:12 +00:00
import { Tabs } from './modules/tabs.js'
import Search from './modules/search.js'
2020-04-25 09:01:30 +00:00
import { initTrackPreview } from './modules/track-preview.js'
2020-04-19 20:02:06 +00:00
/* ===== Socketio listeners ===== */
// Debug messages for socketio
socket.on('message', function (msg) {
console.log(msg)
})
socket.on('logging_in', function () {
toast('Logging in', 'loading', false, 'login-toast')
})
socket.on('logged_in', function (data) {
switch (data.status) {
case 1:
case 3:
toast('Logged in', 'done', true, 'login-toast')
if (data.arl) {
localStorage.setItem('arl', data.arl)
$('#login_input_arl').val(data.arl)
}
$('#open_login_prompt').hide()
if (data.user) {
$('#settings_username').text(data.user.name)
$('#settings_picture').attr(
'src',
`https://e-cdns-images.dzcdn.net/images/user/${data.user.picture}/125x125-000000-80-0-0.jpg`
)
$('#logged_in_info').show()
}
break
case 2:
toast('Already logged in', 'done', true, 'login-toast')
if (data.user) {
$('#settings_username').text(data.user.name)
$('#settings_picture').attr(
'src',
`https://e-cdns-images.dzcdn.net/images/user/${data.user.picture}/125x125-000000-80-0-0.jpg`
)
$('#logged_in_info').show()
}
break
case 0:
toast("Couldn't log in", 'close', true, 'login-toast')
localStorage.removeItem('arl')
$('#login_input_arl').val('')
$('#open_login_prompt').show()
$('#logged_in_info').hide()
$('#settings_username').text('Not Logged')
$('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
break
}
})
socket.on('logged_out', function () {
toast('Logged out', 'done', true, 'login-toast')
localStorage.removeItem('arl')
$('#login_input_arl').val('')
$('#open_login_prompt').show()
$('#logged_in_info').hide()
$('#settings_username').text('Not Logged')
$('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
})
/* ===== App initialization ===== */
2020-04-23 19:03:12 +00:00
function startApp() {
Downloads.linkListeners()
QualityModal.init()
2020-04-23 19:03:12 +00:00
Tabs.linkListeners()
Search.linkListeners()
2020-04-25 09:01:30 +00:00
initTrackPreview()
2020-04-19 20:02:06 +00:00
if (localStorage.getItem('arl')) {
let arl = localStorage.getItem('arl')
socket.emit('login', arl)
$('#login_input_arl').val(arl)
}
// Check if download tab should be open
if ('true' === localStorage.getItem('downloadTabOpen')) {
document.querySelector('#download_tab_container').classList.remove('tab_hidden')
}
// Open default tab
document.getElementById('main_home_tablink').click()
}
2020-04-23 19:03:12 +00:00
document.addEventListener('DOMContentLoaded', startApp)