Add Q&A slide type
CI / Deploy / test (push) Successful in 48s
CI / Deploy / deploy (push) Skipped

Participants submit questions, upvote them and the presenter discusses
them one by one, most upvoted first.

- Stages collect -> vote -> discuss, switched manually by the presenter
  (V key or stepper, going back is possible)
- Configurable upvote budget per participant, freely distributable
  (several upvotes on one question are allowed), votes can be withdrawn
- Participants see all questions while collecting to avoid duplicates;
  no counts before the discussion
- While discussing, next/prev step through the questions before
  moving to the next slide
- Migration: slides.stage, slides.focus_response_id, responses.parent_id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 16:09:46 +01:00
1 parent ea944b080f
commit f0c6687100
21 files changed
+1327 -27

No files matched your search

+294 -9
View File
@@ -300,7 +300,8 @@
return;
}
// A quiz shows its answer options even before anyone has answered.
if (results.participants === 0 && !(results.type === 'quiz' && !results.revealed)) {
// A Q&A slide always shows its stages.
if (results.participants === 0 && results.type !== 'qa' && !(results.type === 'quiz' && !results.revealed)) {
container.appendChild(el('p', { class: 'results-empty', text: 'Noch keine Antworten' }));
return;
}
@@ -498,6 +499,111 @@
return el('ul', { class: 'answers' }, results.answers.map(function (answer) {
return el('li', { class: 'answer', text: answer.text });
}));
},
/**
* Presenter view of a Q&A slide: stage stepper, then per stage the
* collected questions, the live ranking or the question being discussed.
* options: {onStage: fn(stage)|null, onQuestion: fn(step)|null}
*/
qa: function (results, options) {
var onStage = options && options.onStage;
var onQuestion = options && options.onQuestion;
var questions = results.questions;
var maxVotes = Math.max.apply(null, questions.map(function (q) { return q.votes; }).concat([1]));
var children = [Qa.steps(results.stage, onStage)];
function votes(question) {
return el('span', {
class: 'qa-votes',
'aria-label': question.votes + (question.votes === 1 ? ' Stimme' : ' Stimmen'),
text: '▲ ' + question.votes
});
}
if (results.stage === 'collect') {
children.push(el('p', { class: 'qa-summary', text: questions.length === 0
? 'Noch keine Fragen – jetzt mitmachen und Fragen einreichen!'
: questions.length + (questions.length === 1 ? ' Frage eingereicht' : ' Fragen eingereicht') }));
children.push(el('ul', { class: 'answers' }, questions.slice().reverse().map(function (question) {
return el('li', { class: 'answer', text: question.text });
})));
} else if (results.stage === 'vote') {
children.push(el('p', { class: 'qa-summary', text: 'Abstimmen: jede Person hat '
+ results.maxUpvotes + (results.maxUpvotes === 1 ? ' Stimme' : ' Stimmen')
+ ' · bisher ' + results.upvotes + ' vergeben' }));
children.push(el('ol', { class: 'qa-list' }, questions.map(function (question) {
return el('li', { class: 'qa-item' }, [
el('span', { class: 'qa-item__text', text: question.text }),
votes(question),
el('span', { class: 'qa-item__bar', style: 'width:' + (question.votes / maxVotes * 100) + '%' })
]);
})));
} else if (questions.length === 0) {
children.push(el('p', { class: 'qa-summary', text: 'Es wurden keine Fragen eingereicht.' }));
} else {
var index = results.focusIndex === null ? 0 : results.focusIndex;
var focus = questions[index];
var nav = function (step, label, symbol, disabled) {
if (!onQuestion) {
return null;
}
var button = el('button', { type: 'button', class: 'ctrl', title: label }, [
el('span', { 'aria-hidden': 'true', text: symbol }),
el('span', { class: 'sr-only', text: label })
]);
button.disabled = disabled;
button.addEventListener('click', function () { onQuestion(step); });
return button;
};
children.push(el('div', { class: 'qa-focus', 'aria-live': 'polite' }, [
el('p', { class: 'qa-focus__meta' }, [
el('span', { text: 'Frage ' + (index + 1) + ' von ' + questions.length }),
votes(focus)
]),
el('p', { class: 'qa-focus__text', text: focus.text }),
el('div', { class: 'qa-focus__nav' }, [
nav(-1, 'Vorherige Frage', '←', index === 0),
nav(1, 'Nächste Frage', '→', index === questions.length - 1)
])
]));
children.push(el('ol', { class: 'qa-list qa-list--compact' }, questions.map(function (question, i) {
var attrs = { class: 'qa-item' + (i < index ? ' is-done' : '') + (i === index ? ' is-current' : '') };
if (i === index) {
attrs['aria-current'] = 'true';
}
return el('li', attrs, [
el('span', { class: 'qa-item__text', text: (i < index ? '✓ ' : '') + question.text }),
votes(question)
]);
})));
}
return el('div', { class: 'qa-results qa-results--' + results.stage }, children);
}
};
// ------------------------------------------------------------------ Q&A
var Qa = {
labels: { collect: 'Fragen sammeln', vote: 'Abstimmen', discuss: 'Besprechen' },
/** Stepper of the three stages; clickable for the presenter (onStage). */
steps: function (stage, onStage) {
return el('ol', { class: 'qa-steps', 'aria-label': 'Ablauf' }, ['collect', 'vote', 'discuss'].map(function (key, i) {
var current = key === stage;
var label = (i + 1) + ' ' + Qa.labels[key];
var item = onStage
? el('button', { type: 'button', class: 'qa-steps__step', text: label })
: el('span', { class: 'qa-steps__step', text: label });
if (current) {
item.setAttribute('aria-current', 'step');
}
if (onStage) {
item.disabled = current;
item.addEventListener('click', function () { onStage(key); });
}
return el('li', { class: current ? 'is-current' : '' }, [item]);
}));
}
};
@@ -704,29 +810,38 @@
}
var quizPhase = slide.type === 'quiz' && state.results ? state.results.phase : null;
var qaStage = slide.type === 'qa' && state.results ? state.results.stage : null;
q('[data-type]').textContent = quizPhase
? slide.typeLabel + ' · ' + { ready: 'bereit', running: 'läuft', finished: 'Auflösung' }[quizPhase]
: slide.typeLabel;
: (qaStage ? slide.typeLabel + ' · ' + Qa.labels[qaStage] : slide.typeLabel);
q('[data-question]').textContent = slide.question;
q('[data-description]').textContent = slide.description;
root.classList.toggle('stage--content', !slide.isInteractive);
root.classList.toggle('stage--video', slide.type === 'youtube');
renderMedia(slide);
root.classList.toggle('stage--closed', slide.isInteractive && !slide.isVotingOpen && !quizPhase);
root.classList.toggle('stage--closed', slide.isInteractive && !slide.isVotingOpen && !quizPhase && !qaStage);
root.classList.toggle('stage--qa', !!qaStage);
var voting = q('[data-voting]');
voting.hidden = !slide.isInteractive;
// Q&A: V switches to the next stage, there is none after "discuss".
voting.hidden = !slide.isInteractive || qaStage === 'discuss';
voting.querySelector('[data-label]').textContent = quizPhase
? { ready: 'Quiz starten', running: 'Quiz beenden', finished: 'Quiz erneut öffnen' }[quizPhase]
: (slide.isVotingOpen ? 'Abstimmung schließen' : 'Abstimmung öffnen');
: (qaStage
? { collect: 'Einreichen beenden', vote: 'Abstimmung beenden', discuss: '' }[qaStage]
: (slide.isVotingOpen ? 'Abstimmung schließen' : 'Abstimmung öffnen'));
q('[data-participants]').textContent = slide.isInteractive
? state.results.participants + ' ' + (state.results.participants === 1 ? 'Antwort' : 'Antworten')
: '';
var count = qaStage ? state.results.questions.length : (slide.isInteractive ? state.results.participants : 0);
q('[data-participants]').textContent = !slide.isInteractive ? '' : (qaStage
? count + (count === 1 ? ' Frage' : ' Fragen')
: count + ' ' + (count === 1 ? 'Antwort' : 'Antworten'));
var container = q('[data-results]');
container.classList.toggle('is-hidden', hideResults);
Results.render(container, slide.isInteractive ? state.results : null);
Results.render(container, slide.isInteractive ? state.results : null, {
onStage: function (stage) { control('stage', { stage: stage }); },
onQuestion: function (step) { control('question', { step: step }); }
});
Countdown.start();
}
@@ -833,8 +948,178 @@
&& state.isVotingOpen);
}
// --- Q&A: submit questions, upvote, follow the discussion ----------
var qaRoot = root.querySelector('[data-qa]');
var qaList = qaRoot ? qaRoot.querySelector('[data-qa-list]') : null;
var qaMessage = qaRoot ? qaRoot.querySelector('[data-qa-message]') : null;
var qaForm = qaRoot ? qaRoot.querySelector('[data-qa-form]') : null;
var qaRendered = '';
var qaSending = false;
function qaSay(text, isError) {
qaMessage.textContent = text;
qaMessage.classList.toggle('is-error', !!isError);
}
function sendQa(data) {
if (qaSending) {
return Promise.resolve(null);
}
qaSending = true;
data.slide_id = root.querySelector('[name=slide_id]').value;
return postJson(voteUrl, data).then(function (response) {
qaSay(response.message, !response.ok);
if (response.state.version !== version) {
window.location.reload();
return null;
}
renderQa(response.state);
return response;
}).catch(function () {
qaSay('Das hat nicht geklappt. Bitte versuche es noch einmal.', true);
return null;
}).then(function (response) {
qaSending = false;
return response;
});
}
function renderQa(state) {
var qa = state.qa;
if (!qaList || !qa) {
return;
}
var key = JSON.stringify(qa);
if (key === qaRendered) {
return;
}
qaRendered = key;
// Keep keyboard focus on the same question after re-rendering.
var focused = document.activeElement && document.activeElement.getAttribute('data-question-id');
qaList.textContent = '';
if (qa.stage === 'collect') {
// All questions so far, so participants can spot duplicates before asking.
qaList.appendChild(el('h2', { class: 'qa-heading', text: 'Bereits gestellte Fragen (' + qa.questions.length + ')' }));
if (!qa.questions.length) {
qaList.appendChild(el('p', { class: 'results-empty', text: 'Noch keine – stell gern die erste Frage!' }));
return;
}
qaList.appendChild(el('ul', { class: 'qa-list' }, qa.questions.map(function (question) {
var mine = qa.mine.indexOf(question.id) >= 0;
return el('li', { class: 'qa-item' + (mine ? ' is-mine' : '') }, [
el('span', { class: 'qa-item__text', text: question.text }),
mine ? el('span', { class: 'qa-own', text: 'deine Frage' }) : null
]);
})));
return;
}
if (qa.stage === 'vote') {
var used = qa.maxUpvotes - qa.votesLeft;
qaList.appendChild(el('p', { class: 'qa-budget', 'aria-live': 'polite' }, [
el('strong', { text: String(qa.votesLeft) }),
el('span', { text: ' von ' + qa.maxUpvotes + (qa.maxUpvotes === 1 ? ' Stimme' : ' Stimmen') + ' übrig' })
].concat(Array.apply(null, Array(qa.maxUpvotes)).map(function (_, i) {
return el('span', { class: 'qa-budget__dot' + (i < used ? ' is-used' : ''), 'aria-hidden': 'true' });
}))));
if (!qa.questions.length) {
qaList.appendChild(el('p', { class: 'results-empty', text: 'Es wurden keine Fragen eingereicht.' }));
return;
}
qaList.appendChild(el('ul', { class: 'qa-list' }, qa.questions.map(function (question) {
var mine = qa.mine.indexOf(question.id) >= 0;
// Own upvotes on this question – several are allowed to push important questions.
var given = (qa.upvoted || {})[question.id] || 0;
var action;
if (mine) {
action = el('span', { class: 'qa-own', text: 'deine Frage' });
} else {
var button = function (vote, symbol, label, disabled) {
var b = el('button', {
type: 'button',
class: 'qa-upvote qa-upvote--' + vote,
'aria-label': label + ': ' + question.text,
'data-question-id': question.id + '-' + vote
}, [el('span', { 'aria-hidden': 'true', text: symbol })]);
b.disabled = disabled;
b.addEventListener('click', function () {
sendQa({ vote: vote, question_id: question.id });
});
return b;
};
action = el('span', { class: 'qa-stepper' + (given > 0 ? ' is-active' : '') }, [
button('remove', '−', 'Stimme zurücknehmen', given === 0),
el('span', {
class: 'qa-stepper__count',
'aria-label': given === 0 ? 'keine Stimme von dir' : given + (given === 1 ? ' Stimme' : ' Stimmen') + ' von dir',
text: '▲ ' + given
}),
button('up', '+', 'Stimme geben', qa.votesLeft === 0)
]);
}
return el('li', { class: 'qa-item' + (mine ? ' is-mine' : '') + (given > 0 ? ' is-upvoted' : '') }, [
el('span', { class: 'qa-item__text', text: question.text }),
action
]);
})));
} else {
if (!qa.questions.length) {
qaList.appendChild(el('p', { class: 'results-empty', text: 'Es wurden keine Fragen eingereicht.' }));
return;
}
var index = -1;
qa.questions.forEach(function (question, i) {
if (question.id === qa.focusId) {
index = i;
}
});
if (index >= 0) {
qaList.appendChild(el('div', { class: 'qa-focus', 'aria-live': 'polite' }, [
el('p', { class: 'qa-focus__meta', text: 'Wird gerade besprochen · Frage ' + (index + 1) + ' von ' + qa.questions.length }),
el('p', { class: 'qa-focus__text', text: qa.questions[index].text })
]));
}
qaList.appendChild(el('ol', { class: 'qa-list qa-list--compact' }, qa.questions.map(function (question, i) {
var mine = qa.mine.indexOf(question.id) >= 0;
var attrs = { class: 'qa-item' + (i < index ? ' is-done' : '') + (i === index ? ' is-current' : '') + (mine ? ' is-mine' : '') };
if (i === index) {
attrs['aria-current'] = 'true';
}
return el('li', attrs, [
el('span', { class: 'qa-item__text', text: (i < index ? '✓ ' : '') + question.text + (mine ? ' (deine Frage)' : '') }),
el('span', { class: 'qa-votes', 'aria-label': question.votes + (question.votes === 1 ? ' Stimme' : ' Stimmen'), text: '▲ ' + question.votes })
]);
})));
}
if (focused) {
var again = qaList.querySelector('[data-question-id="' + focused + '"]');
if (again && !again.disabled) {
again.focus();
}
}
}
if (qaForm) {
qaForm.addEventListener('submit', function (event) {
event.preventDefault();
var field = qaForm.querySelector('[name=question]');
var text = field.value.trim();
if (!text) {
return;
}
sendQa({ question: text }).then(function (response) {
if (response && response.ok) {
field.value = '';
}
});
});
}
function renderResults(state) {
current = state;
renderQa(state);
if (!card || !container) {
return;
}