Use bootstrap-modal for single-calc.

This commit is contained in:
MaxJa4
2024-01-25 15:18:12 +01:00
parent 130fe2ad57
commit acf756765b
6 changed files with 119 additions and 51 deletions

View File

@@ -47,6 +47,7 @@ func init() {
"./templates/modals/add_player.html", "./templates/modals/add_player.html",
"./templates/modals/delete_player.html", "./templates/modals/delete_player.html",
"./templates/modals/edit_player.html", "./templates/modals/edit_player.html",
"./templates/modals/single_calc.html",
//"./templates/modals/settings.html", //"./templates/modals/settings.html",
"./templates/modals/full_calc.html", "./templates/modals/full_calc.html",
"./templates/components/header.html", "./templates/components/header.html",

View File

@@ -9,45 +9,6 @@ const swalClasses = {
htmlContainer: 'fs-5 text-center text-secondary-emphasis', htmlContainer: 'fs-5 text-center text-secondary-emphasis',
}; };
function showSingleCalcDialog(btn) {
Swal.fire({
title: 'Einzel-Abfrage',
input: 'text',
inputLabel: 'Welcher Spieler soll abgefragt werden?\nDie Abfrage kann ein paar Sekunden dauern.',
inputPlaceholder: 'Spieler-Name eingeben...',
confirmButtonText: 'Berechnen',
showCancelButton: true,
cancelButtonText: 'Abbrechen',
customClass: swalClasses,
buttonsStyling: false
}).then((result) => {
if (result.isConfirmed) {
let promise = htmx.ajax('POST', '/score/' + result.value, {target: '#' + btn.id, swap: 'none'});
}
});
}
function showSingleCalcResultDialog(xhr, reqPath) {
let icon;
switch (xhr.status) {
case 200:
icon = 'success';
break;
case 404:
icon = 'warning';
break;
default:
icon = 'error';
}
Swal.fire({
title: 'Abfrage für "' + reqPath.replace("/score/", '') + '"',
text: "Score: " + xhr.response,
icon: icon,
customClass: swalClasses
});
}
function showAdminActionExecutedDialog(xhr, method) { function showAdminActionExecutedDialog(xhr, method) {
Swal.fire({ Swal.fire({
title: 'Action executed', title: 'Action executed',

View File

@@ -69,7 +69,7 @@
</button> </button>
</div> </div>
<div class="col-auto position-absolute end-0"> <div class="col-auto position-absolute end-0">
<button class="btn btn-lg btn-outline-secondary text-secondary-emphasis" id="singleCalcBtn" onclick="showSingleCalcDialog(this)" data-bs-action="tooltip" data-bs-title="Einzelnen Spieler berechnen"> <button class="btn btn-lg btn-outline-secondary text-secondary-emphasis" id="singleCalcBtn" data-bs-toggle="modal" data-bs-target="#singleCalcModal" data-bs-action="tooltip" data-bs-title="Einzelnen Spieler berechnen">
<i class="bi bi-person-fill me-2"></i> <i class="bi bi-person-fill me-2"></i>
Einzel-Abfrage Einzel-Abfrage
</button> </button>
@@ -85,8 +85,6 @@
if (reqPath.startsWith("/admin/")) { if (reqPath.startsWith("/admin/")) {
showAdminActionExecutedDialog(xhr, method); showAdminActionExecutedDialog(xhr, method);
} else if (reqPath.startsWith("/score/") && method === "post") {
showSingleCalcResultDialog(xhr, reqPath);
} }
}); });
</script> </script>

View File

@@ -51,7 +51,8 @@
{{/* template "settings" . */}} {{/* template "settings" . */}}
{{template "full_calc" .}} {{ template "full_calc" . }}
{{ template "single_calc" . }}
<script lang="javascript"> <script lang="javascript">
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {

View File

@@ -41,8 +41,6 @@
const homeClanList = document.getElementById('home-clan'); const homeClanList = document.getElementById('home-clan');
const oppClanList = document.getElementById('opponent-clan'); const oppClanList = document.getElementById('opponent-clan');
const spinner = '<span class="spinner-border spinner-border-sm me-2" role="status"></span>Lade Spielerdaten...'
function validateInput() { function validateInput() {
if (playerName.value.length < 1) { if (playerName.value.length < 1) {
playerName.classList.add('is-invalid'); playerName.classList.add('is-invalid');
@@ -59,9 +57,6 @@
const clanId = parseInt(selectedClan.value); const clanId = parseInt(selectedClan.value);
submitButton.innerHTML = spinner;
submitButton.disabled = true;
fetch("/player", { fetch("/player", {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
@@ -93,9 +88,6 @@
}).catch((error) => { }).catch((error) => {
errorDiv.innerText = error.message; errorDiv.innerText = error.message;
errorDiv.style.display = 'block'; errorDiv.style.display = 'block';
}).finally(() => {
submitButton.innerHTML = 'Hinzufügen';
submitButton.disabled = false;
}); });
} }
} }

View File

@@ -0,0 +1,115 @@
{{ define "single_calc" }}
<div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="singleCalcModal" tabindex="-1">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h1 class="modal-title fs-3 text-success fw-bold" id="singleCalcModalLabel">Einzel-Abfrage</h1>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="form-floating">
<input type="text" class="form-control form-control-lg pb-1" id="playerName" placeholder="Spieler-Name" required>
<label for="playerName">Spieler-Name</label>
</div>
<div class="text-warning fs-5 badge mt-2" id="resultScore" style="display: none;"></div>
<div class="error-message text-danger fs-5 badge mt-2" style="display: none;"></div>
</div>
<div class="modal-footer">
<button type="submit" name="submit" class="btn btn-lg btn-primary">Berechnen</button>
<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Schließen</button>
</div>
</div>
</div>
</div>
<script lang="javascript">
document.addEventListener('DOMContentLoaded', function() {
const singleCalcModal = document.getElementById('singleCalcModal');
let submitScoreHandler = null;
const submitButton = singleCalcModal.querySelector('button[name="submit"]');
const playerName = singleCalcModal.querySelector('#playerName');
const errorDiv = singleCalcModal.querySelector('.error-message');
const resultScore = singleCalcModal.querySelector('#resultScore');
const spinner = '<span class="spinner-border spinner-border-sm me-2" role="status"></span>Lade Daten...'
function validateInput() {
if (playerName.value.length < 1) {
playerName.classList.add('is-invalid');
return false;
}
playerName.classList.remove('is-invalid');
return true;
}
function createSubmitScoreHandler(_) {
return function submitScoreHandler(_) {
if (!validateInput())
return;
submitButton.innerHTML = spinner;
submitButton.disabled = true;
resultScore.style.display = 'none';
playerName.disabled = true;
fetch("/score/" + playerName.value, {
method: "POST",
body: JSON.stringify({
name: playerName.value
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => {
if (response.status === 404) {
throw new Error('Berechnung fehlgeschlagen!\nSpielername existiert nicht.');
} else if (response.status === 503 || response.status === 504) {
throw new Error('Berechnung fehlgeschlagen!\nTracker überlastet. Versuche es später erneut.');
} else if (!response.ok) {
throw new Error('Berechnung fehlgeschlagen!\nName ungültig oder Tracker nicht erreichbar.');
}
return response.text();
})
.then((response) => {
resultScore.innerText = 'Spieler hat ' + response + ' Punkte.';
resultScore.style.display = 'block';
}).catch((error) => {
errorDiv.innerText = error.message;
errorDiv.style.display = 'block';
}).finally(() => {
submitButton.innerHTML = 'Berechnen';
submitButton.disabled = false;
playerName.disabled = false;
});
}
}
if (singleCalcModal) {
singleCalcModal.addEventListener('show.bs.modal', event => {
submitScoreHandler = createSubmitScoreHandler(event);
submitButton.addEventListener('click', submitScoreHandler);
});
singleCalcModal.addEventListener('keypress', event => {
if (event.key === 'Enter') {
submitButton.click();
}
});
singleCalcModal.addEventListener('hidden.bs.modal', _ => {
submitButton.removeEventListener('click', submitScoreHandler);
playerName.value = "";
errorDiv.innerText = "";
errorDiv.style.display = 'none';
resultScore.innerText = "";
resultScore.style.display = 'none';
playerName.classList.remove('is-invalid');
});
}
});
</script>
{{ end }}