ff-admin/src/components/queryBuilder/BuilderHost.vue

196 lines
6.2 KiB
Vue
Raw Normal View History

2024-12-14 17:02:34 +01:00
<template>
<div class="flex flex-col border border-gray-300 rounded-md select-none">
<div class="flex flex-row max-lg:flex-wrap gap-2 border-b border-gray-300 p-2">
2024-12-16 13:56:06 +01:00
<div
class="p-1 border border-gray-400 bg-green-200 rounded-md"
title="Abfrage starten"
@click="$emit('query:run')"
>
2024-12-14 17:02:34 +01:00
<PlayIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
2024-12-16 13:56:06 +01:00
<div
class="p-1 border border-gray-400 bg-gray-100 rounded-md"
title="Ergebnisse exportieren"
@click="$emit('results:export')"
>
<ArchiveBoxArrowDownIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
<div
class="p-1 border border-gray-400 bg-red-200 rounded-md"
title="Ergebnisse leeren"
@click="$emit('results:clear')"
>
2024-12-14 17:02:34 +01:00
<NoSymbolIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
<div class="p-1 border border-gray-400 bg-red-200 rounded-md" title="Anfrage löschen" @click="clearQuery">
2024-12-16 17:49:18 +01:00
<TrashIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
<div class="grow"></div>
<div
v-if="allowPredefinedSelect && can('read', 'settings', 'query_store')"
class="flex flex-row gap-2 max-lg:w-full max-lg:order-10"
>
2024-12-18 22:27:44 +01:00
<select v-model="activeQueryId" class="max-h-[34px] !py-0">
<option :value="undefined" disabled>gepeicherte Anfrage auswählen</option>
<option v-for="query in queries" :key="query.id" :value="query.id" @click="value = query.query">
{{ query.title }}
</option>
</select>
<div
v-if="can('create', 'settings', 'query_store')"
class="p-1 border border-gray-400 bg-gray-100 rounded-md"
title="Abfrage speichern"
@click="$emit('query:save')"
>
<InboxArrowDownIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
</div>
<div class="grow max-lg:hidden"></div>
<div class="flex flex-row min-w-fit overflow-hidden border border-gray-400 rounded-md">
2024-12-18 14:29:33 +01:00
<div
class="p-1"
:class="queryMode == 'structure' ? 'bg-gray-200' : ''"
title="Schema-Struktur"
@click="queryMode = 'structure'"
>
<SparklesIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
<div
class="p-1"
2024-12-18 22:27:44 +01:00
:class="typeof value == 'object' ? 'bg-gray-200' : ''"
title="Visual Builder"
@click="queryMode = 'builder'"
>
<RectangleGroupIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
<div
class="p-1"
2024-12-18 22:27:44 +01:00
:class="typeof value == 'string' ? 'bg-gray-200' : ''"
title="SQL Editor"
@click="queryMode = 'editor'"
>
<CommandLineIcon class="text-gray-500 h-6 w-6 cursor-pointer" />
</div>
</div>
2024-12-14 17:02:34 +01:00
</div>
<div class="p-2 h-44 md:h-60 w-full overflow-y-auto">
2024-12-18 14:29:33 +01:00
<div v-if="queryMode == 'structure'">
<img src="/administration-db.png" class="h-full w-full cursor-pointer" @click="showStructure" />
</div>
<textarea v-else-if="typeof value == 'string'" v-model="value" placeholder="SQL Query" class="h-full w-full" />
<Table v-else v-model="value" />
2024-12-14 17:02:34 +01:00
</div>
</div>
</template>
<script setup lang="ts">
2024-12-18 14:29:33 +01:00
import { defineAsyncComponent, defineComponent, markRaw, type PropType } from "vue";
2024-12-18 22:27:44 +01:00
import { mapActions, mapState, mapWritableState } from "pinia";
2024-12-14 17:02:34 +01:00
import type { DynamicQueryStructure } from "../../types/dynamicQueries";
2024-12-16 17:49:18 +01:00
import {
ArchiveBoxArrowDownIcon,
CommandLineIcon,
2024-12-16 17:49:18 +01:00
InboxArrowDownIcon,
NoSymbolIcon,
PlayIcon,
RectangleGroupIcon,
2024-12-16 17:49:18 +01:00
TrashIcon,
2024-12-18 14:29:33 +01:00
SparklesIcon,
2024-12-16 17:49:18 +01:00
} from "@heroicons/vue/24/outline";
2024-12-14 17:02:34 +01:00
import { useQueryBuilderStore } from "../../stores/admin/queryBuilder";
2024-12-18 14:29:33 +01:00
import { useModalStore } from "../../stores/modal";
import Table from "./Table.vue";
2024-12-18 22:27:44 +01:00
import { useAbilityStore } from "@/stores/ability";
import { useQueryStoreStore } from "@/stores/admin/queryStore";
2024-12-14 17:02:34 +01:00
</script>
<script lang="ts">
export default defineComponent({
props: {
modelValue: {
type: [Object, String] as PropType<DynamicQueryStructure | string>,
2024-12-14 17:02:34 +01:00
default: {
select: "*",
table: "",
where: [],
join: [],
orderBy: [],
},
},
2024-12-18 22:27:44 +01:00
allowPredefinedSelect: {
type: Boolean,
default: false,
},
2024-12-14 17:02:34 +01:00
},
2024-12-16 13:56:06 +01:00
emits: ["update:model-value", "query:run", "query:save", "results:export", "results:clear"],
watch: {
queryMode() {
if (this.queryMode == "builder") {
this.value = {
select: "*",
table: "",
where: [],
join: [],
orderBy: [],
};
} else {
this.value = "";
}
},
2024-12-18 22:27:44 +01:00
activeQueryId() {
let query = this.queries.find((t) => t.id == this.activeQueryId)?.query;
if (query != undefined) {
this.value = query;
}
},
},
data() {
return {
2024-12-18 22:27:44 +01:00
autoChangeFlag: false as boolean,
2024-12-18 14:29:33 +01:00
queryMode: "builder" as "builder" | "editor" | "structure",
};
},
2024-12-14 17:02:34 +01:00
computed: {
2024-12-18 22:27:44 +01:00
...mapState(useAbilityStore, ["can"]),
2024-12-14 17:02:34 +01:00
...mapState(useQueryBuilderStore, ["tableMetas", "loading"]),
2024-12-18 22:27:44 +01:00
...mapWritableState(useQueryBuilderStore, ["activeQueryId"]),
...mapState(useQueryStoreStore, ["queries"]),
value: {
get() {
return this.modelValue;
},
set(val: DynamicQueryStructure) {
this.$emit("update:model-value", val);
},
},
2024-12-14 17:02:34 +01:00
},
mounted() {
this.fetchTableMetas();
2024-12-18 22:27:44 +01:00
this.fetchQueries();
2024-12-14 17:02:34 +01:00
},
methods: {
2024-12-18 14:29:33 +01:00
...mapActions(useModalStore, ["openModal"]),
2024-12-14 17:02:34 +01:00
...mapActions(useQueryBuilderStore, ["fetchTableMetas"]),
2024-12-18 22:27:44 +01:00
...mapActions(useQueryStoreStore, ["fetchQueries"]),
clearQuery() {
this.$emit("update:model-value", undefined);
2024-12-18 22:27:44 +01:00
this.activeQueryId = undefined;
if (typeof this.value != "string") {
this.value = {
select: "*",
table: "",
where: [],
join: [],
orderBy: [],
};
} else {
this.value = "";
}
},
2024-12-18 14:29:33 +01:00
showStructure() {
this.openModal(markRaw(defineAsyncComponent(() => import("@/components/queryBuilder/StructureModal.vue"))));
},
2024-12-14 17:02:34 +01:00
},
});
</script>