FestivalServer/Public/festival/assets/festival.js

125 lines
4.0 KiB
JavaScript
Raw Normal View History

2022-03-26 09:55:22 +01:00
var isPlaying = false;
2022-03-26 22:53:18 +01:00
registeredName = localStorage.getItem('name');
didAcceptInvitation = localStorage.getItem('registered');
2022-03-26 22:27:23 +01:00
var currentLang = 'de';
if (window.navigator.language != 'de') {
currentLang = 'en';
}
function localize() {
let lang = ':lang(' + currentLang + ')';
let show = '[lang]' + lang;
document.querySelectorAll(show).forEach(function (node) {
node.style.display = 'unset';
});
let hide = '[lang]:not(' + lang + ')';
document.querySelectorAll(hide).forEach(function (node) {
node.style.display = 'none';
});
}
2021-11-20 12:19:50 +01:00
function showRegisterForm() {
document.getElementById("text-container").style.top = '2%'
document.getElementById("register-button").style.opacity = 0
document.getElementById("participation-buttons").style.display = 'inline-block'
document.getElementById("name-form").style.display = 'inherit'
document.getElementById("add-calender-event").style.display = 'none'
document.getElementById("guest-name").focus()
2022-03-26 22:27:23 +01:00
document.getElementById("info-button").style.display = 'none';
2022-03-26 09:55:22 +01:00
if (!isPlaying) {
playPause();
}
}
function playPause() {
2021-11-20 12:19:50 +01:00
var audio = document.getElementById("audio");
2022-03-26 09:55:22 +01:00
var playButton = document.getElementById("play-button");
var pauseButton = document.getElementById("pause-button");
var playText = document.getElementById("playing-text");
var pauseText = document.getElementById("paused-text");
if (isPlaying) {
audio.pause();
playButton.style.display = "inherit";
pauseButton.style.display = "none";
playText.style.display = "none";
pauseText.style.display = "table-cell";
} else {
audio.play();
playButton.style.display = "none";
pauseButton.style.display = "inherit";
playText.style.display = "table-cell";
pauseText.style.display = "none";
}
isPlaying = !isPlaying;
2021-11-20 12:19:50 +01:00
}
async function participate(isParticipating) {
let value = document.getElementById("guest-name").value
if (!value) {
return
}
let name = value.trim()
document.getElementById("participation-buttons").style.display = 'none'
document.getElementById("name-form").style.display = 'none'
2022-03-26 22:27:23 +01:00
document.getElementById("info-button").style.display = 'inherit'
2021-11-20 12:19:50 +01:00
console.log("Registering: " + name);
2022-03-26 22:53:18 +01:00
localStorage.removeItem('name');
localStorage.removeItem('registered');
2021-11-20 12:19:50 +01:00
if (isParticipating) {
let registered = await performRequest(name, "register")
if (registered) {
2022-03-26 22:53:18 +01:00
document.getElementById("registered-text").style.display = 'inherit';
document.getElementById("add-calender-event").style.display = 'inherit';
localStorage.setItem('name', name);
localStorage.setItem('registered', 'true');
2021-11-20 12:19:50 +01:00
} else {
document.getElementById("error-text").style.display = 'inherit'
}
} else {
let declined = await performRequest(name, "decline")
if (declined) {
2022-03-26 22:53:18 +01:00
document.getElementById("declined-text").style.display = 'inherit';
localStorage.setItem('name', name);
localStorage.setItem('registered', 'false');
2021-11-20 12:19:50 +01:00
} else {
document.getElementById("error-text").style.display = 'inherit'
}
}
}
function handleErrors(response) {
if (!response.ok) throw new Error(response.status);
return response.text();
}
function catchErrors(error) {
console.log(error);
return "Failed";
}
async function performRequest(name, path) {
2022-02-10 16:18:50 +01:00
let text = await fetch("/festival/api/" + path, {
2021-11-20 12:19:50 +01:00
method: 'POST',
body: name
}).then(handleErrors).catch(catchErrors);
if (text == "Success") {
console.log("Registered: " + name);
return true;
}
console.log("Failed to register: " + text);
return false;
}
2022-03-26 22:27:23 +01:00
function downloadCalender() {
if (currentLang == 'de') {
download('/festival/event.ics')
} else {
download('/festival/event-en.ics')
}
}
2022-02-10 16:18:50 +01:00
function download(file) {
window.location = file
}