Files
laraundfelix/resources/js/inquiry-widget.js
T
LarsandClaude Sonnet 5.5 fb479c1b01 Liefere das Anfrageformular je Anfragegrund aus und gib dem Widget Gründe, Formularführung und Präsentation
Das Widget Anfrage wählt statt eines Anfrage-Einstiegs mehrere Anfragegründe, jeder zeigt auf ein veröffentlichtes Formular
(optional mit vorgewähltem Anliegen). Der Besucher erhält genau das Formular seines Grundes: /anfrage/formular/{vorlage}
mit Grund in der Adresse, dasselbe Token, dieselbe Auswertung und Annahme. Ein Dialog (dialog-Element) und die Grundauswahl
als Karten oder Auswahlfeld kommen aus resources/js/inquiry-widget.js. Seiten mit einem Einstieg bleiben darstellbar.
Bereiche lassen sich wie Gruppen anzeigen oder ausblenden.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 22:29:31 +01:00

303 lines
9.9 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.
/*
* Widget „Anfrage“ (ADR-61): Auswahl des Anfragegrundes, genau ein Formular je Grund, inline oder als Dialog.
* Das Formular selbst baut resources/js/inquiry-form.js aus der Beschreibung, die der Server für den Grund liefert.
* Je Grund gibt es höchstens eine Formularinstanz; ein Wechsel blendet sie nur aus, Eingaben bleiben erhalten. Es ist nie
* mehr als ein Formular sichtbar.
*/
import { initInquiryForms } from './inquiry-form.js';
let counter = 0;
function h(tag, attrs = {}, ...children) {
const el = document.createElement(tag);
for (const [key, value] of Object.entries(attrs)) {
if (value === false || value === null || value === undefined) {
continue;
}
if (key === 'class') {
el.className = value;
} else if (key === 'text') {
el.textContent = value;
} else if (key.startsWith('on')) {
el.addEventListener(key.slice(2), value);
} else if (value === true) {
el.setAttribute(key, '');
} else {
el.setAttribute(key, String(value));
}
}
for (const child of children.flat()) {
if (child === null || child === undefined || child === false) {
continue;
}
el.append(child.nodeType ? child : document.createTextNode(String(child)));
}
return el;
}
class InquiryWidget {
constructor(root) {
this.root = root;
this.id = 'iw' + ++counter;
this.config = JSON.parse(root.dataset.config);
this.reasons = this.config.reasons;
this.forms = new Map();
this.active = null;
this.sent = false;
this.modal = this.config.open === 'modal';
}
start() {
const wanted = new URLSearchParams(window.location.search).get('grund');
const preselected = this.reasons.find((reason) => reason.key === wanted) || null;
this.build();
if (this.reasons.length === 1) {
this.choose(this.reasons[0].key, false);
} else if (preselected) {
this.choose(preselected.key, false);
}
if (this.modal) {
// Ein Button der Seite mit ?grund=… will den Dialog sofort sehen; ohne Vorwahl öffnet der Besucher selbst.
if (preselected) {
this.open();
}
} else if (preselected && window.location.hash === '') {
this.root.scrollIntoView({ block: 'start' });
}
}
/* ------------------------------------------------------------------ Aufbau */
build() {
this.question = this.config.question;
this.picker = this.reasons.length > 1 ? this.buildPicker() : null;
this.chosenBar = this.reasons.length > 1 ? this.buildChosenBar() : null;
this.formHost = h('div', { class: 'iw-forms' });
this.body = h('div', { class: 'iw-body' }, ...[this.picker, this.chosenBar, this.formHost].filter(Boolean));
this.root.replaceChildren();
if (!this.modal) {
this.root.append(this.body);
this.root.classList.add('is-inline');
return;
}
this.root.classList.add('is-modal');
this.opener = h('button', {
type: 'button',
class: 'button iw-opener',
'aria-haspopup': 'dialog',
text: this.config.openLabel,
onclick: () => this.open(),
});
this.close = h('button', { type: 'button', class: 'iw-close', 'aria-label': 'Dialog schließen', onclick: () => this.dialog.close() }, '×');
this.dialog = h('dialog', { class: 'iw-dialog', 'aria-label': this.config.openLabel },
h('div', { class: 'iw-dialog-head' }, this.close),
this.body,
);
this.dialog.addEventListener('close', () => this.onClose());
this.dialog.addEventListener('click', (event) => {
// Klick auf den abgedunkelten Hintergrund (das Dialogelement selbst) schließt; Eingaben bleiben erhalten.
if (event.target === this.dialog) {
this.dialog.close();
}
});
this.root.append(this.opener, this.dialog);
}
buildPicker() {
const name = this.id + '-grund';
const legendId = this.id + '-question';
if (this.config.picker === 'select') {
const select = h('select', {
id: this.id + '-select',
class: 'iw-select',
onchange: () => {
if (select.value !== '') {
this.choose(select.value, true);
}
},
}, h('option', { value: '', text: 'Bitte wählen …' }), ...this.reasons.map((reason) => h('option', { value: reason.key, text: reason.label })));
this.select = select;
return h('div', { class: 'iw-picker iw-picker--select' },
h('label', { class: 'iw-question', for: select.id, text: this.question }),
select,
);
}
const cards = this.reasons.map((reason) => {
const input = h('input', {
type: 'radio',
name,
id: `${this.id}-r-${reason.key}`,
value: reason.key,
onchange: () => this.choose(reason.key, true),
});
return h('div', { class: 'iw-card' }, input,
h('label', { for: input.id },
h('span', { class: 'iw-card-label', text: reason.label }),
reason.description ? h('span', { class: 'iw-card-desc', text: reason.description }) : null,
),
);
});
return h('fieldset', { class: 'iw-picker iw-picker--cards', 'aria-labelledby': legendId },
h('legend', { class: 'iw-question', id: legendId, text: this.question }),
h('div', { class: 'iw-cards' }, ...cards),
);
}
buildChosenBar() {
this.chosenLabel = h('strong', { class: 'iw-chosen-label' });
this.changeButton = h('button', { type: 'button', class: 'iw-change', onclick: () => this.change() }, 'Anderen Grund wählen');
return h('p', { class: 'iw-chosen', hidden: true }, 'Anfragegrund: ', this.chosenLabel, ' ', this.changeButton);
}
/* ------------------------------------------------------------------ Auswahl */
choose(key, focus) {
const reason = this.reasons.find((candidate) => candidate.key === key);
if (!reason) {
return;
}
this.active = key;
if (!this.forms.has(key)) {
this.forms.set(key, this.mount(reason));
}
for (const [formKey, el] of this.forms) {
el.hidden = formKey !== key;
}
if (this.picker) {
this.picker.hidden = true;
this.chosenBar.hidden = false;
this.chosenLabel.textContent = reason.label;
if (this.select) {
this.select.value = key;
}
const radio = this.root.querySelector(`input[type='radio'][value='${key}']`);
if (radio) {
radio.checked = true;
}
}
if (focus) {
const heading = this.forms.get(key).querySelector('.inquiry-heading');
(heading || this.forms.get(key)).focus?.({ preventScroll: false });
}
}
change() {
if (this.sent) {
return;
}
this.picker.hidden = false;
this.chosenBar.hidden = true;
for (const el of this.forms.values()) {
el.hidden = true;
}
this.active = null;
const target = this.select || this.picker.querySelector('input:checked') || this.picker.querySelector('input');
target?.focus();
}
mount(reason) {
const el = h('div', {
class: 'inquiry',
'data-inquiry': '',
'data-embedded': '1',
'data-definition-url': reason.definitionUrl,
'data-state-url': reason.stateUrl,
'data-submit-url': reason.submitUrl,
'data-preview': this.config.preview ? '1' : null,
tabindex: '-1',
}, h('p', { class: 'inquiry-loading', text: `Das Formular „${reason.label}“ wird geladen …` }));
el.addEventListener('inquiry:sent', () => this.onSent());
this.formHost.append(el);
initInquiryForms(this.formHost);
return el;
}
onSent() {
this.sent = true;
if (this.picker) {
this.picker.hidden = true;
this.chosenBar.hidden = true;
}
}
/* ------------------------------------------------------------------ Dialog */
open() {
if (this.dialog.open) {
return;
}
this.lastFocus = document.activeElement;
this.dialog.showModal();
document.documentElement.classList.add('iw-locked');
this.opener.setAttribute('aria-expanded', 'true');
const target = this.active ? this.forms.get(this.active).querySelector('.inquiry-heading') : this.picker?.querySelector('input, select');
(target || this.close).focus?.();
}
onClose() {
document.documentElement.classList.remove('iw-locked');
this.opener.setAttribute('aria-expanded', 'false');
(this.lastFocus && document.contains(this.lastFocus) ? this.lastFocus : this.opener).focus?.();
}
}
export function initInquiryWidgets(root = document) {
root.querySelectorAll('[data-inquiry-widget]').forEach((el) => {
if (el.dataset.inquiryReady === '1') {
return;
}
el.dataset.inquiryReady = '1';
try {
new InquiryWidget(el).start();
} catch {
el.replaceChildren(h('p', { class: 'inquiry-note', role: 'alert', text: 'Das Anfrageformular konnte nicht geladen werden. Bitte laden Sie die Seite neu.' }));
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => initInquiryWidgets());
} else {
initInquiryWidgets();
}