454 lines
13 KiB
Vue
454 lines
13 KiB
Vue
<template>
|
|
<div :class="`directories-${intId} position-relative`">
|
|
<Transition name="fade">
|
|
<p
|
|
v-if="intMessage"
|
|
class="d-flex align-items-center text-danger mb-2"
|
|
>
|
|
<span>{{ Object.values(intMessage)[0] }} </span>
|
|
<a
|
|
class="btn btn-sm btn-primary"
|
|
:href="getEditButtonRoute(Number(Object.keys(intMessage)[0]))"
|
|
:title="__('Modifier ce paramètre')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-pencil" />
|
|
</a>
|
|
</p>
|
|
</Transition>
|
|
<vue-nestable
|
|
:value="intDirectories"
|
|
@change="updateOrderDirectories"
|
|
@input="intDirectories = $event"
|
|
:hooks="{ beforeMove: beforeMove }"
|
|
>
|
|
<template
|
|
#default="{
|
|
item,
|
|
}: {
|
|
item: {
|
|
id: number,
|
|
name: string[],
|
|
needAuthorization: boolean,
|
|
children: number[],
|
|
is_system: boolean,
|
|
},
|
|
}"
|
|
>
|
|
<vue-nestable-handle
|
|
class="d-flex justify-content-between align-items-center border rounded px-2 py-1"
|
|
>
|
|
<div class="d-flex justify-content-start align-items-center">
|
|
<div
|
|
v-if="
|
|
typeof item.children != 'undefined' && item.children.length > 0
|
|
"
|
|
>
|
|
<button
|
|
v-if="intDisplay[item.id]"
|
|
@click="displayList($event, item.id)"
|
|
class="btn btn-sm btn-outline-dark"
|
|
:title="__('Dérouler la liste')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-arrow-down" />
|
|
</button>
|
|
<button
|
|
v-else
|
|
@click="displayList($event, item.id)"
|
|
class="btn btn-sm btn-outline-dark"
|
|
:title="__('Rouler la liste')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-arrow-up" />
|
|
</button>
|
|
</div>
|
|
<button class="btn btn-sm border-0 disabled opacity-100">
|
|
<FontAwesomeIcon icon="fa-solid fa-grip-vertical" />
|
|
</button>
|
|
<p class="m-0">
|
|
{{ item.name["fr" as keyof typeof item.name] }}
|
|
</p>
|
|
</div>
|
|
<div
|
|
v-if="!item.is_system"
|
|
class="d-flex justify-content-end align-items-center"
|
|
>
|
|
<div class="input-group">
|
|
<button
|
|
v-if="item.needAuthorization"
|
|
@click="updateAuthorization(item)"
|
|
class="btn btn-sm btn-warning"
|
|
:title="__('L\'accès à ce dossier est restreint')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-lock" />
|
|
</button>
|
|
<button
|
|
v-else
|
|
@click="updateAuthorization(item)"
|
|
class="btn btn-sm btn-success"
|
|
:title="__('L\'accès à ce dossier est tout public')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-lock-open" />
|
|
</button>
|
|
<a
|
|
:href="getEditDirectoryRoute(item.id)"
|
|
class="btn btn-sm btn-primary"
|
|
:title="__('Modifier le Dossier')"
|
|
data-bs="tooltip"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-pencil" />
|
|
</a>
|
|
<button
|
|
@click="deleteDirectory($event, item)"
|
|
class="btn btn-sm btn-danger confirmDelete"
|
|
:title="__('Supprimer le Dossier')"
|
|
data-bs="tooltip"
|
|
ref="confirmDelete"
|
|
>
|
|
<FontAwesomeIcon icon="fa-solid fa-trash" />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</vue-nestable-handle>
|
|
</template>
|
|
</vue-nestable>
|
|
<div
|
|
v-if="intLoading"
|
|
class="loading position-absolute top-0 start-0 d-flex justify-content-center align-items-center rounded-1 w-100 h-100"
|
|
>
|
|
<div
|
|
class="spinner-border text-secondary"
|
|
role="status"
|
|
>
|
|
<span class="visually-hidden">
|
|
{{ __("form.image_input_viewer_loading") }}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script lang="ts">
|
|
import { defineComponent } from "vue";
|
|
import { VueNestable, VueNestableHandle } from "vue3-nestable";
|
|
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
|
|
import { Tooltip } from "bootstrap";
|
|
import route from "../../modules/route";
|
|
import trans from "../../modules/trans";
|
|
import sweetalertConfirm from "../../modules/sweetalertConfirm";
|
|
|
|
type DirectoryObject = {
|
|
id: number;
|
|
name: string;
|
|
directory_id: number | null;
|
|
text: string | null;
|
|
order: number;
|
|
needAuthorization: number;
|
|
created_at: string;
|
|
updated_at: string;
|
|
children: Array<DirectoryObject> | undefined;
|
|
is_system: boolean | null;
|
|
};
|
|
|
|
export default defineComponent({
|
|
components: {
|
|
VueNestable,
|
|
VueNestableHandle,
|
|
FontAwesomeIcon,
|
|
},
|
|
mixins: [route, trans, sweetalertConfirm],
|
|
data(): {
|
|
intCsrf: string | null | undefined;
|
|
intId: String;
|
|
intDirectories: DirectoryObject[];
|
|
intMessage: object | object[] | null;
|
|
intDisplay: boolean[];
|
|
intLoading: boolean;
|
|
intTooltipList: HTMLButtonElement[];
|
|
} {
|
|
return {
|
|
intCsrf: document
|
|
.querySelector("meta[name=\"csrf-token\"]")
|
|
?.getAttribute("content"),
|
|
intId: "",
|
|
intDirectories: [],
|
|
intMessage: null,
|
|
intDisplay: [],
|
|
intLoading: false,
|
|
intTooltipList: [],
|
|
};
|
|
},
|
|
mounted() {
|
|
const json = String(this.$attrs.json ?? "{}");
|
|
const data = JSON.parse(json);
|
|
this.intId = data.id;
|
|
if (data.directories) {
|
|
this.intDirectories = this.recursiveDir(data.directories);
|
|
}
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
},
|
|
methods: {
|
|
/**
|
|
* Prepare the list of items.
|
|
* @param list
|
|
* @param parent
|
|
*/
|
|
recursiveDir(list: DirectoryObject[], parent: number | null = null) {
|
|
let root = list.filter((e: DirectoryObject) => e.directory_id == parent);
|
|
for (let i = 0; i < root.length; i++) {
|
|
let childs = this.recursiveDir(list, root[i].id);
|
|
root[i].children = [];
|
|
for (let j = 0; j < childs.length; j++) {
|
|
root[i].children?.push(childs[j]);
|
|
}
|
|
if (!root[i].children?.length) {
|
|
delete root[i].children;
|
|
}
|
|
}
|
|
return root;
|
|
},
|
|
/**
|
|
* Check if item is a system item before the move.
|
|
* @param object
|
|
*/
|
|
beforeMove({
|
|
dragItem,
|
|
pathTo,
|
|
}: {
|
|
dragItem: DirectoryObject;
|
|
pathTo: number[];
|
|
}) {
|
|
if (dragItem.is_system) {
|
|
if (pathTo.length >= 2) return false;
|
|
else return true;
|
|
}
|
|
if (pathTo.length == 4) {
|
|
return false;
|
|
}
|
|
return true;
|
|
},
|
|
/**
|
|
* Update data's directories.
|
|
*/
|
|
updateOrderDirectories() {
|
|
let newOrder = this.assignOrder(this.intDirectories, null);
|
|
const route = this.route("bo.directories.save-order");
|
|
if (!route) {
|
|
throw new Error("Undefined route bo.directories.save-order");
|
|
}
|
|
window.axios.post(route, { directories: newOrder }).catch((error) => {
|
|
if (error.response) {
|
|
console.error(error.response.data);
|
|
} else if (error.request) {
|
|
console.error(error.request);
|
|
} else {
|
|
// Something happened in setting up the request that triggered an Error.
|
|
console.error("Error", error.message);
|
|
}
|
|
});
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
},
|
|
/**
|
|
* Assign the new order and parent id to each directory.
|
|
* @param children
|
|
* @param parentId
|
|
*/
|
|
assignOrder(children: DirectoryObject[], parentId: number | null) {
|
|
children.forEach((element, index) => {
|
|
element.order = index;
|
|
element.directory_id = parentId;
|
|
if (element.children && element.children.length) {
|
|
element.children = this.assignOrder(element.children, element.id);
|
|
} else {
|
|
element.children = [];
|
|
}
|
|
});
|
|
return children;
|
|
},
|
|
/**
|
|
* Return edit route for a directory.
|
|
* @param id
|
|
* @return string
|
|
*/
|
|
getEditDirectoryRoute(id: number): string {
|
|
const route = this.route("bo.directories.edit", {
|
|
ID: id,
|
|
});
|
|
if (!route) {
|
|
throw new Error("Undefined route bo.directories.edit");
|
|
}
|
|
return route;
|
|
},
|
|
/**
|
|
* Return edit route for a directory.
|
|
* @param id
|
|
* @return string
|
|
*/
|
|
getEditButtonRoute(id: number): string {
|
|
const route = this.route("bo.settings.edit", {
|
|
ID: id,
|
|
});
|
|
if (!route) {
|
|
throw new Error("Undefined route bo.settings.edit");
|
|
}
|
|
return route;
|
|
},
|
|
/**
|
|
* Show/Hide children.
|
|
* @param id
|
|
*/
|
|
displayList(e: Event, id: number) {
|
|
this.intDisplay[id] = this.intDisplay[id] ? false : true;
|
|
document
|
|
.querySelector(".nestable-item-" + id + " .nestable-list")
|
|
?.classList.toggle("visually-hidden");
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
},
|
|
/**
|
|
* Update authorization for a specific directory.
|
|
* @param LaravelModel model
|
|
*/
|
|
updateAuthorization(model: LaravelModel) {
|
|
this.intLoading = true;
|
|
const route = this.route("bo.directories.need-authorization", {
|
|
ID: model.id as number,
|
|
});
|
|
if (!route) {
|
|
throw new Error("Undefined route bo.directories.need-authorization");
|
|
}
|
|
window.axios
|
|
.post(route, { id: model.id })
|
|
.then((reponse) => {
|
|
this.intDirectories.forEach((directory, directoryindex) => {
|
|
if (directory.id === reponse.data.id) {
|
|
this.intDirectories[directoryindex] = reponse.data;
|
|
}
|
|
});
|
|
this.intLoading = false;
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
if (error.response) {
|
|
console.error(error.response.data);
|
|
} else if (error.request) {
|
|
console.error(error.request);
|
|
} else {
|
|
// Something happened in setting up the request that triggered an Error.
|
|
console.error("Error", error.message);
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* Update authorization for a specific directory.
|
|
* @param LaravelModel model
|
|
*/
|
|
deleteDirectory(e: Event, model: LaravelModel) {
|
|
const btnConfirmDelete = this.$refs.confirmDelete as HTMLButtonElement;
|
|
(async () => {
|
|
const promise = new Promise((resolve: (value: boolean) => void) => {
|
|
sweetalertConfirm.methods.confirm(
|
|
"Confirmez",
|
|
"Êtes vous sure ?",
|
|
btnConfirmDelete,
|
|
function (response) {
|
|
resolve(response.isConfirmed);
|
|
},
|
|
{ icon: "warning" }
|
|
);
|
|
return false;
|
|
});
|
|
if (await promise) {
|
|
this.intLoading = true;
|
|
const route = this.route("bo.directories.destroy", {
|
|
ID: model.id as number,
|
|
});
|
|
if (!route) {
|
|
throw new Error("Undefined route bo.directories.destroy");
|
|
}
|
|
window.axios
|
|
.post(route, { id: model.id, _method: "DELETE" })
|
|
.then((reponse) => {
|
|
if (reponse.data[0] !== undefined) {
|
|
this.intDirectories = this.recursiveDir(reponse.data);
|
|
} else {
|
|
this.intMessage = reponse.data;
|
|
setTimeout(() => {
|
|
this.intMessage = null;
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
}, 6000);
|
|
}
|
|
this.intLoading = false;
|
|
this.$nextTick(() => {
|
|
this.updateBootstrapTooltip();
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
if (error.response) {
|
|
console.error(error.response.data);
|
|
} else if (error.request) {
|
|
console.error(error.request);
|
|
} else {
|
|
// Something happened in setting up the request that triggered an Error.
|
|
console.error("Error", error.message);
|
|
}
|
|
});
|
|
}
|
|
})();
|
|
},
|
|
/**
|
|
* Update Bootstrap tooltips.
|
|
*/
|
|
updateBootstrapTooltip() {
|
|
let newTooltipList = [].slice.call(
|
|
document.querySelectorAll(
|
|
".directories-" + this.intId + " [data-bs=\"tooltip\"]"
|
|
)
|
|
) as HTMLButtonElement[];
|
|
let tmp = newTooltipList.filter((x) => !this.intTooltipList.includes(x));
|
|
tmp.map((tooltip) => {
|
|
return new Tooltip(tooltip);
|
|
});
|
|
this.closeBootstrapTooltip();
|
|
this.intTooltipList = newTooltipList;
|
|
},
|
|
/**
|
|
* Close all Bootstrap tooltips.
|
|
*/
|
|
closeBootstrapTooltip() {
|
|
this.intTooltipList.forEach((tooltip) => {
|
|
tooltip.blur();
|
|
Tooltip.getInstance(tooltip)?.hide();
|
|
});
|
|
},
|
|
},
|
|
});
|
|
</script>
|
|
<style lang="scss">
|
|
.loading {
|
|
background-color: rgba(0, 0, 0, 0.15);
|
|
z-index: 5;
|
|
}
|
|
|
|
.fade-enter-active,
|
|
.fade-leave-active {
|
|
transition: opacity 0.5s ease;
|
|
}
|
|
|
|
.fade-enter-from,
|
|
.fade-leave-to {
|
|
opacity: 0;
|
|
}
|
|
</style>
|