adrema/resources/js/views/form/Conditions.vue

157 lines
5.2 KiB
Vue
Raw Normal View History

2024-04-18 22:15:28 +02:00
<template>
2024-04-18 22:18:56 +02:00
<ui-note v-if="locked" class="mt-2" type="danger">
2024-04-18 22:15:28 +02:00
Dieses Formular wurde bereits bearbeitet.<br />
Bitte speichere es erst ab und editiere dann die Bedingungen.
</ui-note>
<div v-else>
2024-04-19 22:13:55 +02:00
<f-select id="mode" v-model="inner.mode" :options="modeOptions" name="mode" label="Modus"></f-select>
2024-04-18 22:15:28 +02:00
<ui-icon-button class="mt-4 mb-2" icon="plus" @click="addCondition">Bedingung einfügen</ui-icon-button>
2024-04-19 22:06:09 +02:00
<div v-for="(condition, index) in inner.ifs" :key="index" class="grid grid-cols-[1fr_1fr_1fr_max-content] gap-2">
2024-04-19 23:57:05 +02:00
<f-select
:id="`field-${index}`"
:model-value="condition.field"
:options="fieldOptions"
:name="`field-${index}`"
label="Feld"
@update:model-value="update(index, 'field', $event)"
></f-select>
2024-04-18 22:15:28 +02:00
<f-select
2024-04-18 22:18:56 +02:00
:id="`comparator-${index}`"
2024-04-18 22:15:28 +02:00
:options="comparatorOptions"
:model-value="condition.comparator"
:name="`comparator-${index}`"
label="Vergleich"
2024-04-19 23:57:05 +02:00
@update:model-value="update(index, 'comparator', $event)"
2024-04-18 22:15:28 +02:00
></f-select>
<f-select
2024-04-19 23:57:05 +02:00
v-if="condition.field && ['isEqual', 'isNotEqual'].includes(condition.comparator) && ['RadioField', 'DropdownField'].includes(getField(condition.field).type)"
2024-04-18 22:15:28 +02:00
:id="`value-${index}`"
2024-04-18 22:18:56 +02:00
v-model="condition.value"
:options="getOptions(condition.field)"
2024-04-18 22:15:28 +02:00
:name="`value-${index}`"
label="Wert"
></f-select>
<f-multipleselect
2024-04-19 23:57:05 +02:00
v-if="condition.field && ['isIn', 'isNotIn'].includes(condition.comparator) && ['RadioField', 'DropdownField'].includes(getField(condition.field).type)"
2024-04-18 22:15:28 +02:00
:id="`value-${index}`"
2024-04-18 22:18:56 +02:00
v-model="condition.value"
:options="getOptions(condition.field)"
2024-04-18 22:15:28 +02:00
:name="`value-${index}`"
label="Wert"
></f-multipleselect>
2024-04-19 23:57:05 +02:00
<f-switch
v-if="condition.field && condition.comparator && ['CheckboxField'].includes(getField(condition.field).type)"
:id="`value-${index}`"
v-model="condition.value"
:name="`value-${index}`"
label="Wert"
></f-switch>
2024-04-19 22:06:09 +02:00
<ui-action-button tooltip="Löschen" icon="trash" class="btn-danger self-end h-8" @click="inner.ifs.splice(index, 1)"></ui-action-button>
2024-04-18 22:15:28 +02:00
</div>
<ui-icon-button class="mt-4 mb-2" icon="save" @click="save">Speichern</ui-icon-button>
</div>
</template>
<script setup>
import {ref, inject, computed} from 'vue';
const axios = inject('axios');
2024-04-19 13:49:47 +02:00
const emit = defineEmits(['save']);
2024-04-18 22:15:28 +02:00
const props = defineProps({
value: {
required: true,
},
single: {
required: true,
},
});
const comparatorOptions = ref([
2024-04-19 23:57:05 +02:00
{id: 'isEqual', name: 'ist gleich', defaultValue: {DropdownField: null, RadioField: null, CheckboxField: false}},
{id: 'isNotEqual', name: 'ist ungleich', defaultValue: {DropdownField: null, RadioField: null, CheckboxField: false}},
{id: 'isIn', name: 'ist in', defaultValue: {DropdownField: [], RadioField: [], CheckboxField: false}},
{id: 'isNotIn', name: 'ist nicht in', defaultValue: {DropdownField: [], RadioField: [], CheckboxField: false}},
2024-04-18 22:15:28 +02:00
]);
2024-04-19 22:13:55 +02:00
const modeOptions = ref([
{id: 'all', name: 'alle Bedingungen müssen zutreffen'},
{id: 'any', name: 'mindestens eine Bedingung muss zutreffen'},
]);
2024-04-18 22:15:28 +02:00
const fields = computed(() => {
const result = [];
props.single.config.sections.forEach((section) => {
section.fields.forEach((field) => {
if (['DropdownField', 'RadioField', 'CheckboxField'].includes(field.type)) {
result.push(field);
}
});
});
return result;
});
2024-04-19 23:57:05 +02:00
function update(index, key, value) {
if (key === 'comparator') {
var old = inner.value.ifs[index];
inner.value.ifs[index] = {
field: old.field,
comparator: value,
value: old.field ? comparatorOptions.value.find((c) => c.id === value).defaultValue[getField(old.field).type] : null,
};
}
if (key === 'field') {
var old = inner.value.ifs[index];
inner.value.ifs[index] = {
field: value,
comparator: null,
value: null,
};
}
2024-04-18 22:15:28 +02:00
}
function getField(fieldName) {
return fields.value.find((f) => f.key === fieldName);
}
function getOptions(fieldName) {
return getField(fieldName).options.map((o) => {
return {id: o, name: o};
});
}
const fieldOptions = computed(() =>
fields.value.map((field) => {
return {id: field.key, name: field.name};
})
);
2024-04-19 22:06:09 +02:00
const inner = ref(JSON.parse(JSON.stringify(props.value)));
2024-04-18 22:15:28 +02:00
const locked = ref(false);
function addCondition() {
2024-04-19 22:06:09 +02:00
inner.value.ifs.push({
2024-04-18 22:15:28 +02:00
field: null,
comparator: null,
value: null,
});
}
async function save() {
2024-04-19 22:06:09 +02:00
emit('save', inner.value);
2024-04-18 22:15:28 +02:00
}
async function checkIfDirty() {
const response = await axios.post(props.single.links.is_dirty, {config: props.single.config});
locked.value = response.data.result;
}
checkIfDirty();
</script>