deemixer/public/js/init.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

// Initialization
const socket = io.connect(window.location.href)
localStorage = window.localStorage;
search_selected = ""
main_selected=""
2020-04-11 10:51:22 +00:00
toastsWithId = {}
function toast(msg, icon=null, dismiss=true, id=null){
2020-04-11 19:55:12 +00:00
if (toastsWithId[id]){
toastObj = toastsWithId[id]
toastDOM = $(`div.toastify[toast_id=${id}]`)
if (msg){
toastDOM.find(".toast-message").html(msg)
}
if (icon){
if (icon=='loading')
icon = `<div class="circle-loader"></div>`
else
icon = `<i class="material-icons">${icon}</i>`
toastDOM.find(".toast-icon").html(icon)
}
if (dismiss !== null && dismiss){
setTimeout(function(){ toastObj.hideToast() }, 3000);
}
}else{
if (icon == null)
icon = ""
else if (icon=='loading')
icon = `<div class="circle-loader"></div>`
else
icon = `<i class="material-icons">${icon}</i>`
toastObj = Toastify({
text: `<span class="toast-icon">${icon}</span><span class="toast-message">${msg}</toast>`,
duration: dismiss ? 3000 : 0,
gravity: 'bottom',
position: 'left'
}).showToast()
if (id){
toastsWithId[id] = toastObj
$(toastObj.toastElement).attr('toast_id', id)
}
2020-04-11 10:51:22 +00:00
}
}
socket.on("toast", (data)=>{
toast(data.msg, data.icon || null, data.dismiss !== undefined ? data.dismiss : true, data.id || null)
})
socket.on("updateToast", (data)=>{
2020-04-11 19:55:12 +00:00
toast(data.msg, data.icon || null, data.dismiss !== undefined ? data.dismiss : true, data.id || null)
})
function openLoginPrompt(){
socket.emit("loginpage")
}
socket.emit("init");
if (localStorage.getItem("arl")){
socket.emit("login", localStorage.getItem("arl"));
}
socket.on("logging_in", function(){
toast("Logging in", "loading", false, "login-toast")
})
socket.on("logged_in", function(data){
if (data.status != 0){
console.log(data)
toast("Logged in", "done", true, "login-toast")
if (data.arl && data.status == 1) localStorage.setItem("arl", data.arl)
2020-04-11 10:51:22 +00:00
}else{
2020-04-11 19:55:12 +00:00
toast("Couldn't log in", "close", true, "login-toast")
2020-04-11 10:51:22 +00:00
}
})