Let participants edit or delete their Q&A questions while collecting
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51d1be8695
commit
3a8962976e
6 files changed
+272
-15
No files matched your search
+101
-1
@@ -1385,6 +1385,9 @@
|
||||
var qaForm = qaRoot ? qaRoot.querySelector('[data-qa-form]') : null;
|
||||
var qaRendered = '';
|
||||
var qaSending = false;
|
||||
// Own question being edited: polling must not re-render the list (and wipe the draft) meanwhile.
|
||||
var qaEditing = null;
|
||||
var qaLatest = null;
|
||||
|
||||
function qaSay(text, isError) {
|
||||
qaMessage.textContent = text;
|
||||
@@ -1403,6 +1406,9 @@
|
||||
window.location.reload();
|
||||
return null;
|
||||
}
|
||||
if (response.ok && data.vote === 'edit') {
|
||||
qaEditing = null;
|
||||
}
|
||||
renderQa(response.state);
|
||||
return response;
|
||||
}).catch(function () {
|
||||
@@ -1419,6 +1425,10 @@
|
||||
if (!qaList || !qa) {
|
||||
return;
|
||||
}
|
||||
qaLatest = state;
|
||||
if (qaEditing !== null) {
|
||||
return; // rendered once the edit is saved or cancelled
|
||||
}
|
||||
var key = JSON.stringify(qa);
|
||||
if (key === qaRendered) {
|
||||
return;
|
||||
@@ -1437,11 +1447,16 @@
|
||||
}
|
||||
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' : '') }, [
|
||||
var item = 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
|
||||
]);
|
||||
if (mine) {
|
||||
item.appendChild(qaOwnActions(item, question));
|
||||
}
|
||||
return item;
|
||||
})));
|
||||
restoreQaFocus(focused);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1528,6 +1543,10 @@
|
||||
})));
|
||||
}
|
||||
|
||||
restoreQaFocus(focused);
|
||||
}
|
||||
|
||||
function restoreQaFocus(focused) {
|
||||
if (focused) {
|
||||
var again = qaList.querySelector('[data-question-id="' + focused + '"]');
|
||||
if (again && !again.disabled) {
|
||||
@@ -1536,6 +1555,87 @@
|
||||
}
|
||||
}
|
||||
|
||||
/** Ends editing and shows the latest state, focus back on the question's edit button. */
|
||||
function qaStopEditing(id) {
|
||||
qaEditing = null;
|
||||
qaRendered = '';
|
||||
renderQa(qaLatest);
|
||||
restoreQaFocus(id + '-edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect stage: edit and delete buttons for an own question. Editing swaps the item
|
||||
* content for a small form in place.
|
||||
*/
|
||||
function qaOwnActions(item, question) {
|
||||
var button = function (action, text, extraClass) {
|
||||
return el('button', {
|
||||
type: 'button',
|
||||
class: 'button button--small button--ghost' + (extraClass || ''),
|
||||
'aria-label': text + ': ' + question.text,
|
||||
'data-question-id': question.id + '-' + action,
|
||||
text: text
|
||||
});
|
||||
};
|
||||
var edit = button('edit', 'Bearbeiten');
|
||||
var remove = button('delete', 'Löschen', ' button--danger');
|
||||
|
||||
edit.addEventListener('click', function () {
|
||||
qaEditing = question.id;
|
||||
qaSay('', false);
|
||||
var field = el('textarea', { name: 'question', rows: '3', maxlength: '500', required: '', 'aria-label': 'Deine Frage bearbeiten' });
|
||||
field.value = question.text;
|
||||
var form = el('form', { class: 'qa-edit' }, [
|
||||
field,
|
||||
el('div', { class: 'qa-edit__actions' }, [
|
||||
el('button', { type: 'submit', class: 'button button--small button--primary', text: 'Speichern' }),
|
||||
el('button', { type: 'button', class: 'button button--small', 'data-qa-cancel': '', text: 'Abbrechen' })
|
||||
])
|
||||
]);
|
||||
form.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
var text = field.value.trim();
|
||||
if (!text) {
|
||||
qaSay('Bitte gib eine Frage ein.', true);
|
||||
field.focus();
|
||||
return;
|
||||
}
|
||||
if (text === question.text) {
|
||||
qaStopEditing(question.id);
|
||||
return;
|
||||
}
|
||||
sendQa({ vote: 'edit', question_id: question.id, question: text }).then(function (response) {
|
||||
if (response && response.ok) {
|
||||
restoreQaFocus(question.id + '-edit');
|
||||
} else {
|
||||
field.focus();
|
||||
}
|
||||
});
|
||||
});
|
||||
form.querySelector('[data-qa-cancel]').addEventListener('click', function () {
|
||||
qaStopEditing(question.id);
|
||||
});
|
||||
field.addEventListener('keydown', function (event) {
|
||||
if (event.key === 'Escape') {
|
||||
qaStopEditing(question.id);
|
||||
}
|
||||
});
|
||||
item.textContent = '';
|
||||
item.classList.add('is-editing');
|
||||
item.appendChild(form);
|
||||
field.focus();
|
||||
field.setSelectionRange(field.value.length, field.value.length);
|
||||
});
|
||||
|
||||
remove.addEventListener('click', function () {
|
||||
if (window.confirm('Deine Frage wirklich löschen?\n\n„' + question.text + '“')) {
|
||||
sendQa({ vote: 'delete', question_id: question.id });
|
||||
}
|
||||
});
|
||||
|
||||
return el('span', { class: 'qa-own-actions' }, [edit, remove]);
|
||||
}
|
||||
|
||||
if (qaForm) {
|
||||
qaForm.addEventListener('submit', function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
Reference in new issue
Block a user