rpg-westreiche/westreiche_website/assets/app.js

330 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Westreiche – wenig JavaScript, nur Komfort. */
(function () {
'use strict';
// Löschbestätigungen über data-confirm
document.addEventListener('click', function (event) {
const trigger = event.target.closest('[data-confirm]');
if (!trigger) {
return;
}
if (!window.confirm(trigger.getAttribute('data-confirm'))) {
event.preventDefault();
}
});
// Einladungslink kopieren
document.querySelectorAll('[data-copy]').forEach(function (button) {
button.addEventListener('click', async function () {
const target = document.querySelector(button.getAttribute('data-copy'));
if (!target) {
return;
}
const text = (target.textContent || '').trim();
try {
await navigator.clipboard.writeText(text);
const label = button.textContent;
button.textContent = 'Kopiert';
setTimeout(function () {
button.textContent = label;
}, 1800);
} catch (error) {
window.prompt('Link kopieren:', text);
}
});
});
// Attributboni live anzeigen
const bonus = function (value) {
if (value <= 3) return -3;
if (value <= 5) return -2;
if (value <= 8) return -1;
if (value <= 12) return 0;
if (value <= 15) return 1;
if (value <= 17) return 2;
return 3;
};
document.querySelectorAll('[data-attr]').forEach(function (input) {
const out = document.querySelector('[data-attr-bonus="' + input.name + '"]');
if (!out) {
return;
}
const update = function () {
const value = parseInt(input.value, 10) || 0;
const b = bonus(value);
out.textContent = b > 0 ? '+' + b : String(b);
out.classList.toggle('bonus-pos', b > 0);
out.classList.toggle('bonus-neg', b < 0);
out.classList.toggle('bonus-zero', b === 0);
};
input.addEventListener('input', update);
update();
});
// Trefferpunkte: Rechenkette anzeigen, im Overlay je Wurf ein Feld
const hpWidget = document.querySelector('[data-hp-widget]');
if (hpWidget) {
const hpInput = hpWidget.querySelector('[data-hp-input]');
const totalField = hpWidget.querySelector('[data-hp-total]');
const sumOut = hpWidget.querySelector('[data-hp-sum]');
const dialog = document.querySelector('#hp-rolls-dialog');
const dialogSum = dialog ? dialog.querySelector('[data-hp-dialog-sum]') : null;
const slots = dialog ? Array.from(dialog.querySelectorAll('[data-hp-slot]')) : [];
const die = parseInt(hpWidget.getAttribute('data-hp-die'), 10) || 4;
const baseDie = parseInt(hpWidget.getAttribute('data-hp-base'), 10) || 4;
const con = parseInt(hpWidget.getAttribute('data-hp-con'), 10) || 0;
const parse = function (text) {
return (String(text || '').match(/[0-9]+/g) || []).map(function (token) {
return parseInt(token, 10) || 0;
});
};
const values = function () {
return slots.map(function (input) {
const value = parseInt(input.value, 10);
return isNaN(value) || value < 1 ? 0 : value;
});
};
const chainText = function (list) {
let sum = 0;
const parts = list.map(function (value) {
sum += value;
return value > 0 ? String(value) : '–';
});
let chain = parts.join(' + ');
if (con !== 0) {
chain += (con > 0 ? ' + ' : ' − ') + Math.abs(con) + ' KON';
}
return { text: chain + ' = ' + Math.max(1, sum + con), sum: sum };
};
const paint = function (list) {
const info = chainText(list);
if (sumOut) {
sumOut.textContent = info.text;
}
if (dialogSum) {
dialogSum.textContent = info.text;
}
if (totalField) {
totalField.value = String(Math.max(1, info.sum + con));
}
};
// Kette aus dem gespeicherten Feld in die Eingabefelder des Overlays legen
const load = function () {
const stored = parse(hpInput ? hpInput.value : '');
slots.forEach(function (input, index) {
const value = stored[index] || 0;
input.value = value > 0 ? String(value) : '';
});
};
load();
paint(values());
slots.forEach(function (input) {
input.addEventListener('input', function () {
paint(values());
});
});
const apply = function () {
const list = values();
while (list.length > 0 && list[list.length - 1] === 0) {
list.pop();
}
if (hpInput) {
hpInput.value = list.join(' ');
}
paint(list.length > 0 ? list : [0]);
if (dialog && dialog.open) {
dialog.close();
}
};
if (dialog) {
const openButton = hpWidget.querySelector('[data-dialog-open]');
if (openButton) {
openButton.addEventListener('click', load);
}
const applyButton = dialog.querySelector('[data-hp-apply]');
if (applyButton) {
applyButton.addEventListener('click', apply);
}
// Enter im Overlay übernimmt die Würfe, statt den Charakterbogen abzuschicken
dialog.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
event.preventDefault();
apply();
}
});
const rollButton = dialog.querySelector('[data-hp-roll]');
if (rollButton) {
rollButton.addEventListener('click', function () {
slots.forEach(function (input, index) {
if (!input.value) {
input.value = String(1 + Math.floor(Math.random() * (index === 0 ? baseDie : die)));
}
});
paint(values());
});
}
}
}
// Zauberdetails im Bogen: Klick auf den Namen füllt den Detail-Schirm
const spellDetail = document.querySelector('#spell-detail');
if (spellDetail) {
const fields = ['name', 'line', 'classes', 'duration', 'range', 'note', 'summary', 'body'];
document.querySelectorAll('[data-spell-detail]').forEach(function (button) {
button.addEventListener('click', function () {
fields.forEach(function (key) {
const out = spellDetail.querySelector('[data-spell-field="' + key + '"]');
if (!out) {
return;
}
const value = (button.getAttribute('data-' + key) || '').trim();
out.textContent = value !== '' ? value : (key === 'body' || key === 'summary' ? '' : '–');
if (key === 'summary' || key === 'body') {
out.hidden = value === '';
}
});
const empty = spellDetail.querySelector('[data-spell-field="empty"]');
if (empty) {
const hasCatalog = (button.getAttribute('data-body') || '').trim() !== ''
|| (button.getAttribute('data-summary') || '').trim() !== ''
|| (button.getAttribute('data-duration') || '').trim() !== ''
|| (button.getAttribute('data-range') || '').trim() !== '';
empty.hidden = hasCatalog;
}
if (typeof spellDetail.showModal === 'function' && !spellDetail.open) {
spellDetail.showModal();
}
});
});
}
// 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) {
return;
}
event.preventDefault();
const pressed = event.submitter;
const want = pressed ? parseInt(pressed.value, 10) || 0 : 0;
try {
const response = await fetch(form.action, {
method: 'POST',
body: new FormData(form),
headers: { 'X-Requested-With': 'fetch' }
});
const data = await response.json();
if (!data || data.ok !== true) {
return;
}
form.querySelectorAll('.pp-slot').forEach(function (slot) {
const value = parseInt(slot.value, 10) || 0;
const filled = value <= data.pp;
slot.classList.toggle('is-filled', filled);
slot.setAttribute('aria-pressed', filled ? 'true' : 'false');
});
} catch (error) {
form.submit();
}
});
// Selektor-Schirme: Fertigkeiten und Zauber aus dem Katalog wählen
// Blendet Überschriften aus, wenn keine ihrer Einträge sichtbar ist.
const hideEmptyGroups = function (dialog) {
dialog.querySelectorAll('.selector-group').forEach(function (heading) {
let node = heading.nextElementSibling;
let anyVisible = false;
while (node && !node.classList.contains('selector-group')) {
if (node.hasAttribute('data-selector-item') && !node.hidden) {
anyVisible = true;
break;
}
node = node.nextElementSibling;
}
heading.hidden = !anyVisible;
});
};
const filterSelector = function (dialog) {
const search = dialog.querySelector('[data-selector-search]');
const school = dialog.querySelector('[data-selector-school]');
const grade = dialog.querySelector('[data-selector-grade]');
const query = search ? search.value.trim().toLowerCase() : '';
const wantSchool = school ? school.value : '';
const wantGrade = grade ? grade.value : '';
let visible = 0;
dialog.querySelectorAll('[data-selector-item]').forEach(function (item) {
const name = item.getAttribute('data-name') || '';
const matches = (!query || name.indexOf(query) !== -1)
&& (!wantSchool || item.getAttribute('data-school') === wantSchool)
&& (!wantGrade || item.getAttribute('data-grade') === wantGrade);
item.hidden = !matches;
if (matches) {
visible++;
}
});
const empty = dialog.querySelector('[data-selector-empty]');
if (empty) {
empty.hidden = visible !== 0;
}
hideEmptyGroups(dialog);
};
document.querySelectorAll('[data-dialog-open]').forEach(function (button) {
button.addEventListener('click', function () {
const dialog = document.querySelector(button.getAttribute('data-dialog-open'));
if (!dialog || typeof dialog.showModal !== 'function' || dialog.open) {
return;
}
dialog.showModal();
const search = dialog.querySelector('[data-selector-search]');
if (search) {
search.value = '';
}
dialog.querySelectorAll('select[data-selector-school], select[data-selector-grade]').forEach(function (select) {
select.selectedIndex = 0;
});
filterSelector(dialog);
if (search) {
search.focus();
}
});
});
// Nach dem Filtern (GET-Reload) den Schirm gleich wieder als Modal öffnen.
document.querySelectorAll('dialog.selector[data-autopen]').forEach(function (dialog) {
if (typeof dialog.showModal === 'function' && !dialog.open) {
dialog.showModal();
}
});
document.querySelectorAll('dialog.selector').forEach(function (dialog) {
dialog.addEventListener('click', function (event) {
// Klick auf den Hintergrund (nicht auf den Inhalt) schließt den Schirm
if (event.target === dialog) {
dialog.close();
}
});
dialog.querySelectorAll('[data-dialog-close]').forEach(function (button) {
button.addEventListener('click', function () {
dialog.close();
});
});
const search = dialog.querySelector('[data-selector-search]');
if (search) {
search.addEventListener('input', function () {
filterSelector(dialog);
});
}
dialog.querySelectorAll('[data-selector-school], [data-selector-grade]').forEach(function (select) {
select.addEventListener('change', function () {
filterSelector(dialog);
});
});
});
})();