2022-11-19 01:28:11 +00:00
|
|
|
import ReconnectingWebSocket from 'reconnecting-websocket';
|
|
|
|
import Sortable from 'sortablejs';
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
const DND_DELAY = 300;
|
|
|
|
|
|
|
|
const addInteractHandler = (source, handler) => {
|
|
|
|
source.addEventListener("click", e => {
|
|
|
|
console.log("click", e);
|
|
|
|
handler(e, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
let touchStart;
|
|
|
|
source.addEventListener("touchstart", e => {
|
|
|
|
console.log("touch start", touchStart, e.timeStamp, e);
|
|
|
|
touchStart = e.timeStamp;
|
|
|
|
}, {passive: true})
|
|
|
|
|
|
|
|
source.addEventListener("touchend", e => {
|
|
|
|
console.log("touch end", touchStart, e);
|
|
|
|
console.log(e.timeStamp - touchStart)
|
|
|
|
if ((e.timeStamp - touchStart) < DND_DELAY) {
|
|
|
|
handler(e, true);
|
|
|
|
}
|
|
|
|
}, {passive: true});
|
|
|
|
}
|
|
|
|
|
2022-11-19 01:28:11 +00:00
|
|
|
window.onload = function () {
|
2022-11-18 21:18:12 +00:00
|
|
|
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")
|
2022-11-19 01:28:11 +00:00
|
|
|
const link = document.querySelector("#link")
|
|
|
|
const time = document.querySelector("#time")
|
2022-11-18 21:18:12 +00:00
|
|
|
const waiting = document.querySelector("#waiting")
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
addInteractHandler(
|
|
|
|
document.querySelector("input#next"), (e) => {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
"event": "next"
|
|
|
|
}))
|
|
|
|
})
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
addInteractHandler(
|
|
|
|
document.querySelector("input#prev"), (e) => {
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
"event": "prev"
|
|
|
|
}))
|
|
|
|
})
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
Sortable.create(items, {
|
2022-11-21 22:52:07 +00:00
|
|
|
delay: DND_DELAY,
|
2022-11-19 18:46:55 +00:00
|
|
|
delayOnTouchOnly: true,
|
2022-11-18 21:18:12 +00:00
|
|
|
group: "dndmusicbot-playlists",
|
|
|
|
filter: ".locked",
|
|
|
|
onMove: (e) => {
|
2022-11-18 21:26:28 +00:00
|
|
|
if (e.related) {
|
|
|
|
return !e.related.classList.contains('locked');
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
},
|
|
|
|
invertSwap: true,
|
|
|
|
store: {
|
2022-11-18 21:26:28 +00:00
|
|
|
get: function (sortable) {
|
2022-11-18 21:18:12 +00:00
|
|
|
var order = localStorage.getItem(sortable.options.group.name);
|
|
|
|
return order ? order.split('|') : [];
|
|
|
|
},
|
2022-11-18 21:26:28 +00:00
|
|
|
set: function (sortable) {
|
2022-11-18 21:18:12 +00:00
|
|
|
var order = sortable.toArray();
|
|
|
|
localStorage.setItem(sortable.options.group.name, order.join('|'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Sortable.create(amb, {
|
2022-11-21 22:52:07 +00:00
|
|
|
delay: DND_DELAY,
|
2022-11-19 18:46:55 +00:00
|
|
|
delayOnTouchOnly: true,
|
2022-11-18 21:18:12 +00:00
|
|
|
group: "dndmusicbot-ambiance",
|
|
|
|
filter: ".locked",
|
|
|
|
onMove: (e) => {
|
2022-11-18 21:26:28 +00:00
|
|
|
if (e.related) {
|
|
|
|
return !e.related.classList.contains('locked');
|
|
|
|
}
|
2022-11-18 21:18:12 +00:00
|
|
|
},
|
|
|
|
invertSwap: true,
|
|
|
|
store: {
|
2022-11-18 21:26:28 +00:00
|
|
|
get: function (sortable) {
|
2022-11-18 21:18:12 +00:00
|
|
|
var order = localStorage.getItem(sortable.options.group.name);
|
|
|
|
return order ? order.split('|') : [];
|
|
|
|
},
|
2022-11-18 21:26:28 +00:00
|
|
|
set: function (sortable) {
|
2022-11-18 21:18:12 +00:00
|
|
|
var order = sortable.toArray();
|
|
|
|
localStorage.setItem(sortable.options.group.name, order.join('|'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ws.onopen = (e) => {
|
2022-11-19 01:28:11 +00:00
|
|
|
waiting.classList.add("u-hidden")
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ws.onclose = (e) => {
|
2022-11-19 01:28:11 +00:00
|
|
|
waiting.classList.remove("u-hidden")
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ws.onmessage = (e) => {
|
|
|
|
data = JSON.parse(e.data)
|
|
|
|
switch (data.event) {
|
2022-11-25 13:06:26 +00:00
|
|
|
case "ambiance_add":
|
|
|
|
const container = document.querySelector("#ambiance")
|
|
|
|
var newdiv = document.createElement('div');
|
|
|
|
newdiv.className = "item"
|
|
|
|
newdiv.dataset.id = data.payload.id
|
|
|
|
newdiv.innerText = data.payload.title
|
|
|
|
addInteractHandler(newdiv, (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
|
|
|
|
|
|
|
var id = e.target.dataset.id
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
"event": ((id === "reset") ? "ambiance_stop" : "ambiance_play"),
|
|
|
|
"payload": id
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
|
|
|
container.insertBefore(newdiv, document.querySelector("#ambiance div:last-child"))
|
|
|
|
break
|
2022-11-18 21:18:12 +00:00
|
|
|
case "ambiance_play":
|
|
|
|
document.querySelectorAll("#ambiance > div").forEach((e) => {e.style.removeProperty("background-color")})
|
2022-11-25 13:06:26 +00:00
|
|
|
document.querySelector(`#ambiance > div[data-id='${data.payload.id}']`).style.backgroundColor = "burlywood"
|
2022-11-18 21:18:12 +00:00
|
|
|
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) {
|
2022-11-19 01:28:11 +00:00
|
|
|
info.classList.add("u-hidden")
|
2022-11-18 21:18:12 +00:00
|
|
|
} else {
|
2022-11-19 01:28:11 +00:00
|
|
|
info.classList.remove("u-hidden")
|
|
|
|
link.children.channel.innerText = data.payload.channel
|
|
|
|
link.children.title.innerText = data.payload.current
|
|
|
|
link.href = "https://youtu.be/" + data.payload.song
|
|
|
|
link.target = "_blank"
|
|
|
|
time.innerText = `${msToTime(data.payload.position)} / ${msToTime(data.payload.len)}`
|
2022-11-18 21:18:12 +00:00
|
|
|
document.querySelector(`#items > div[data-id='${data.payload.playlist}']`).style.backgroundColor = "burlywood"
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
|
|
|
items.style.pointerEvents = 'auto'
|
|
|
|
}, 1000);
|
|
|
|
break
|
|
|
|
case "song_position":
|
2022-11-19 01:28:11 +00:00
|
|
|
time.innerText = `${msToTime(data.payload.position)} / ${msToTime(data.payload.len)}`
|
|
|
|
info.classList.remove("u-hidden")
|
2022-11-18 21:18:12 +00:00
|
|
|
break
|
|
|
|
case "stop":
|
|
|
|
setTimeout(() => {
|
|
|
|
items.style.pointerEvents = 'auto'
|
|
|
|
}, 2000);
|
|
|
|
|
2022-11-19 01:28:11 +00:00
|
|
|
info.classList.add("u-hidden")
|
2022-11-18 21:18:12 +00:00
|
|
|
break
|
|
|
|
case "new_playlist":
|
|
|
|
addPlaylist(data.payload);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
addInteractHandler(document.querySelector("#addambiance"), (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-20 23:37:49 +00:00
|
|
|
//output.innerText = ""
|
2022-11-18 21:18:12 +00:00
|
|
|
var title = document.querySelector("#inputambiance > input[name='title']")
|
|
|
|
var url = document.querySelector("#inputambiance > input[name='url']")
|
|
|
|
if (title.value == "" || url.value == "") {
|
2022-11-20 23:37:49 +00:00
|
|
|
console.log("Title or Url is empty!")
|
2022-11-18 21:18:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.send(JSON.stringify({
|
|
|
|
"event": "ambiance_add",
|
|
|
|
"payload": {
|
|
|
|
"title": title.value,
|
|
|
|
"url": url.value
|
|
|
|
}
|
|
|
|
}))
|
2022-11-25 13:06:26 +00:00
|
|
|
|
|
|
|
title.value = ""
|
|
|
|
url.value = ""
|
2022-11-18 21:18:12 +00:00
|
|
|
})
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
addInteractHandler(submit, (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-20 23:37:49 +00:00
|
|
|
//output.innerText = ""
|
2022-11-18 21:18:12 +00:00
|
|
|
var title = document.querySelector("#inputplaylist > input[name='title']")
|
|
|
|
var url = document.querySelector("#inputplaylist > 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
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
title.innerText = ""
|
|
|
|
url.innerText = ""
|
|
|
|
})
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
document.querySelectorAll("#items .item").forEach(item => addInteractHandler(item, (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
2022-11-18 21:18:12 +00:00
|
|
|
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
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
|
2022-11-21 22:52:07 +00:00
|
|
|
document.querySelectorAll("#ambiance .item").forEach(item => addInteractHandler(item, (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
2022-11-18 21:18:12 +00:00
|
|
|
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
|
2022-11-21 22:52:07 +00:00
|
|
|
addInteractHandler(newdiv, (e, isTouch) => {
|
|
|
|
isTouch && e.preventDefault()
|
2022-11-18 21:18:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|