Add drag and drop sorting for slides
CI / Deploy / test (push) Successful in 43s
CI / Deploy / deploy (push) Skipped

Slides can be reordered on the dashboard by dragging the handle (mouse,
touch and pen via Pointer Events) or with the arrow keys on the focused
handle. The new order is posted as ids[] to slides/reorder, which only
applies a complete, valid order of the poll's own slides. The up/down
buttons stay as no-JS fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 14:44:10 +01:00
1 parent 50010484be
commit 9983d6c01b
8 files changed
+301 -8

No files matched your search

+173 -1
View File
@@ -7,6 +7,7 @@
* - [data-participant] participant page, polls /p/{code}/state
* - [data-countdown] quiz timer (data-remaining = seconds), fires `plenario:timeup` on document
* - [data-quiz-options] slide form: "add answer" button for quiz slides
* - [data-sortable] admin slide list: drag & drop / arrow keys via [data-drag-handle], POSTs ids[] to the URL
*
* Results JSON shape: see src/Service/ResultsService.php
*/
@@ -55,7 +56,11 @@
function postJson(url, data) {
var body = new FormData();
Object.keys(data).forEach(function (key) {
body.append(key, data[key]);
if (Array.isArray(data[key])) {
data[key].forEach(function (value) { body.append(key + '[]', value); });
} else {
body.append(key, data[key]);
}
});
return fetch(url, {
method: 'POST',
@@ -927,6 +932,172 @@
update();
}
// ------------------------------------------------------------------ sortable slide list
function initSortable(list) {
var url = list.getAttribute('data-sortable');
var section = list.parentNode;
var status = section.querySelector('[data-sortable-status]');
var hint = section.querySelector('[data-sortable-hint]');
var error = null;
var saving = Promise.resolve();
var drag = null;
function items() {
return Array.prototype.slice.call(list.children).filter(function (node) {
return node.hasAttribute('data-slide-id');
});
}
function order() {
return items().map(function (item) { return item.getAttribute('data-slide-id'); });
}
function renumber() {
items().forEach(function (item, index) {
var pos = item.querySelector('[data-position]');
if (pos) {
pos.textContent = String(index + 1);
}
});
}
function label(item) {
var title = item.querySelector('.slide-item__title');
return title ? title.textContent : '';
}
function announce(text) {
if (status) {
status.textContent = text;
}
}
function announceMove(item) {
var all = items();
announce('„' + label(item) + '“ ist jetzt an Position ' + (all.indexOf(item) + 1) + ' von ' + all.length + '.');
}
function showError(message) {
if (!error) {
var reload = el('button', { type: 'button', class: 'button button--small', text: 'Seite neu laden' });
reload.addEventListener('click', function () { window.location.reload(); });
error = el('div', { class: 'notice', role: 'alert' }, [el('span'), document.createTextNode(' '), reload]);
section.insertBefore(error, list);
}
error.firstChild.textContent = message;
}
function save(item) {
var ids = order();
renumber();
announceMove(item);
item.classList.add('slide-item--saving');
// Sequential, so an older order can never overwrite a newer one.
saving = saving.then(function () {
return postJson(url, { ids: ids });
}).catch(function () {
showError('Die Reihenfolge konnte nicht gespeichert werden. Bitte lade die Seite neu.');
}).then(function () {
item.classList.remove('slide-item--saving');
});
}
// Keyboard: arrow keys on the handle move the slide by one.
list.addEventListener('keydown', function (event) {
var handle = event.target.closest('[data-drag-handle]');
if (!handle || (event.key !== 'ArrowUp' && event.key !== 'ArrowDown')) {
return;
}
event.preventDefault();
var item = handle.closest('[data-slide-id]');
var up = event.key === 'ArrowUp';
var sibling = up ? item.previousElementSibling : item.nextElementSibling;
if (!sibling || !sibling.hasAttribute('data-slide-id')) {
return;
}
list.insertBefore(item, up ? sibling : sibling.nextElementSibling);
handle.focus();
save(item);
});
// Pointer (mouse, touch, pen): drag by the handle.
function pageTop(node) {
return node.getBoundingClientRect().top + window.scrollY;
}
function onMove(event) {
if (!drag || event.pointerId !== drag.pointerId) {
return;
}
var item = drag.item;
var all = items();
var index = all.indexOf(item);
var prev = all[index - 1];
var next = all[index + 1];
var top = event.clientY + window.scrollY - drag.grabOffset;
var bottom = top + item.offsetHeight;
if (prev && top < pageTop(prev) + prev.offsetHeight / 2) {
list.insertBefore(item, prev);
} else if (next && bottom > pageTop(next) + next.offsetHeight / 2) {
list.insertBefore(item, next.nextElementSibling);
}
item.style.transform = '';
item.style.transform = 'translateY(' + (top - pageTop(item)) + 'px)';
// Scroll the page when dragging close to the viewport edge.
if (event.clientY < 60) {
window.scrollBy(0, -12);
} else if (event.clientY > window.innerHeight - 60) {
window.scrollBy(0, 12);
}
}
function onEnd(event) {
if (!drag || event.pointerId !== drag.pointerId) {
return;
}
var item = drag.item;
var changed = order().join(',') !== drag.before;
item.style.transform = '';
item.classList.remove('slide-item--dragging');
list.classList.remove('slide-list--sorting');
drag = null;
if (changed) {
save(item);
}
}
list.addEventListener('pointerdown', function (event) {
var handle = event.target.closest('[data-drag-handle]');
if (!handle || event.button !== 0 || drag) {
return;
}
event.preventDefault();
var item = handle.closest('[data-slide-id]');
handle.setPointerCapture(event.pointerId);
drag = {
item: item,
pointerId: event.pointerId,
grabOffset: event.clientY - item.getBoundingClientRect().top,
before: order().join(',')
};
item.classList.add('slide-item--dragging');
list.classList.add('slide-list--sorting');
});
list.addEventListener('pointermove', onMove);
list.addEventListener('pointerup', onEnd);
list.addEventListener('pointercancel', onEnd);
if (items().length > 1) {
list.querySelectorAll('[data-drag-handle]').forEach(function (handle) { handle.hidden = false; });
if (hint) {
hint.hidden = false;
}
}
}
// ------------------------------------------------------------------ boot
document.addEventListener('DOMContentLoaded', function () {
@@ -934,6 +1105,7 @@
document.querySelectorAll('[data-presenter]').forEach(initPresenter);
document.querySelectorAll('[data-participant]').forEach(initParticipant);
document.querySelectorAll('[data-quiz-options]').forEach(initQuizOptions);
document.querySelectorAll('[data-sortable]').forEach(initSortable);
// Prevent double submits of vote forms.
document.querySelectorAll('.vote-form').forEach(function (form) {