Praxispunkte: groessere Klickflaeche, Zaehler und Klick-Handler statt Submit
This commit is contained in:
parent
3795c517fb
commit
1acb99dc51
4 changed files with 29 additions and 15 deletions
|
|
@ -201,31 +201,42 @@
|
|||
});
|
||||
}
|
||||
|
||||
// Praxispunkte im Charakterbogen: Klick setzt die Punkte ohne Reload
|
||||
document.addEventListener('submit', async function (event) {
|
||||
const form = event.target.closest('[data-pp-form]');
|
||||
if (!form) {
|
||||
// Praxispunkte im Charakterbogen: Klick setzt die Punkte ohne Reload.
|
||||
// Der Klick wird abgefangen, damit die Kästchen sofort reagieren; scheitert
|
||||
// der Aufruf, wird das Formular normal abgeschickt (funktioniert ohne JS).
|
||||
document.addEventListener('click', async function (event) {
|
||||
const slot = event.target.closest('.pp-slot');
|
||||
if (!slot) {
|
||||
return;
|
||||
}
|
||||
const form = slot.closest('[data-pp-form]');
|
||||
if (!form || typeof window.fetch !== 'function') {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
const pressed = event.submitter;
|
||||
const want = pressed ? parseInt(pressed.value, 10) || 0 : 0;
|
||||
const body = new FormData(form);
|
||||
body.set('pp', slot.value);
|
||||
try {
|
||||
const response = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
body: new FormData(form),
|
||||
body: body,
|
||||
headers: { 'X-Requested-With': 'fetch' }
|
||||
});
|
||||
const data = await response.json();
|
||||
if (!data || data.ok !== true) {
|
||||
form.submit();
|
||||
return;
|
||||
}
|
||||
form.querySelectorAll('.pp-slot').forEach(function (slot) {
|
||||
const value = parseInt(slot.value, 10) || 0;
|
||||
form.querySelectorAll('.pp-slot').forEach(function (button) {
|
||||
const value = parseInt(button.value, 10) || 0;
|
||||
const filled = value <= data.pp;
|
||||
slot.classList.toggle('is-filled', filled);
|
||||
slot.setAttribute('aria-pressed', filled ? 'true' : 'false');
|
||||
button.classList.toggle('is-filled', filled);
|
||||
button.setAttribute('aria-pressed', filled ? 'true' : 'false');
|
||||
});
|
||||
const count = form.querySelector('.pp-count');
|
||||
if (count) {
|
||||
count.textContent = data.pp + '/' + form.querySelectorAll('.pp-slot').length;
|
||||
}
|
||||
} catch (error) {
|
||||
form.submit();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue