rpg-westreiche/westreiche_website/assets/app.js

185 lines
6.5 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();
});
// 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();
}
});
});
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);
});
});
});
})();