ldc-com-app-bo/resources/ts/bo/components/CrudDocumentInputComponent.vue
Vincent BRIDIER 777d3e0e86 Multiple fix
2023-07-13 08:34:54 +00:00

409 lines
11 KiB
Vue

<template>
<div :class="`crud-document-input-${intId} row`">
<!-- FILE LANGUAGE -->
<div class="col-12 col-md-6 mb-3">
<label
for="selectLanguageDocument"
class="col-form-label"
>
<b>{{ __("Langue du document") }} *</b>
</label>
<select
id="selectLanguageDocument"
name="lang"
class="form-select"
aria-label="Select language for the document"
@click="languageDocument($event)"
role="button"
>
<option
v-for="(language, languageIndex) in intLanguages"
:key="languageIndex"
:value="language.value"
:selected="Number(intModel?.lang) === language.value ? true : false"
>
{{ language.label }}
</option>
</select>
<small class="form-text text-muted">
{{ __("Choisissez une langue parmis la liste.") }}
</small>
</div>
<!-- INPUT NAME -->
<div class="col-12 col-md-6 mb-3">
<label
for="documentTitle"
class="col-form-label"
>
<b>{{ __("Titre du document") }} *</b>
</label>
<WordCounter
id="documentTitle"
class="ck-word-count"
/>
<input
id="documentTitle"
type="text"
name="title"
class="form-control need-word-counter"
:placeholder="__('Un beau titre de document')"
:value="intModel?.title"
>
<small class="form-text text-muted">
{{ __("Doit posséder 3 caractères minimum, 255 maximum.") }}
</small>
</div>
<!-- SELECT DIRECTORY -->
<div
v-if="intIsDocFR"
class="col-12 col-md-6 mb-3"
>
<label
for="directoryDirectoryId"
class="col-form-label"
>
<b>{{ __("Dossier à associer") }} *</b>
</label>
<select
id="directoryDirectoryId"
class="form-select"
name="directory_id"
aria-describedby="directoryDirectoryIdHelp"
role="button"
>
<option
v-for="(directory, directoryIndex) in intDirectories"
:key="directoryIndex"
:value="directory.id"
:selected="intModel?.directory_id === directory.id ? true : false"
>
{{ directory.name }}
</option>
</select>
<small class="form-text text-muted">
{{ __("Choisissez un dossier à associer parmis la liste.") }}
</small>
</div>
<!-- SELECT FRENCH DOCUMENT -->
<div
v-else
class="col-12 col-md-6 mb-3"
>
<label
for="documentParentId"
class="col-form-label"
>
<b>{{ __("Document français à associer") }} *</b>
</label>
<select
v-if="intDocuments.length > 0"
id="documentParentId"
class="form-select"
name="document_id"
aria-describedby="documentParentIdHelp"
role="button"
>
<option
v-for="(document, documentIndex) in intDocuments"
:key="documentIndex"
:value="document.id"
:selected="intModel?.document_id === document.id ? true : false"
>
{{ document.title }}
</option>
</select>
<p
v-else
class="text-muted"
>
{{ __("Aucun documents français n'est disponible") }}
</p>
<small class="form-text text-muted">
{{ __("Choisissez un document français à associer parmis la liste.") }}
</small>
</div>
<!-- CHOOSE FILE -->
<div class="col-12 col-md-6 mb-3">
<label
for="documentInputFile"
class="col-form-label"
>
<b>{{ __("Document à ajouter") }} *</b>
</label>
<div :class="intIsUploading ? 'd-none' : ''">
<div class="input-group">
<button
class="btn btn-outline-secondary"
type="button"
data-bs="tooltip"
:title="__('Modifier le document source')"
>
<FontAwesomeIcon icon="fa-solid fa-folder-open" />
</button>
<input
type="text"
class="form-control"
role="button"
ref="inputFile"
:value="
intDocumentDatas.path
? intDocumentDatas.path
: 'Sélectionnez un fichier'
"
data-bs="tooltip"
:title="__('Modifier le document source')"
readonly
>
</div>
<div v-if="intMessage">
<p
class="m-0"
:class="intSuccess ? 'text-success' : 'text-danger'"
>
{{ intMessage }}
</p>
</div>
</div>
<div
class="progress"
:class="!intIsUploading ? 'd-none' : ''"
>
<div
class="progress-bar progress-bar-striped progress-bar-animated"
ref="progressBar"
role="progressbar"
aria-label="Progress bar"
aria-valuenow="0"
aria-valuemin="0"
aria-valuemax="100"
>
{{ intFilePercent }}%
</div>
</div>
<small class="form-text text-muted">
{{ __("Doit être au format .pdf ou .mp4.") }}
</small>
</div>
<input
class="d-none"
name="path"
:value="intDocumentDatas.path"
>
<input
class="d-none"
name="uuid"
:value="intDocumentDatas.uuid"
>
<input
class="d-none"
name="type"
:value="intDocumentDatas.type"
>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { Tooltip } from "bootstrap";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import WordCounter from "./WordCounterComponent.vue";
import route from "./../../modules/route";
import trans from "./../../modules/trans";
import Resumable from "resumablejs";
export default defineComponent({
name: "AddDocumentComponent",
mixins: [route, trans],
components: {
WordCounter,
FontAwesomeIcon,
},
data(): {
intId: string;
intMessage: string;
intSuccess: boolean;
intIsUploading: boolean;
intFilePercent: number | null;
intProgressbar: HTMLDivElement | null;
intLanguages: {
value: number;
label: string;
}[];
intDocuments: { id: number; title: string }[];
intDirectories: LaravelModel[];
intIsDocFR: boolean;
intModel: LaravelModel | null;
intR: Resumable | null;
intDocumentDatas: {
directory_id: number;
document_id: number | null;
uuid: string;
title: string;
type: string;
path: string;
lang: string;
published: boolean;
};
intCsrf: string;
intTooltipList: HTMLButtonElement[];
} {
return {
intId: "",
intR: null,
intMessage: "",
intSuccess: false,
intIsUploading: false,
intFilePercent: 0,
intProgressbar: null,
intDocuments: [],
intDirectories: [],
intLanguages: [],
intIsDocFR: true,
intModel: null,
intDocumentDatas: {
directory_id: 0,
document_id: null,
uuid: "",
title: "",
type: "",
path: "",
lang: "",
published: false,
},
intCsrf: "",
intTooltipList: [],
};
},
mounted() {
const json = String(this.$attrs.json ?? "{}"),
data = JSON.parse(json);
this.intId = data.id;
this.intLanguages = data.languages;
this.intDocuments = data.documents;
this.intDirectories = data.directories;
this.intModel = data.model;
if (this.intModel != null) {
this.intDocumentDatas.path = String(this.intModel.path);
this.intDocumentDatas.uuid = String(this.intModel.uuid);
this.intDocumentDatas.type = String(this.intModel.type);
this.intIsDocFR = this.intModel.lang === "1" ? true : false;
}
this.intCsrf = data.csrf;
this.$nextTick(() => {
this.initResumable();
this.updateBootstrapTooltip();
});
},
methods: {
languageDocument(e: Event) {
const target = e.target as HTMLSelectElement;
this.intIsDocFR =
Number(target.value) === this.intLanguages[0].value ? true : false;
},
initResumable() {
// Route.
const uploadFileRoute = route.methods.route("bo.documents.upload");
if (!uploadFileRoute) {
throw new Error("Undefined route bo.documents.upload");
}
this.intR = new Resumable({
chunkSize: 1024 * 1024, // 1MB
simultaneousUploads: 1,
maxFiles: 1,
testChunks: false,
target: uploadFileRoute,
query: {
_token: this.intCsrf,
},
});
this.intR.on("fileAdded", this.fileAdded);
this.intR.on("fileSuccess", this.fileSuccess);
this.intR.on("fileError", this.fileError);
this.intR.on("fileProgress", () => {
if (this.intR !== null) {
this.intFilePercent = Math.floor(this.intR.progress() * 100);
if (this.intProgressbar !== null)
this.intProgressbar.style.width = this.intFilePercent + "%";
}
});
this.intR.assignBrowse(this.$refs.inputFile as Element, false);
},
/**
*
* @param file The file would like to store.
*/
fileAdded(file: any) {
if (
file.file.type !== "application/pdf" &&
file.file.type !== "video/mp4"
) {
return !(this.intMessage = "Extension du fichier invalide.");
}
this.intIsUploading = true;
setTimeout(() => {
this.intProgressbar = this.$refs.progressBar as HTMLDivElement;
this.intProgressbar.style.width = this.intFilePercent + "%";
}, 10);
this.closeBootstrapTooltip();
if (this.intR !== null) this.intR.upload();
},
/**
* Run method if the file is stored.
* @param file The file stored.
* @param message Set a new successful message.
*/
fileSuccess(file: any, message: string) {
let result = JSON.parse(message);
this.intDocumentDatas.path = file.fileName;
this.intDocumentDatas.uuid = result.uid;
if (file.file.type === "application/pdf") {
this.intDocumentDatas.type = "pdf";
} else {
this.intDocumentDatas.type = "mp4";
}
this.intSuccess = true;
this.intMessage = "Fichier chargé.";
const intInputFileResult = this.$refs.progressBar as HTMLInputElement;
intInputFileResult.value =
this.intDocumentDatas.uuid + this.intDocumentDatas.type;
setTimeout(() => {
this.intIsUploading = false;
this.$nextTick(() => {
this.updateBootstrapTooltip();
});
}, 800);
},
/**
* Set a new error message.
*/
fileError() {
this.intMessage = "Une erreur est survenue, veuillez recharger la page.";
},
/**
* Update Bootstrap tooltips.
*/
updateBootstrapTooltip() {
let newTooltipList = [].slice.call(
document.querySelectorAll(
".crud-document-input-" + this.intId + " [data-bs=\"tooltip\"]"
)
) as HTMLButtonElement[];
let tmp = newTooltipList.filter((x) => !this.intTooltipList.includes(x));
tmp.map((tooltip) => {
return new Tooltip(tooltip);
});
this.intTooltipList = newTooltipList;
this.closeBootstrapTooltip();
},
/**
* Close all Bootstrap tooltips.
*/
closeBootstrapTooltip() {
this.intTooltipList.forEach((tooltip) => {
tooltip.blur();
Tooltip.getInstance(tooltip)?.hide();
});
},
},
});
</script>