Ersetze die Builder-Liste des Formular-Editors durch eine Formularansicht mit Ziehen und Ablegen
CI Feature / test (push) Successful in 4m18s
CI Feature / test (push) Successful in 4m18s
Der Aufbau eines Formulars zeigt Bereiche als Karten, Teilbereiche als eingerahmte Einheit darin und Felder als Zeilen (Frage, Art der Antwort, Pflicht). Hilfetext, Auswahlwerte, Bedingung und weitere Einstellungen öffnen als Pop-up. Bereiche, Teilbereiche und Felder lassen sich ziehen (SortableJS aus Filament) oder per Pfeiltasten und über "Verschieben nach" ordnen; Rückgängig nimmt Änderungen bis zum Speichern zurück. Der Browser führt den Baum und übergibt ihn beim Speichern als Text; FormEditor prüft ihn (validateTree) und überführt ihn über fromTree in dieselbe flache Ansicht wie bisher. Entwurf, Katalog und Engine bleiben unverändert, keine Migration. Tests: Die Livewire-Tests des Editors setzen jetzt den Baumtext (editor_json) statt der Builder-Blöcke; neu sind Baumumwandlung ohne Verlust, Teilbereich mit Feldern, Prüfung und Konfiguration. Das ist bewusst: Der Builder entfällt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
79c4e877ab
commit
380a3013c7
10 files changed
+1860
-225
No files matched your search
@@ -0,0 +1,828 @@
|
||||
@verbatim
|
||||
<script>
|
||||
/*
|
||||
* Formularansicht des Aufbaus (ADR-62). Der Baum besteht aus Bereichen (areas[]), darin Felder, Teilbereiche (mit eigenen
|
||||
* Feldern) und höchstens einer Frage nach dem Anliegen. Der Browser führt den Baum; beim Speichern liegt er als Text im
|
||||
* Formularzustand (state), den FormEditor prüft und in Katalog und Entwurf überführt. Ziehen und Ablegen: SortableJS, das
|
||||
* Filament mitbringt (window.Sortable). Der Baum ist die einzige Wahrheit: Nach jedem Ablegen wird die Verschiebung im
|
||||
* Baum nachvollzogen und die vom Ziehen bewegte DOM-Position zurückgesetzt, damit Alpine die Liste neu zeichnet.
|
||||
*/
|
||||
window.lufFormEditor = window.lufFormEditor || function (cfg) {
|
||||
const letters = 'abcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const random = (n) => Array.from({ length: n }, () => letters[Math.floor(Math.random() * letters.length)]).join('');
|
||||
const uid = () => 'n' + random(9);
|
||||
const kindOfEffect = { area: 'step', section: 'group', field: 'field' };
|
||||
|
||||
const blankCond = () => ({ effect: '', match: 'all', conditions: [], pos: null });
|
||||
const prepCond = (cond) => {
|
||||
if (!cond || Array.isArray(cond)) {
|
||||
return blankCond();
|
||||
}
|
||||
|
||||
return {
|
||||
effect: cond.effect || '',
|
||||
match: cond.match === 'any' ? 'any' : 'all',
|
||||
conditions: (cond.conditions || []).map((c) => ({ _id: uid(), source: c.source || '', op: c.op || 'eq', values: [...(c.values || [])] })),
|
||||
pos: cond.pos ?? null,
|
||||
};
|
||||
};
|
||||
const prepField = (f) => ({
|
||||
_id: uid(),
|
||||
kind: 'field',
|
||||
key: f.key || '',
|
||||
name: f.name || '',
|
||||
label: f.label || '',
|
||||
help: f.help || '',
|
||||
type: f.type || 'text',
|
||||
options: (f.options || []).map((o) => ({ _id: uid(), key: o.key || '', label: o.label || '' })),
|
||||
required: !!f.required,
|
||||
purpose: f.purpose || '',
|
||||
used_in: f.used_in || '',
|
||||
cond: prepCond(f.cond),
|
||||
});
|
||||
const prepItem = (item) => {
|
||||
if (item.kind === 'issue') {
|
||||
return { _id: uid(), kind: 'issue', issues: [...(item.issues || [])] };
|
||||
}
|
||||
|
||||
if (item.kind === 'section') {
|
||||
return {
|
||||
_id: uid(),
|
||||
kind: 'section',
|
||||
key: item.key || '',
|
||||
label: item.label || '',
|
||||
description: item.description || '',
|
||||
missing: !!item.missing,
|
||||
used_in: item.used_in || '',
|
||||
cond: prepCond(item.cond),
|
||||
items: (item.items || []).map(prepField),
|
||||
};
|
||||
}
|
||||
|
||||
return prepField(item);
|
||||
};
|
||||
const prepArea = (a) => ({
|
||||
_id: uid(),
|
||||
kind: 'area',
|
||||
key: a.key || 's' + random(6),
|
||||
title: a.title || '',
|
||||
intro: a.intro || '',
|
||||
cond: prepCond(a.cond),
|
||||
items: (a.items || []).map(prepItem),
|
||||
});
|
||||
|
||||
return {
|
||||
cfg,
|
||||
tree: { areas: [] },
|
||||
history: [],
|
||||
last: '',
|
||||
modal: null,
|
||||
modalNode: null,
|
||||
catalogSearch: '',
|
||||
announcement: '',
|
||||
opener: null,
|
||||
refreshTimer: null,
|
||||
sortables: [],
|
||||
|
||||
init() {
|
||||
this.load(this.state);
|
||||
this.$watch('state', (value) => {
|
||||
// Der Server hat einen neuen Stand geliefert (nach dem Speichern: feste Kennungen statt tmp_…).
|
||||
if (value !== this.last) {
|
||||
this.load(value);
|
||||
this.history = [];
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Laden und Übergeben */
|
||||
|
||||
load(json) {
|
||||
let data = {};
|
||||
|
||||
try {
|
||||
data = JSON.parse(json || '{}') || {};
|
||||
} catch (e) {
|
||||
data = {};
|
||||
}
|
||||
|
||||
this.tree = { areas: (data.areas || []).map(prepArea) };
|
||||
this.modal = null;
|
||||
this.modalNode = null;
|
||||
|
||||
if (this.$refs.dialog && this.$refs.dialog.open) {
|
||||
this.$refs.dialog.close();
|
||||
}
|
||||
|
||||
this.last = this.serialize();
|
||||
},
|
||||
|
||||
serialize() {
|
||||
return JSON.stringify({ areas: this.tree.areas }, (key, value) => (key === '_id' ? undefined : value));
|
||||
},
|
||||
|
||||
commit() {
|
||||
const json = this.serialize();
|
||||
|
||||
if (json === this.last) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.last = json;
|
||||
this.state = json;
|
||||
this.refreshSoon();
|
||||
},
|
||||
|
||||
// Die Regelliste im Register „Weitere Regeln“ liest den Aufbau auf dem Server; deshalb nach Änderungen kurz abgleichen.
|
||||
refreshSoon() {
|
||||
clearTimeout(this.refreshTimer);
|
||||
this.refreshTimer = setTimeout(() => this.$wire.$refresh(), 1500);
|
||||
},
|
||||
|
||||
snapshot() {
|
||||
this.history.push(this.serialize());
|
||||
|
||||
if (this.history.length > 40) {
|
||||
this.history.shift();
|
||||
}
|
||||
},
|
||||
|
||||
undo() {
|
||||
const previous = this.history.pop();
|
||||
|
||||
if (previous === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rest = this.history;
|
||||
this.load(previous);
|
||||
this.history = rest;
|
||||
this.last = '';
|
||||
this.commit();
|
||||
this.announce('Letzte Änderung rückgängig gemacht.');
|
||||
},
|
||||
|
||||
announce(text) {
|
||||
this.announcement = '';
|
||||
this.$nextTick(() => { this.announcement = text; });
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Hilfen */
|
||||
|
||||
isTmp(key) {
|
||||
return String(key || '').startsWith('tmp_');
|
||||
},
|
||||
|
||||
typeOf(value) {
|
||||
return this.cfg.types.find((type) => type.value === value) || { value, label: value, options: false, control: false };
|
||||
},
|
||||
|
||||
typeLabel(value) {
|
||||
return this.typeOf(value).label;
|
||||
},
|
||||
|
||||
all() {
|
||||
const out = [];
|
||||
|
||||
this.tree.areas.forEach((area) => {
|
||||
out.push(area);
|
||||
area.items.forEach((item) => {
|
||||
out.push(item);
|
||||
|
||||
if (item.kind === 'section') {
|
||||
item.items.forEach((inner) => out.push(inner));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return out;
|
||||
},
|
||||
|
||||
find(id) {
|
||||
for (let ai = 0; ai < this.tree.areas.length; ai++) {
|
||||
const area = this.tree.areas[ai];
|
||||
|
||||
if (area._id === id) {
|
||||
return { node: area, list: this.tree.areas, index: ai, parent: null };
|
||||
}
|
||||
|
||||
for (let ii = 0; ii < area.items.length; ii++) {
|
||||
const item = area.items[ii];
|
||||
|
||||
if (item._id === id) {
|
||||
return { node: item, list: area.items, index: ii, parent: area };
|
||||
}
|
||||
|
||||
if (item.kind === 'section') {
|
||||
for (let fi = 0; fi < item.items.length; fi++) {
|
||||
if (item.items[fi]._id === id) {
|
||||
return { node: item.items[fi], list: item.items, index: fi, parent: item };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
container(id) {
|
||||
if (id === 'areas') {
|
||||
return { list: this.tree.areas, node: null };
|
||||
}
|
||||
|
||||
const hit = this.find(id.slice(2));
|
||||
|
||||
return hit ? { list: hit.node.items, node: hit.node } : null;
|
||||
},
|
||||
|
||||
summary() {
|
||||
const nodes = this.all();
|
||||
const fields = nodes.filter((node) => node.kind === 'field').length;
|
||||
const sections = nodes.filter((node) => node.kind === 'section').length;
|
||||
|
||||
return this.tree.areas.length + (this.tree.areas.length === 1 ? ' Bereich' : ' Bereiche') + ' · ' + fields + (fields === 1 ? ' Feld' : ' Felder') + (sections ? ' · ' + sections + (sections === 1 ? ' Teilbereich' : ' Teilbereiche') : '');
|
||||
},
|
||||
|
||||
hasIssue() {
|
||||
return this.all().some((node) => node.kind === 'issue');
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Angaben, die noch fehlen */
|
||||
|
||||
fieldProblem(field) {
|
||||
if (!field.label.trim()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return this.typeOf(field.type).options && !field.options.some((option) => option.label.trim());
|
||||
},
|
||||
|
||||
problems() {
|
||||
let count = 0;
|
||||
|
||||
this.tree.areas.forEach((area) => {
|
||||
if (!area.title.trim()) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
|
||||
this.all().forEach((node) => {
|
||||
if (node.kind === 'section' && !node.label.trim()) {
|
||||
count++;
|
||||
} else if (node.kind === 'field' && this.fieldProblem(node)) {
|
||||
count++;
|
||||
}
|
||||
});
|
||||
|
||||
return count;
|
||||
},
|
||||
|
||||
optionsText(field) {
|
||||
const count = field.options.length;
|
||||
|
||||
return count === 0 ? 'Auswahlwerte fehlen noch' : 'Auswahlwerte (' + count + '): ' + field.options.map((option) => option.label).filter(Boolean).join(', ');
|
||||
},
|
||||
|
||||
issueText(item) {
|
||||
if (!item.issues.length) {
|
||||
return 'Alle angebotenen Anliegen';
|
||||
}
|
||||
|
||||
return item.issues.map((key) => (this.cfg.issues.find((issue) => issue.key === key) || { label: key }).label).join(', ');
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Hinzufügen und Entfernen */
|
||||
|
||||
addArea() {
|
||||
this.snapshot();
|
||||
const area = prepArea({ title: '', items: [] });
|
||||
this.tree.areas.push(area);
|
||||
this.afterAdd(area, '[data-fe-feld="titel"]');
|
||||
},
|
||||
|
||||
newField(base = {}) {
|
||||
return prepField({ key: 'tmp_' + random(8), type: 'text', ...base });
|
||||
},
|
||||
|
||||
addField(target) {
|
||||
this.snapshot();
|
||||
const field = this.newField();
|
||||
target.items.push(field);
|
||||
this.afterAdd(field, '[data-fe-feld="frage"]');
|
||||
},
|
||||
|
||||
addSection(area) {
|
||||
this.snapshot();
|
||||
const section = prepItem({ kind: 'section', key: 'tmp_' + random(8), label: '', items: [] });
|
||||
area.items.push(section);
|
||||
this.afterAdd(section, '[data-fe-feld="teil"]');
|
||||
},
|
||||
|
||||
addIssue(area) {
|
||||
if (this.hasIssue()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.snapshot();
|
||||
const issue = prepItem({ kind: 'issue', issues: [] });
|
||||
// Die Frage nach dem Anliegen steht üblicherweise zuoberst im Bereich.
|
||||
area.items.unshift(issue);
|
||||
this.afterAdd(issue, '.vw-fe-symbol');
|
||||
},
|
||||
|
||||
afterAdd(node, selector) {
|
||||
this.commit();
|
||||
this.$nextTick(() => {
|
||||
const el = this.$root.querySelector('[data-fe-id="' + node._id + '"] ' + selector);
|
||||
|
||||
if (el) {
|
||||
el.scrollIntoView({ block: 'nearest' });
|
||||
el.focus();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
insertCatalog(entry) {
|
||||
const target = this.modalNode;
|
||||
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.snapshot();
|
||||
const field = this.newField({
|
||||
key: entry.key,
|
||||
name: entry.name,
|
||||
label: entry.label,
|
||||
help: entry.help,
|
||||
type: entry.type,
|
||||
options: entry.options,
|
||||
purpose: entry.purpose,
|
||||
used_in: entry.used_in,
|
||||
});
|
||||
target.items.push(field);
|
||||
this.commit();
|
||||
this.closeModal();
|
||||
this.announce('Das Feld „' + field.label + '“ wurde eingefügt.');
|
||||
},
|
||||
|
||||
catalogEntries() {
|
||||
const used = new Set(this.all().filter((node) => node.kind === 'field').map((node) => node.key));
|
||||
const term = this.catalogSearch.trim().toLowerCase();
|
||||
|
||||
return this.cfg.catalog.filter((entry) => !used.has(entry.key) && (term === '' || (entry.label + ' ' + entry.name).toLowerCase().includes(term)));
|
||||
},
|
||||
|
||||
remove(node) {
|
||||
const hit = this.find(node._id);
|
||||
|
||||
if (!hit) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.snapshot();
|
||||
hit.list.splice(hit.index, 1);
|
||||
this.commit();
|
||||
|
||||
if (this.modal) {
|
||||
this.closeModal();
|
||||
}
|
||||
|
||||
this.announce('Entfernt. Mit „Rückgängig“ lässt sich das zurücknehmen.');
|
||||
},
|
||||
|
||||
typeChanged(field) {
|
||||
const type = this.typeOf(field.type);
|
||||
|
||||
if (type.options && field.options.length === 0) {
|
||||
field.options = [{ _id: uid(), key: '', label: '' }];
|
||||
}
|
||||
|
||||
if (field.type === 'boolean') {
|
||||
field.options = [{ _id: uid(), key: 'ja', label: 'Ja' }, { _id: uid(), key: 'nein', label: 'Nein' }];
|
||||
}
|
||||
|
||||
if (field.purpose && !this.purposesFor(field).some((purpose) => purpose.value === field.purpose)) {
|
||||
field.purpose = '';
|
||||
}
|
||||
|
||||
if (type.options && field.options.every((option) => !option.label.trim())) {
|
||||
this.openModal('options', field);
|
||||
}
|
||||
},
|
||||
|
||||
purposesFor(field) {
|
||||
return this.cfg.purposes.filter((purpose) => purpose.types.includes(field.type));
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Auswahlwerte */
|
||||
|
||||
addOption() {
|
||||
this.modalNode.options.push({ _id: uid(), key: '', label: '' });
|
||||
this.$nextTick(() => {
|
||||
const inputs = this.$root.querySelectorAll('[data-fe-werte] input');
|
||||
inputs[inputs.length - 1]?.focus();
|
||||
});
|
||||
},
|
||||
|
||||
removeOption(index) {
|
||||
this.modalNode.options.splice(index, 1);
|
||||
},
|
||||
|
||||
moveOption(index, delta) {
|
||||
const options = this.modalNode.options;
|
||||
const target = index + delta;
|
||||
|
||||
if (target < 0 || target >= options.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
options.splice(target, 0, options.splice(index, 1)[0]);
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Verschieben */
|
||||
|
||||
shift(node, delta) {
|
||||
const hit = this.find(node._id);
|
||||
|
||||
if (!hit) {
|
||||
return;
|
||||
}
|
||||
|
||||
const target = hit.index + delta;
|
||||
this.snapshot();
|
||||
|
||||
if (target >= 0 && target < hit.list.length) {
|
||||
hit.list.splice(hit.index, 1);
|
||||
hit.list.splice(target, 0, node);
|
||||
} else if (hit.parent && hit.parent.kind === 'section') {
|
||||
// Am Rand eines Teilbereichs: das Feld verlässt ihn, direkt vor oder hinter dem Teilbereich.
|
||||
const outer = this.find(hit.parent._id);
|
||||
hit.list.splice(hit.index, 1);
|
||||
outer.list.splice(outer.index + (delta < 0 ? 0 : 1), 0, node);
|
||||
} else {
|
||||
this.history.pop();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.commit();
|
||||
const now = this.find(node._id);
|
||||
this.announce((node.label || node.title || 'Element') + ': Position ' + (now.index + 1) + ' von ' + now.list.length + '.');
|
||||
setTimeout(() => this.$root.querySelector('[data-fe-id="' + node._id + '"] .vw-fe-griff')?.focus(), 40);
|
||||
},
|
||||
|
||||
movable(node) {
|
||||
return node && node.kind !== 'area' && this.targetsFor(node).length > 0;
|
||||
},
|
||||
|
||||
targetsFor(node) {
|
||||
const targets = [];
|
||||
|
||||
this.tree.areas.forEach((area, ai) => {
|
||||
targets.push({ value: 'a:' + area._id, label: 'Bereich ' + (ai + 1) + (area.title.trim() ? ' „' + area.title.trim() + '“' : '') });
|
||||
|
||||
if (node.kind === 'field') {
|
||||
area.items.filter((item) => item.kind === 'section').forEach((section) => {
|
||||
targets.push({ value: 's:' + section._id, label: ' Teilbereich „' + (section.label.trim() || 'ohne Überschrift') + '“ (Bereich ' + (ai + 1) + ')' });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return targets;
|
||||
},
|
||||
|
||||
moveTo(node, destination) {
|
||||
if (!destination) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hit = this.find(node._id);
|
||||
const to = this.container(destination);
|
||||
|
||||
if (!hit || !to) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.snapshot();
|
||||
hit.list.splice(hit.index, 1);
|
||||
to.list.push(node);
|
||||
this.commit();
|
||||
this.closeModal();
|
||||
this.announce('Verschoben.');
|
||||
},
|
||||
|
||||
// Ziehen und Ablegen -------------------------------------------------------------------------
|
||||
|
||||
mountList(el, id) {
|
||||
if (!window.Sortable) {
|
||||
return;
|
||||
}
|
||||
|
||||
const isAreas = id === 'areas';
|
||||
const inSection = id.startsWith('s:');
|
||||
|
||||
window.Sortable.create(el, {
|
||||
group: {
|
||||
name: isAreas ? 'fe-bereiche' : 'fe-elemente',
|
||||
pull: true,
|
||||
put: (to, from, dragged) => (isAreas ? false : (inSection ? dragged.dataset.feKind === 'field' : true)),
|
||||
},
|
||||
draggable: isAreas ? '[data-fe-area]' : '[data-fe-item]',
|
||||
handle: isAreas ? '.vw-fe-griff-bereich' : '.vw-fe-griff',
|
||||
animation: 150,
|
||||
ghostClass: 'vw-fe-geist',
|
||||
chosenClass: 'vw-fe-gegriffen',
|
||||
dragClass: 'vw-fe-gezogen',
|
||||
emptyInsertThreshold: 24,
|
||||
fallbackOnBody: true,
|
||||
swapThreshold: 0.6,
|
||||
onEnd: (event) => this.dropped(event),
|
||||
});
|
||||
},
|
||||
|
||||
dropped(event) {
|
||||
const { item, from, to, oldDraggableIndex, newDraggableIndex } = event;
|
||||
const id = item.dataset.feId;
|
||||
const draggable = item.hasAttribute('data-fe-area') ? '[data-fe-area]' : '[data-fe-item]';
|
||||
const target = to.dataset.feList;
|
||||
|
||||
if (from === to && oldDraggableIndex === newDraggableIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Die DOM-Position zurücksetzen: Alpine zeichnet die Listen aus dem Baum neu.
|
||||
item.remove();
|
||||
const siblings = Array.from(from.querySelectorAll(':scope > ' + draggable));
|
||||
from.insertBefore(item, siblings[oldDraggableIndex] || null);
|
||||
|
||||
const hit = this.find(id);
|
||||
const destination = this.container(target);
|
||||
|
||||
if (!hit || !destination) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ein Teilbereich bleibt auf der Ebene eines Bereichs; Felder dürfen überallhin.
|
||||
if (hit.node.kind === 'section' && destination.node && destination.node.kind === 'section') {
|
||||
return;
|
||||
}
|
||||
|
||||
this.snapshot();
|
||||
hit.list.splice(hit.index, 1);
|
||||
destination.list.splice(Math.min(newDraggableIndex, destination.list.length), 0, hit.node);
|
||||
this.commit();
|
||||
this.announce((hit.node.label || hit.node.title || 'Element') + ' verschoben.');
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Dialog */
|
||||
|
||||
openModal(kind, node) {
|
||||
this.opener = document.activeElement;
|
||||
this.catalogSearch = '';
|
||||
this.modal = { kind };
|
||||
this.modalNode = node;
|
||||
|
||||
if (kind === 'cond' && node.cond.effect && node.cond.conditions.length === 0) {
|
||||
this.addCond(node);
|
||||
}
|
||||
|
||||
this.$nextTick(() => {
|
||||
if (!this.$refs.dialog.open) {
|
||||
this.$refs.dialog.showModal();
|
||||
}
|
||||
|
||||
// Der Inhalt des Dialogs entsteht erst nach dem Öffnen; deshalb kurz warten, bis das Zielfeld da ist.
|
||||
setTimeout(() => {
|
||||
if (kind === 'options') {
|
||||
const inputs = Array.from(this.$root.querySelectorAll('[data-fe-werte] input'));
|
||||
(inputs.find((input) => input.value === '') || this.$root.querySelector('[data-fe-aktion="option-neu"]'))?.focus();
|
||||
} else {
|
||||
this.$refs.dialogFocus?.focus();
|
||||
}
|
||||
}, 60);
|
||||
});
|
||||
},
|
||||
|
||||
closeModal() {
|
||||
if (this.$refs.dialog.open) {
|
||||
this.$refs.dialog.close();
|
||||
} else {
|
||||
this.onDialogClose();
|
||||
}
|
||||
},
|
||||
|
||||
onDialogClose() {
|
||||
// Leere, nie gespeicherte Auswahlwerte fallen beim Schließen weg.
|
||||
if (this.modal && this.modal.kind === 'options' && this.modalNode) {
|
||||
this.modalNode.options = this.modalNode.options.filter((option) => option.label.trim() || option.key);
|
||||
}
|
||||
|
||||
this.commit();
|
||||
this.modal = null;
|
||||
this.modalNode = null;
|
||||
|
||||
if (this.opener && document.contains(this.opener)) {
|
||||
this.opener.focus();
|
||||
}
|
||||
},
|
||||
|
||||
dialogTitle() {
|
||||
const node = this.modalNode;
|
||||
|
||||
if (!this.modal || !node) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const name = (node.label || node.title || '').trim();
|
||||
const of = name ? ' „' + name + '“' : '';
|
||||
|
||||
switch (this.modal.kind) {
|
||||
case 'text': return node.kind === 'field' ? 'Hilfetext' + of : (node.kind === 'area' ? 'Einleitung des Bereichs' + of : 'Erläuterung des Teilbereichs' + of);
|
||||
case 'options': return 'Auswahlwerte' + of;
|
||||
case 'cond': return 'Bedingung' + of;
|
||||
case 'issue': return 'Frage nach dem Anliegen';
|
||||
case 'more': return 'Weitere Einstellungen' + of;
|
||||
case 'catalog': return 'Vorhandenes Feld einfügen';
|
||||
default: return '';
|
||||
}
|
||||
},
|
||||
|
||||
textKey() {
|
||||
const kind = this.modalNode?.kind;
|
||||
|
||||
return kind === 'field' ? 'help' : (kind === 'area' ? 'intro' : 'description');
|
||||
},
|
||||
|
||||
textLabel() {
|
||||
const kind = this.modalNode?.kind;
|
||||
|
||||
return kind === 'field' ? 'Hilfetext (steht unter der Frage)' : (kind === 'area' ? 'Einleitung (steht unter dem Titel des Bereichs)' : 'Erläuterung (steht unter der Überschrift)');
|
||||
},
|
||||
|
||||
textHint() {
|
||||
return this.modalNode?.kind === 'field' ? 'Ein Hilfetext erklärt, was in das Feld gehört. Leer lassen, wenn die Frage reicht.' : 'Optional. Leer lassen, wenn nichts erklärt werden muss.';
|
||||
},
|
||||
|
||||
/* ------------------------------------------------------------ Bedingungen */
|
||||
|
||||
hasCond(node) {
|
||||
return !!node.cond.effect && node.cond.conditions.some((c) => c.source);
|
||||
},
|
||||
|
||||
effectOptions(node) {
|
||||
return this.cfg.effects[kindOfEffect[node.kind]] || {};
|
||||
},
|
||||
|
||||
effectChanged(node) {
|
||||
if (node.cond.effect) {
|
||||
if (node.cond.conditions.length === 0) {
|
||||
this.addCond(node);
|
||||
}
|
||||
} else {
|
||||
node.cond.conditions = [];
|
||||
}
|
||||
},
|
||||
|
||||
addCond(node) {
|
||||
node.cond.conditions.push({ _id: uid(), source: '', op: 'eq', values: [] });
|
||||
},
|
||||
|
||||
removeCond(node, index) {
|
||||
node.cond.conditions.splice(index, 1);
|
||||
|
||||
if (node.cond.conditions.length === 0) {
|
||||
node.cond.effect = '';
|
||||
}
|
||||
},
|
||||
|
||||
toggleValue(cond, value, on) {
|
||||
const has = cond.values.includes(value);
|
||||
|
||||
if (on && !has) {
|
||||
cond.values.push(value);
|
||||
} else if (!on && has) {
|
||||
cond.values.splice(cond.values.indexOf(value), 1);
|
||||
}
|
||||
},
|
||||
|
||||
toggleIssue(item, key, on) {
|
||||
const has = item.issues.includes(key);
|
||||
|
||||
if (on && !has) {
|
||||
item.issues.push(key);
|
||||
} else if (!on && has) {
|
||||
item.issues.splice(item.issues.indexOf(key), 1);
|
||||
}
|
||||
},
|
||||
|
||||
// Felder, auf die sich eine Bedingung stützen darf: gespeicherte Auswahl- und Ja/Nein-Felder oberhalb des Elements.
|
||||
sourcesFor(node) {
|
||||
const sources = [];
|
||||
let issue = null;
|
||||
|
||||
for (const other of this.all()) {
|
||||
if (other._id === node._id) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (other.kind === 'issue') {
|
||||
issue = other;
|
||||
} else if (other.kind === 'field' && this.typeOf(other.type).control && !this.isTmp(other.key)) {
|
||||
sources.push({ value: other.key, label: other.label.trim() || other.key });
|
||||
}
|
||||
}
|
||||
|
||||
if (issue) {
|
||||
sources.unshift({ value: 'unteranliegen', label: 'Unteranliegen' }, { value: 'anliegen', label: 'Anliegen' });
|
||||
}
|
||||
|
||||
return sources;
|
||||
},
|
||||
|
||||
// Auswahl der Grundlage; eine nicht mehr gültige Grundlage bleibt sichtbar, damit sie ersetzt werden kann.
|
||||
sourceChoices(node, cond) {
|
||||
const sources = this.sourcesFor(node);
|
||||
|
||||
if (cond.source && !sources.some((source) => source.value === cond.source)) {
|
||||
sources.push({ value: cond.source, label: this.sourceLabel(cond.source) + ' (nicht mehr verfügbar)' });
|
||||
}
|
||||
|
||||
return sources;
|
||||
},
|
||||
|
||||
sourceLabel(source) {
|
||||
if (source === 'anliegen') {
|
||||
return 'Anliegen';
|
||||
}
|
||||
|
||||
if (source === 'unteranliegen') {
|
||||
return 'Unteranliegen';
|
||||
}
|
||||
|
||||
const field = this.all().find((node) => node.kind === 'field' && node.key === source);
|
||||
|
||||
return field ? (field.label.trim() || source) : source;
|
||||
},
|
||||
|
||||
sourceValues(node, source) {
|
||||
if (!source) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (source === 'anliegen' || source === 'unteranliegen') {
|
||||
const only = (this.all().find((other) => other.kind === 'issue') || { issues: [] }).issues;
|
||||
const values = [];
|
||||
|
||||
this.cfg.issues.forEach((issue) => {
|
||||
if (only.length && !only.includes(issue.key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (source === 'anliegen') {
|
||||
values.push({ value: issue.key, label: issue.label });
|
||||
} else {
|
||||
issue.children.forEach((child) => values.push({ value: child.key, label: issue.label + ' › ' + child.label }));
|
||||
}
|
||||
});
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
const field = this.all().find((other) => other.kind === 'field' && other.key === source);
|
||||
|
||||
return field ? field.options.filter((option) => option.key).map((option) => ({ value: option.key, label: option.label })) : [];
|
||||
},
|
||||
|
||||
condSourceProblem(node, cond) {
|
||||
return !!cond.source && !this.sourcesFor(node).some((source) => source.value === cond.source);
|
||||
},
|
||||
|
||||
condProblem(node) {
|
||||
return !!node.cond.effect && node.cond.conditions.some((cond) => this.condSourceProblem(node, cond));
|
||||
},
|
||||
|
||||
condText(node) {
|
||||
if (!this.hasCond(node)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const effect = (this.effectOptions(node)[node.cond.effect] || '').replace(/\s*…$/, '').replace(/,\s*wenn$/, '');
|
||||
const joiner = node.cond.match === 'any' ? ' oder ' : ' und ';
|
||||
const parts = node.cond.conditions.filter((cond) => cond.source).map((cond) => {
|
||||
const values = cond.values.map((value) => {
|
||||
const found = this.sourceValues(node, cond.source).find((option) => option.value === value);
|
||||
|
||||
return found ? found.label : value;
|
||||
});
|
||||
|
||||
return this.sourceLabel(cond.source) + ' ' + (this.cfg.operators[cond.op] || cond.op) + ' ' + (values.join(' / ') || '…');
|
||||
});
|
||||
|
||||
return effect + ', wenn ' + parts.join(joiner) + (this.condProblem(node) ? ' – bitte prüfen: Grundlage steht nicht oberhalb' : '');
|
||||
},
|
||||
};
|
||||
};
|
||||
</script>
|
||||
@endverbatim
|
||||
Reference in new issue
Block a user