174 lines
7.4 KiB
JavaScript
174 lines
7.4 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
function matches(element, term) {
|
|
return term === '' || (element.dataset.name || '').indexOf(term) !== -1;
|
|
}
|
|
|
|
// ---- Rangliste durchsuchen ----
|
|
var rankingSearch = document.getElementById('contribution-search');
|
|
var ranking = document.getElementById('contribution-ranking');
|
|
if (rankingSearch && ranking) {
|
|
var noMatch = document.getElementById('contribution-no-match');
|
|
var showAll = document.getElementById('contribution-show-all');
|
|
var expanded = false;
|
|
|
|
// Ohne Suche nur die ersten Plaetze (Karten mit data-extra bleiben eingeklappt, bis man
|
|
// "Alle anzeigen" drueckt); mit Suche werden alle Treffer gezeigt.
|
|
function applyRanking() {
|
|
var term = rankingSearch.value.trim().toLowerCase();
|
|
var visible = 0;
|
|
ranking.querySelectorAll('.contribution-card').forEach(function (card) {
|
|
var collapsed = card.hasAttribute('data-extra') && !expanded && term === '';
|
|
card.hidden = collapsed || !matches(card, term);
|
|
if (!card.hidden) visible++;
|
|
});
|
|
if (noMatch) noMatch.hidden = visible > 0;
|
|
if (showAll) {
|
|
showAll.hidden = term !== '';
|
|
showAll.textContent = expanded ? showAll.dataset.less : showAll.dataset.more;
|
|
}
|
|
}
|
|
|
|
rankingSearch.addEventListener('input', applyRanking);
|
|
if (showAll) {
|
|
showAll.addEventListener('click', function () {
|
|
expanded = !expanded;
|
|
applyRanking();
|
|
});
|
|
}
|
|
}
|
|
|
|
// ---- Formular "Punkte vergeben" ----
|
|
var form = document.getElementById('contribution-form');
|
|
if (!form) return;
|
|
|
|
var picks = Array.prototype.slice.call(form.querySelectorAll('.contribution-pick'));
|
|
var boxes = picks.map(function (pick) { return pick.querySelector('input[type="checkbox"]'); });
|
|
var counter = document.getElementById('picker-count');
|
|
var pickerSearch = document.getElementById('picker-search');
|
|
var submit = document.getElementById('contribution-submit');
|
|
var pointsInput = document.getElementById('points');
|
|
|
|
function updateCount() {
|
|
var count = boxes.filter(function (box) { return box.checked; }).length;
|
|
counter.textContent = count === 0 ? '(keins ausgewählt)' : '(' + count + ' ausgewählt)';
|
|
picks.forEach(function (pick, i) { pick.classList.toggle('checked', boxes[i].checked); });
|
|
|
|
var points = parseInt(pointsInput.value, 10);
|
|
var label = 'Punkte vergeben';
|
|
if (count > 0 && points) {
|
|
label = points > 0
|
|
? points + ' Punkte an ' + count + (count === 1 ? ' Mitglied' : ' Mitglieder') + ' vergeben'
|
|
: Math.abs(points) + ' Punkte bei ' + count + (count === 1 ? ' Mitglied' : ' Mitgliedern') + ' abziehen';
|
|
}
|
|
submit.textContent = label;
|
|
}
|
|
|
|
boxes.forEach(function (box) { box.addEventListener('change', updateCount); });
|
|
pointsInput.addEventListener('input', updateCount);
|
|
|
|
pickerSearch.addEventListener('input', function () {
|
|
var term = pickerSearch.value.trim().toLowerCase();
|
|
picks.forEach(function (pick) { pick.hidden = !matches(pick, term); });
|
|
});
|
|
|
|
// "Alle" / "Keine" wirken nur auf die gerade sichtbaren (gefilterten) Mitglieder.
|
|
function setVisible(checked) {
|
|
picks.forEach(function (pick, i) {
|
|
if (!pick.hidden) boxes[i].checked = checked;
|
|
});
|
|
updateCount();
|
|
}
|
|
document.getElementById('picker-all').addEventListener('click', function () { setVisible(true); });
|
|
document.getElementById('picker-none').addEventListener('click', function () { setVisible(false); });
|
|
|
|
// ---- Anlass und Raid ----
|
|
var category = document.getElementById('category');
|
|
var raidField = document.getElementById('raid-field');
|
|
var raidSelect = document.getElementById('raid_id');
|
|
var raidInfo = document.getElementById('raid-info');
|
|
var titleInput = document.getElementById('title');
|
|
var timeInput = document.getElementById('awarded_at');
|
|
var raidsEl = document.getElementById('contribution-raids');
|
|
var raids = raidsEl ? JSON.parse(raidsEl.textContent) : [];
|
|
var autoTitle = '';
|
|
|
|
function toggleRaidField() {
|
|
raidField.hidden = category.value !== 'raid';
|
|
if (raidField.hidden && raidInfo) raidInfo.hidden = true;
|
|
}
|
|
category.addEventListener('change', toggleRaidField);
|
|
toggleRaidField();
|
|
|
|
var raidMode = document.getElementById('raid-mode');
|
|
var currentRaid = null;
|
|
|
|
function selectUsers(ids) {
|
|
boxes.forEach(function (box) {
|
|
box.checked = ids.indexOf(parseInt(box.value, 10)) !== -1;
|
|
});
|
|
}
|
|
|
|
// mode: present = Anwesende mit Punkten, absent = Abwesende mit Abzug,
|
|
// participants = alle in einer Gruppe Angemeldeten (ohne Anwesenheitsliste).
|
|
function applyRaid(raid, mode) {
|
|
var ids = mode === 'present' ? raid.present : (mode === 'absent' ? raid.absent : raid.participants);
|
|
selectUsers(ids);
|
|
|
|
// Grund nur ueberschreiben, wenn nichts Eigenes eingetragen wurde.
|
|
if (titleInput.value === '' || titleInput.value === autoTitle) {
|
|
autoTitle = (mode === 'absent' ? 'Abwesend: ' : '') + raid.title + (raid.date ? ' (' + raid.date + ')' : '');
|
|
titleInput.value = autoTitle;
|
|
}
|
|
if (raid.time) timeInput.value = raid.time;
|
|
|
|
// Abwesenden werden Punkte abgezogen, allen anderen gutgeschrieben.
|
|
var points = parseInt(pointsInput.value, 10);
|
|
if (points && ((mode === 'absent' && points > 0) || (mode !== 'absent' && points < 0))) {
|
|
pointsInput.value = -points;
|
|
}
|
|
|
|
var info;
|
|
if (mode === 'present') {
|
|
info = ids.length + ' Anwesende ausgewählt (laut Anwesenheitsliste).';
|
|
} else if (mode === 'absent') {
|
|
info = ids.length + ' Abwesende ausgewählt - die Punkte werden abgezogen.';
|
|
} else {
|
|
info = ids.length + ' Teilnehmer ausgewählt (laut Anmeldung).';
|
|
if (raid.bench.length) info += ' ' + raid.bench.length + ' auf der Ersatzbank (nicht ausgewählt).';
|
|
}
|
|
if (!raid.attendance) {
|
|
info += ' Für diesen Raid wurde noch keine Anwesenheitsliste geführt.';
|
|
}
|
|
if (raid.withoutAccount.length) {
|
|
info += ' Ohne Website-Konto, daher keine Punkte möglich: ' + raid.withoutAccount.join(', ') + '.';
|
|
}
|
|
raidInfo.textContent = info;
|
|
raidInfo.hidden = false;
|
|
raidMode.querySelectorAll('[data-raid-mode]').forEach(function (button) {
|
|
button.classList.toggle('active', button.getAttribute('data-raid-mode') === mode);
|
|
});
|
|
updateCount();
|
|
}
|
|
|
|
if (raidSelect) {
|
|
raidSelect.addEventListener('change', function () {
|
|
currentRaid = raids.filter(function (r) { return r.id === raidSelect.value; })[0] || null;
|
|
raidMode.hidden = !(currentRaid && currentRaid.attendance);
|
|
if (!currentRaid) {
|
|
raidInfo.hidden = true;
|
|
return;
|
|
}
|
|
applyRaid(currentRaid, currentRaid.attendance ? 'present' : 'participants');
|
|
});
|
|
|
|
raidMode.querySelectorAll('[data-raid-mode]').forEach(function (button) {
|
|
button.addEventListener('click', function () {
|
|
if (currentRaid) applyRaid(currentRaid, button.getAttribute('data-raid-mode'));
|
|
});
|
|
});
|
|
}
|
|
|
|
updateCount();
|
|
})();
|