rpg-westreiche/westreiche_website/assets/app.js

249 lines
8.8 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: Einzelwürfe live zu TP max zusammenrechnen und auswürfeln
const hpInput = document.querySelector('[data-hp-rolls]');
if (hpInput) {
const totalField = document.querySelector('[data-hp-total]');
const label = document.querySelector('[data-hp-label]');
const die = parseInt(hpInput.getAttribute('data-hp-die'), 10) || 4;
const baseDie = parseInt(hpInput.getAttribute('data-hp-base'), 10) || 4;
const slots = parseInt(hpInput.getAttribute('data-hp-slots'), 10) || 1;
const con = parseInt(hpInput.getAttribute('data-hp-con'), 10) || 0;
const signedText = function (value) {
return value > 0 ? '+' + value : String(value);
};
const parseRolls = function () {
return (hpInput.value.match(/[0-9]+/g) || []).map(function (token) {
return parseInt(token, 10) || 0;
}).slice(0, slots);
};
const update = function () {
const rolls = parseRolls();
let sum = 0;
const levelDice = [];
for (let slot = 0; slot < slots; slot++) {
const value = rolls[slot] || 0;
if (value > 0) {
sum += value;
}
if (slot > 0) {
levelDice.push(value > 0 ? String(value) : '–');
}
}
const total = Math.max(1, sum + con);
if (totalField) {
totalField.value = String(total);
}
if (label) {
const base = rolls[0] > 0 ? String(rolls[0]) : '–';
label.textContent = '1W' + baseDie + ' ' + base + ' · W' + die + ' ' + (levelDice.join(' ') || '–')
+ ' · KON ' + signedText(con) + ' = ' + total;
}
};
hpInput.addEventListener('input', update);
const rollButton = document.querySelector('[data-hp-roll]');
if (rollButton) {
rollButton.addEventListener('click', function () {
const rolls = parseRolls();
const next = [];
for (let slot = 0; slot < slots; slot++) {
const value = rolls[slot] || 0;
next.push(value > 0 ? value : 1 + Math.floor(Math.random() * (slot === 0 ? baseDie : die)));
}
hpInput.value = next.join(' ');
update();
});
}
update();
}
// 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();
}
});
});
}
// 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);
});
});
});
})();