dndmusicbot/js/script.js

205 lines
5.8 KiB
JavaScript

window.onload = function() {
const ws = new ReconnectingWebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") + window.location.host + "/ws");
const items = document.querySelector("#items");
const amb = document.querySelector("#ambiance")
const submit = document.querySelector("#addplaylist")
const output = document.querySelector(".container2 p#output")
const info = document.querySelector("#info")
const waiting = document.querySelector("#waiting")
document.querySelector("input#next").addEventListener("click", (e) => {
ws.send(JSON.stringify({
"event": "next"
}))
})
document.querySelector("input#prev").addEventListener("click", (e) => {
ws.send(JSON.stringify({
"event": "prev"
}))
})
Sortable.create(items, {
group: "dndmusicbot-playlists",
filter: ".locked",
onMove: (e) => {
if (e.related)
{
return !e.related.classList.contains('locked');
}
},
invertSwap: true,
store: {
get: function(sortable) {
var order = localStorage.getItem(sortable.options.group.name);
return order ? order.split('|') : [];
},
set: function(sortable) {
var order = sortable.toArray();
localStorage.setItem(sortable.options.group.name, order.join('|'));
}
}
})
Sortable.create(amb, {
group: "dndmusicbot-ambiance",
filter: ".locked",
onMove: (e) => {
if (e.related)
{
return !e.related.classList.contains('locked');
}
},
invertSwap: true,
store: {
get: function(sortable) {
var order = localStorage.getItem(sortable.options.group.name);
return order ? order.split('|') : [];
},
set: function(sortable) {
var order = sortable.toArray();
localStorage.setItem(sortable.options.group.name, order.join('|'));
}
}
})
ws.onopen = (e) => {
waiting.style.display = "none";
}
ws.onclose = (e) => {
waiting.style.display = "block";
}
ws.onmessage = (e) => {
data = JSON.parse(e.data)
switch (data.event) {
case "ambiance_play":
document.querySelectorAll("#ambiance > div").forEach((e) => {e.style.removeProperty("background-color")})
document.querySelector(`#ambiance > div[data-id='${data.payload.type}']`).style.backgroundColor = "burlywood"
ambiance.style.pointerEvents = 'auto'
break
case "ambiance_stop":
document.querySelectorAll("#ambiance > div").forEach((el) => {
el.style.removeProperty("background-color")
})
break
case "song_info":
document.querySelectorAll("#items > div").forEach((el) => {
el.style.removeProperty("background-color")
})
if (data.payload.pause) {
info.style.display = "none"
} else {
info.style.display = "block"
info.children.link.children.channel.innerText = data.payload.channel
info.children.link.children.title.innerText = data.payload.current
info.children.link.href = "https://youtu.be/" + data.payload.song
info.children.link.target = "_blank"
info.children.time.innerText = `( ${msToTime(data.payload.position)} / ${msToTime(data.payload.len)} )`
document.querySelector(`#items > div[data-id='${data.payload.playlist}']`).style.backgroundColor = "burlywood"
}
setTimeout(() => {
items.style.pointerEvents = 'auto'
}, 1000);
break
case "song_position":
info.children.time.innerText = `( ${msToTime(data.payload.position)} / ${msToTime(data.payload.len)} )`
info.style.display = "block"
break
case "stop":
setTimeout(() => {
items.style.pointerEvents = 'auto'
}, 2000);
info.style.display = "none"
break
case "new_playlist":
addPlaylist(data.payload);
break;
default:
}
}
submit.addEventListener("click", (e) => {
e.preventDefault()
output.innerText = ""
var title = document.querySelector(".container2 input[name='title'")
var url = document.querySelector(".container2 input[name='url'")
if (title.value == "" || url.value == "") {
output.innerText = "Title or Url is empty!"
return
}
ws.send(JSON.stringify({
"event": "add_playlist",
"payload": {
"title": title.value,
"url": url.value
}
}))
})
document.querySelectorAll("#items").forEach(item => item.addEventListener("click", (e) => {
e.preventDefault()
e.target.parentElement.style.pointerEvents = 'none'
const disableui = setTimeout((t) => {
t.style.pointerEvents = 'auto'
}, 3000, e.target.parentElement);
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "stop" : "load_playlist"),
"payload": id
}))
}));
document.querySelectorAll("#ambiance").forEach(item => item.addEventListener("click", (e) => {
e.preventDefault()
e.target.parentElement.style.pointerEvents = 'none'
const disableui = setTimeout((t) => {
t.style.pointerEvents = 'auto'
}, 3000, e.target.parentElement);
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "ambiance_stop" : "ambiance_play"),
"payload": id
}))
}));
};
function addPlaylist(payload) {
const container = document.querySelector("body > div.container")
var newdiv = document.createElement('div');
newdiv.className = "item"
newdiv.dataset.id = payload.url
newdiv.innerText = payload.title
newdiv.addEventListener("click", (e) => {
e.preventDefault()
var id = e.target.dataset.id
ws.send(JSON.stringify({
"event": ((id === "reset") ? "stop" : "load_playlist"),
"payload": id
}))
})
container.insertBefore(newdiv, document.querySelector("body > div.container div:last-child"))
output.innerText = "New playlist was added: " + payload.title
}
function msToTime(duration) {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = Math.floor((duration / 1000) % 60),
minutes = Math.floor((duration / (1000 * 60)) % 60),
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return minutes + ":" + seconds
}