ldc-legaulois/resources/ts/bo/components/MainMenuNodeEditor.vue

178 lines
4.4 KiB
Vue
Raw Normal View History

2024-04-09 12:31:45 +02:00
<template>
2024-04-12 10:18:11 +02:00
<!-- NODE PROPS EDITOR -->
<fieldset class="main-menu-node-editor-component container-fluid p-4 border mb-3">
<legend>
<span class="fs-5">{{ trans.methods.__("Modification d'un noeud") }}</span>
<button
class="btn btn-sm btn-dark float-end mt-2"
:title="trans.methods.__('Fermer la modification')"
@click.prevent="closeEditor"
>
<FontAwesomeIcon
icon="fa fa-close"
/>
</button>
</legend>
2024-04-09 12:31:45 +02:00
2024-04-12 10:18:11 +02:00
<!-- NODE TYPE -->
<div class="mb-3 row">
<label
for="type"
class="col-sm-2 col-form-label"
>
{{ trans.methods.__("Type de Noeud") }}
</label>
<div class="col-sm-4">
<input
type="text"
class="form-control"
:value="props.enum.label"
disabled
2024-04-09 12:31:45 +02:00
>
</div>
2024-04-12 10:18:11 +02:00
</div>
2024-04-09 12:31:45 +02:00
2024-04-12 10:18:11 +02:00
<!-- Menu Name -->
<div
class="mb-3 row"
>
<label
for="type"
class="col-sm-6 col-form-label"
2024-04-11 09:37:38 +02:00
>
2024-04-12 10:18:11 +02:00
{{ trans.methods.__("Titre du noeud principal affiché dans le menu") }}
</label>
<div class="col input-group">
<input
type="text"
class="form-control"
:value="menuNode.title"
@blur="menuNode.title = ($event.target as HTMLInputElement).value"
2024-04-11 09:37:38 +02:00
>
</div>
2024-04-12 10:18:11 +02:00
</div>
2024-04-11 09:37:38 +02:00
2024-04-12 10:18:11 +02:00
<!-- ROUTE NAME -->
<div
v-if="props.enum.value !== MenuNodeTypeEnum.link"
class="mb-3 row"
>
<label
for="type"
class="col-sm-2 col-form-label"
2024-04-09 12:31:45 +02:00
>
2024-04-12 10:18:11 +02:00
{{ trans.methods.__("Route") }}
</label>
<div class="col input-group">
<input
type="text"
class="form-control"
:value="route.methods.route(String(menuNode.link.routeName))"
disabled
>
<input
type="text"
class="form-control"
:value="menuNode.link.routeName"
disabled
2024-04-09 12:31:45 +02:00
>
</div>
2024-04-12 10:18:11 +02:00
</div>
<!-- URL -->
<div
v-else
class="mb-3 row"
>
<label
for="type"
class="col-sm-2 col-form-label"
2024-04-09 12:31:45 +02:00
>
2024-04-12 10:18:11 +02:00
{{ trans.methods.__("Url") }}
</label>
<div class="col input-group">
<input
2024-04-19 12:23:01 +02:00
ref="urlInput"
2024-04-12 10:18:11 +02:00
type="text"
class="form-control"
:value="String(menuNode.link.url)"
2024-04-19 12:23:01 +02:00
@blur="saveUrl(($event.target as HTMLInputElement | null)!.value)"
2024-04-09 12:31:45 +02:00
>
</div>
2024-04-12 10:18:11 +02:00
<small
v-if="!urlIsValid"
class="d-block text-danger"
>{{ trans.methods.__("L'url saisie n'est pas valide") }}</small>
</div>
</fieldset>
2024-04-09 12:31:45 +02:00
</template>
<script lang="ts" setup>
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
2024-04-11 09:37:38 +02:00
import { defineOptions, onMounted, ref, watch, type PropType } from "vue";
2024-04-09 12:31:45 +02:00
import route from "../../modules/route";
import trans from "../../modules/trans";
import type { MenuNodeInterface } from "../../types/models/MenuNode";
import { MenuNodeTypeEnum } from "../enums";
2024-04-12 10:18:11 +02:00
import MainMenuNodeDataEditor from "./MainMenuNodeDataEditor.vue";
2024-04-09 12:31:45 +02:00
defineOptions({
name: "MainMenuNodeEditor",
2024-04-12 10:18:11 +02:00
components: {MainMenuNodeDataEditor}
2024-04-09 12:31:45 +02:00
});
// * EMITS
const emits = defineEmits<{
2024-04-11 09:37:38 +02:00
closeEditor: [],
updatedNode: [menuNode: MenuNodeInterface],
2024-04-09 12:31:45 +02:00
}>();
// * ATTRIBUTES
// const attrs = useAttrs();
// * PROPS
const props = defineProps({
menuNode: {
type: Object as PropType<MenuNodeInterface>,
required: true
},
enum: {
type: Object as PropType<Enum>,
required: true
}
});
// * DATA
2024-04-11 09:37:38 +02:00
const menuNode = ref<MenuNodeInterface>(Object.assign({}, props.menuNode));
2024-04-09 12:31:45 +02:00
const urlIsValid = ref<boolean>(true);
// * LIFECYCLE
2024-04-19 12:23:01 +02:00
onMounted(() => {
urlIsValid.value = isUrlValid(String(menuNode.value.link.url));
});
2024-04-11 09:37:38 +02:00
watch(menuNode, (newValue) => {
emits("updatedNode", newValue);
}, { deep: true });
2024-04-19 12:23:01 +02:00
// * REFS
const urlInput = ref<HTMLInputElement|null>(null);
2024-04-09 12:31:45 +02:00
// * METHODS
2024-04-19 12:23:01 +02:00
function saveUrl(newUrl: string): void {
urlIsValid.value = isUrlValid(newUrl);
menuNode.value.link.url = urlInput.value!.value;
}
2024-04-09 12:31:45 +02:00
function closeEditor(): void {
emits("closeEditor");
}
function isUrlValid(url: string):boolean {
const regex = RegExp("(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+@]*)*(\\?[;&a-z\\d%_.~+=-@]*)?(\\#[-a-z\\d_@]*)?$", "i");
return url.match(regex)?.[0] !== undefined;
}
</script>
<style lang="scss" scoped>
.main-menu-node-editor-component {
max-height: 30rem;
}
</style>