2020-07-04 06:22:28 +00:00
|
|
|
var requestsDiv = document.getElementById("requests");
|
|
|
|
|
|
|
|
function getRequests(count,allRequests) {
|
|
|
|
var reqUrl;
|
|
|
|
if (allRequests) {
|
|
|
|
reqUrl = "/api/getAllRequests";
|
|
|
|
} else {
|
|
|
|
reqUrl = "/api/getRequests";
|
|
|
|
}
|
|
|
|
reqUrl += `?count=${count}`;
|
|
|
|
fetch(reqUrl)
|
|
|
|
.then(response => response.json())
|
2020-11-01 20:07:16 +00:00
|
|
|
.then(requests => {
|
|
|
|
window.requests = requests;
|
|
|
|
buildTable();
|
|
|
|
});
|
2020-07-04 06:22:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function buildTable() {
|
2020-07-04 06:22:28 +00:00
|
|
|
var requestsDivHTML = '<table><tr><th class="request-url">URL</th><th class="request-requester">Requester</th><th class="request-score">Score</th>';
|
2020-11-01 20:07:16 +00:00
|
|
|
requestsDivHTML += '<th class="request-state">State</td>';
|
|
|
|
if (window.loggedIn) requestsDivHTML += '<th class="request-vote">Vote</td>';
|
|
|
|
if (window.isStreamer) requestsDivHTML += '<th class="request-update">Update</th>'
|
2020-07-04 06:22:28 +00:00
|
|
|
requestsDivHTML += "</tr>";
|
|
|
|
for (request of requests) {
|
2020-08-10 01:39:43 +00:00
|
|
|
requestsDivHTML += `<tr><td class="request-url"><a href="${request.url}" target="_blank">${request.url}</a></td>\
|
2020-08-12 19:43:30 +00:00
|
|
|
<td class="request-requester">${request.imageurl ? `<img src="${request.imageurl}" class="table-userpic"/>` : ''}${request.requester}</td>\
|
2020-07-04 06:22:28 +00:00
|
|
|
<td class="request-score">${request.score}</td>`;
|
2020-11-01 20:07:16 +00:00
|
|
|
requestsDivHTML += `<td class="request-state">${request.state}</td>`;
|
2020-08-08 04:32:06 +00:00
|
|
|
if (window.loggedIn) {
|
|
|
|
if (request.voted) {
|
|
|
|
requestsDivHTML += `<td class="request-vote"><button onclick="deleteVote('${request.url}')">Unvote</button></td>`;
|
|
|
|
} else {
|
|
|
|
requestsDivHTML += `<td class="request-vote"><button onclick="addVote('${request.url}')">Vote</button></td>`;
|
|
|
|
}
|
2020-11-01 20:07:16 +00:00
|
|
|
if (window.isStreamer) {
|
|
|
|
requestsDivHTML += '<td class="request-update"><button onclick="openUpdateRequestModal(this.parentElement.parentElement)">Update</button></td>'
|
|
|
|
}
|
2020-08-08 04:32:06 +00:00
|
|
|
}
|
2020-07-04 06:22:28 +00:00
|
|
|
requestsDivHTML += "</tr>";
|
|
|
|
}
|
|
|
|
requestsDivHTML += "</table>";
|
|
|
|
requestsDiv.innerHTML = requestsDivHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateTable() {
|
2020-11-01 20:07:16 +00:00
|
|
|
allRequests = document.getElementById("allRequests").checked;
|
|
|
|
getRequests(document.getElementById("count").value,allRequests);
|
2020-07-04 06:22:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
function addRequestErr(msg) {
|
|
|
|
document.getElementById('addRequestError').style.display = "inline-block";
|
|
|
|
document.getElementById('addRequestError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRequestErrReset() {
|
|
|
|
document.getElementById('addRequestError').style.display = "none";
|
|
|
|
document.getElementById('addRequestError').innerText = "";
|
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function updateRequestErr(msg) {
|
|
|
|
document.getElementById('updateRequestError').style.display = "inline-block";
|
|
|
|
document.getElementById('updateRequestError').innerText = msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRequestErrReset() {
|
|
|
|
document.getElementById('updateRequestError').style.display = "none";
|
|
|
|
document.getElementById('updateRequestError').innerText = "";
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
function showMessage(msg) {
|
|
|
|
document.getElementById("messageModalText").innerText = msg;
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "none";
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("messageModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeMessageModal() {
|
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2020-07-05 22:20:22 +00:00
|
|
|
function openAddRequestModal() {
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("addRequestModal").style.display = "block";
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function closeAddRequestModal() {
|
2020-07-07 04:24:04 +00:00
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function openUpdateRequestModal(tr) {
|
|
|
|
var url = tr.getElementsByClassName('request-url')[0].innerText;
|
|
|
|
var score = tr.getElementsByClassName('request-score')[0].innerText;
|
|
|
|
var state = tr.getElementsByClassName('request-state')[0].innerText;
|
|
|
|
document.getElementById("updateRequestUrl").href = url;
|
|
|
|
document.getElementById("updateRequestUrl").innerText = url;
|
|
|
|
document.getElementById("updateRequestModalCurrentScore").innerText = score;
|
|
|
|
document.querySelector(`#updateRequestStateSelect [value="${state}"]`).selected = true;
|
|
|
|
document.getElementById("scoreModifierInput").value = 0;
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeUpdateRequestModal() {
|
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
function openDeleteRequestModal(url) {
|
|
|
|
document.getElementById("updateRequestUrl").href = url;
|
|
|
|
document.getElementById("updateRequestUrl").innerText = url;
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("modalBackground").style.display = "flex";
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeDeleteRequestModal() {
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "block";
|
|
|
|
}
|
|
|
|
|
|
|
|
function closeAllModals() {
|
|
|
|
document.getElementById("messageModal").style.display = "none";
|
|
|
|
document.getElementById("addRequestModal").style.display = "none";
|
|
|
|
document.getElementById("updateRequestModal").style.display = "none";
|
|
|
|
document.getElementById("deleteRequestModal").style.display = "none";
|
|
|
|
document.getElementById("modalBackground").style.display = "none";
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:24:04 +00:00
|
|
|
const validUrlRegexes = [
|
|
|
|
/^https:\/\/www\.youtube\.com\/watch\?v=[a-zA-Z0-9_-]{11}$/
|
|
|
|
];
|
|
|
|
function validateAndSubmitRequest() {
|
|
|
|
addRequestErrReset();
|
|
|
|
var url = document.getElementById("addRequestUrl").value;
|
|
|
|
var validUrl = false;
|
|
|
|
for (var regex of validUrlRegexes) {
|
|
|
|
if (regex.test(url)) {
|
|
|
|
validUrl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validUrl) {
|
|
|
|
addRequestErr("Invalid URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetch("/api/addRequest", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
2020-08-08 04:32:06 +00:00
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
updateTable();
|
|
|
|
document.getElementById("addRequestUrl").value = "";
|
|
|
|
response.text().then((message) => {
|
|
|
|
closeAddRequestModal();
|
|
|
|
showMessage(message);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addVote(url) {
|
|
|
|
fetch("/api/addVote", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
2020-09-20 01:14:58 +00:00
|
|
|
response.text().then(showMessage);
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteVote(url) {
|
|
|
|
fetch("/api/deleteVote", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
2020-07-07 04:24:04 +00:00
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(addRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
2020-07-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 20:07:16 +00:00
|
|
|
function updateRequestState(url,state) {
|
|
|
|
updateRequestErrReset();
|
|
|
|
fetch("/api/updateRequestState", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url,
|
|
|
|
state: state
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(updateRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateRequestScoreModifier(url,scoreDiff) {
|
|
|
|
updateRequestErrReset();
|
|
|
|
fetch("/api/updateRequestScoreModifier", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url,
|
|
|
|
scoreDiff: scoreDiff
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
response.text().then(updateRequestErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteRequest(url) {
|
|
|
|
fetch("/api/deleteRequest", { method: 'POST', body: new URLSearchParams({
|
|
|
|
url: url
|
|
|
|
})})
|
|
|
|
.then(response => {
|
|
|
|
updateTable();
|
|
|
|
response.text().then(showMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-04 06:22:28 +00:00
|
|
|
updateTable();
|
2020-07-06 03:41:42 +00:00
|
|
|
|
|
|
|
document.addEventListener("keydown", function onEvent(event) {
|
|
|
|
if (event.key === "Escape") {
|
2020-07-07 04:24:04 +00:00
|
|
|
closeMessageModal();
|
2020-07-06 03:41:42 +00:00
|
|
|
closeAddRequestModal();
|
|
|
|
}
|
|
|
|
});
|
2020-11-01 20:07:16 +00:00
|
|
|
document.getElementById("modalBackground").addEventListener("click", (e) => { if (e.target === e.currentTarget) closeAllModals();});
|
|
|
|
|
|
|
|
var updateRequestStateSelect = document.getElementById("updateRequestStateSelect");
|
|
|
|
for(state of validStates) {
|
|
|
|
var opt = document.createElement("option");
|
|
|
|
opt.text = state;
|
|
|
|
opt.value = state;
|
|
|
|
updateRequestStateSelect.add(opt)
|
|
|
|
}
|