adrema/resources/js/views/formtemplate/FormBuilder.vue

147 lines
5.3 KiB
Vue
Raw Normal View History

2023-12-25 19:45:34 +01:00
<template>
2023-12-27 23:38:13 +01:00
<ui-popup v-if="singleField !== null && singleField.model === null" heading="Feldtyp auswählen"
@close="singleField = null">
<div class="mt-3 grid gap-3 grid-cols-3">
<a v-for="(field, index) in props.meta.fields" :key="index"
class="py-2 px-3 border rounded bg-zinc-800 hover:bg-zinc-700 transition" href="#"
@click.prevent="setFieldType(field)">
<span v-text="field.name"></span>
2023-12-25 22:46:06 +01:00
</a>
2023-12-27 23:38:13 +01:00
</div>
2023-12-25 22:46:06 +01:00
</ui-popup>
2023-12-27 23:38:13 +01:00
<form class="grid gap-3 mt-4 grid-cols-[1fr_max-content] items-start" @submit.prevent="submit">
<div class="grid gap-3">
<slot name="meta"></slot>
<asideform v-if="singleSection !== null"
:heading="`Sektion ${singleSection.index !== null ? 'bearbeiten' : 'erstellen'}`"
@close="singleSection = null" @submit="storeSection">
<f-text :id="`sectionform-name`" v-model="singleSection.model.name" label="Name"
:name="`sectionform-name`"></f-text>
<f-textarea :id="`sectionform-intro`" v-model="singleSection.model.intro" label="Einleitung"
:name="`sectionform-intro`"></f-textarea>
</asideform>
<asideform v-if="singleField !== null && singleField.model !== null"
:heading="`Feld ${singleField.index !== null ? 'bearbeiten' : 'erstellen'}`" @close="singleField = null"
@submit="storeField">
<f-text id="fieldname" v-model="singleField.model.name" label="Name" size="sm" name="fieldname"></f-text>
<column-selector v-model="singleField.model.columns"></column-selector>
<component :is="fields[singleField.model.type]" v-model="singleField.model"></component>
</asideform>
</div>
<ui-box heading="Vorschau" container-class="grid gap-3" class="w-[800px]">
<event-form editable
style="--primary: hsl(181, 75%, 26%); --secondary: hsl(181, 75%, 35%); --font: hsl(181, 84%, 78%); --circle: hsl(181, 86%, 16%)"
:value="previewString" @editSection="editSection($event.detail[0])" @addSection="addSection"
@addField="addField($event.detail[0])" @editField="editField($event.detail[0], $event.detail[1])"
@deleteField="deleteField($event.detail[0], $event.detail[1])"
@deleteSection="deleteSection($event.detail[0])"></event-form>
</ui-box>
</form>
2023-12-25 19:45:34 +01:00
</template>
<script setup>
2023-12-27 23:38:13 +01:00
import { computed, ref } from 'vue';
import { snakeCase } from 'change-case';
2023-12-25 19:45:34 +01:00
import '!/eventform/dist/main.js';
2023-12-25 22:46:06 +01:00
import Asideform from './Asideform.vue';
2023-12-26 00:44:49 +01:00
import TextareaField from './TextareaField.vue';
2023-12-26 20:20:32 +01:00
import TextField from './TextField.vue';
import DropdownField from './RadioField.vue';
2023-12-26 00:44:49 +01:00
import RadioField from './RadioField.vue';
2023-12-26 01:05:45 +01:00
import CheckboxField from './CheckboxField.vue';
2023-12-26 20:20:32 +01:00
import CheckboxesField from './CheckboxesField.vue';
2023-12-27 01:20:07 +01:00
import ColumnSelector from './ColumnSelector.vue';
2023-12-25 19:45:34 +01:00
2023-12-25 22:46:06 +01:00
const singleSection = ref(null);
const singleField = ref(null);
2023-12-25 19:45:34 +01:00
const props = defineProps({
modelValue: {},
meta: {},
});
2023-12-27 23:38:13 +01:00
const emit = defineEmits(['update:modelValue']);
2023-12-25 22:46:06 +01:00
2023-12-26 00:44:49 +01:00
const fields = {
TextareaField: TextareaField,
2023-12-26 20:20:32 +01:00
TextField: TextField,
2023-12-26 00:44:49 +01:00
DropdownField: DropdownField,
RadioField: RadioField,
2023-12-26 01:05:45 +01:00
CheckboxField: CheckboxField,
CheckboxesField: CheckboxesField,
2023-12-26 00:44:49 +01:00
};
2023-12-25 22:46:06 +01:00
function editSection(sectionIndex) {
singleSection.value = {
2023-12-27 23:38:13 +01:00
model: { ...inner.value.sections[sectionIndex] },
2023-12-25 22:46:06 +01:00
index: sectionIndex,
};
}
2023-12-25 19:45:34 +01:00
2023-12-27 23:38:13 +01:00
function push() {
emit('update:modelValue', inner.value);
}
2023-12-25 19:45:34 +01:00
const inner = ref(JSON.parse(JSON.stringify(props.modelValue)));
2023-12-25 22:46:06 +01:00
const innerMeta = ref(JSON.parse(JSON.stringify(props.meta)));
2023-12-25 19:45:34 +01:00
function addSection() {
2023-12-25 22:46:06 +01:00
singleSection.value = {
model: JSON.parse(JSON.stringify(innerMeta.value.section_default)),
index: null,
};
}
function editField(sindex, findex) {
singleField.value = {
2023-12-27 23:38:13 +01:00
model: JSON.parse(JSON.stringify(inner.value.sections[sindex].fields[findex])),
2023-12-25 22:46:06 +01:00
sectionIndex: sindex,
index: findex,
};
2023-12-25 19:45:34 +01:00
}
2023-12-25 22:46:06 +01:00
function storeSection() {
if (singleSection.value.index !== null) {
2023-12-27 23:38:13 +01:00
inner.value.sections.splice(singleSection.value.index, 1, singleSection.value.model);
2023-12-25 22:46:06 +01:00
} else {
2023-12-27 23:38:13 +01:00
inner.value.sections.push(singleSection.value.model);
2023-12-25 22:46:06 +01:00
}
singleSection.value = null;
2023-12-27 23:38:13 +01:00
push();
2023-12-25 19:45:34 +01:00
}
2023-12-25 22:46:06 +01:00
function storeField() {
2023-12-26 20:06:57 +01:00
singleField.value.model.key = snakeCase(singleField.value.model.name);
2023-12-25 22:46:06 +01:00
if (singleField.value.index !== null) {
2023-12-27 23:38:13 +01:00
inner.value.sections[singleField.value.sectionIndex].fields.splice(singleField.value.index, 1, singleField.value.model);
2023-12-25 22:46:06 +01:00
} else {
2023-12-27 23:38:13 +01:00
inner.value.sections[singleField.value.sectionIndex].fields.push(singleField.value.model);
2023-12-25 19:45:34 +01:00
}
2023-12-25 22:46:06 +01:00
singleField.value = null;
2023-12-27 23:38:13 +01:00
push();
2023-12-25 22:46:06 +01:00
}
function deleteField(sindex, findex) {
2023-12-27 23:38:13 +01:00
inner.value.sections[sindex].fields.splice(findex, 1);
push();
2023-12-25 22:46:06 +01:00
}
function addField(sectionIndex) {
singleField.value = {
model: null,
sectionIndex: sectionIndex,
index: null,
};
}
2023-12-27 01:27:47 +01:00
function deleteSection(sindex) {
2023-12-27 23:38:13 +01:00
inner.value.sections.splice(sindex, 1);
push();
2023-12-27 01:27:47 +01:00
}
2023-12-25 22:46:06 +01:00
function setFieldType(field) {
singleField.value.model = JSON.parse(JSON.stringify(field.default));
2023-12-25 19:45:34 +01:00
}
2023-12-27 23:38:13 +01:00
const previewString = computed(() => (inner.value && inner.value ? JSON.stringify(inner.value) : '{}'));
2023-12-25 19:45:34 +01:00
</script>