ldc-boutique-cnc/resources/ts/components/ImagesInputComponent.vue
2022-11-08 15:41:10 +01:00

322 lines
8.1 KiB
Vue

<template>
<div
ref="imagesInput"
class="image container"
>
<div
v-if="!inTsingle"
class="row m-0 p-0 border"
>
<div class="col-1 border-end text-center">
#
</div>
<div class="col">
{{ __("Images") }}
</div>
</div>
<Draggable
v-if="!inTsingle"
v-model="picturesList"
tag="div"
item-key="id"
handle=".handle"
group="people"
>
<template
#item="{
element: item,
index: key,
}: {
element: LaravelModel,
index: number,
}"
>
<div class="row m-0 p-0 border">
<div
class="col-1 noselect cursor border-end m-0 pt-2 text-center handle"
>
{{ key + 1 }}
</div>
<div class="col m-0 p-1">
<div class="input-group">
<ImageInput
@click.stop
:id="(item.id as string)"
:name="`${inTname}[]`"
:value="decodeURI(item.text as string)"
:required="true"
:preview="inTpreview"
:width="inTwidth"
:height="inTheight"
:browse-event="inTbrowseEvent"
@browse-file="insertFile(key)"
/>
</div>
</div>
<div class="col-2 col-md-1 m-0 p-1">
<div class="input-group justify-content-end">
<button
class="btn btn-danger me-1"
@click.prevent="removeFile(key)"
data-bs-toggle="tooltip"
:title="__('Supprimer l\'image')"
>
<FontAwesomeIcon icon="fa-solid fa-trash-can" />
</button>
</div>
</div>
</div>
</template>
</Draggable>
<div
v-if="!inTsingle"
class="row m-0 p-0 border"
>
<div class="col text-end p-2">
<button
class="btn btn-small btn-primary"
@click.prevent="addFile"
data-bs-toggle="tooltip"
:title="__('Ajouter une image')"
>
{{ __("Ajouter") }}
</button>
</div>
</div>
<div v-if="inTsingle">
<ImageInput
@click.stop
:id="String(picturesList[0].id)"
:name="`${inTname}`"
:value="decodeURI(String(picturesList[0].text))"
:required="inTrequired"
:preview="inTpreview"
:width="inTwidth"
:height="inTheight"
:helper="inThelper"
:browse-event="inTbrowseEvent"
@browse-file="insertFile(0)"
/>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { Tooltip } from "bootstrap";
import Draggable from "vuedraggable";
import ImageInput from "./ImageInputComponent.vue";
import trans from "../modules/trans";
export default defineComponent({
mixins: [trans],
components: {
Draggable,
ImageInput,
FontAwesomeIcon,
},
mounted() {
const json = String(this.$attrs.json ?? "{}"),
data = JSON.parse(json);
this.inTwidth = data.width ? Number(data.width) : this.width;
this.inTheight = data.height ? Number(data.height) : this.height;
this.inTname = data.name ?? this.name;
this.inThelper = String(data.helper ?? this.helper);
if (data.preview !== undefined) {
this.inTpreview = data.preview ? true : false;
} else {
this.inTpreview = this.preview;
}
if (data.browseEvent !== undefined) {
this.inTbrowseEvent = data.browseEvent ? true : false;
} else {
this.inTbrowseEvent = this.browseEvent;
}
if (data.required !== undefined) {
this.inTrequired = data.required ? true : false;
} else {
this.inTrequired = this.required;
}
if (data.single !== undefined) {
this.inTsingle = data.single ? true : false;
} else {
this.inTsingle = this.single;
}
this.picturesList = this.parsePictureList(data.pictures ?? this.pictures);
window.addEventListener("message", this._onMessage, false);
this.refreshTooltips();
},
props: {
name: {
type: String,
default: String(Math.pow(10, 16) / Math.random()),
},
helper: {
type: String,
default: "",
},
width: {
type: Number,
default: 200,
},
height: {
type: Number,
default: 200,
},
browseEvent: {
type: Boolean,
default: false,
},
preview: {
type: Boolean,
default: false,
},
required: {
type: Boolean,
default: false,
},
pictures: {
type: Array,
default: () => [],
},
},
data(): {
inTsingle: boolean;
inTbrowseEvent: boolean;
inTname: string;
inThelper: string;
inTrequired: boolean;
inTpreview: boolean;
inTwidth: number;
inTheight: number;
picturesList: LaravelModelList;
currentIndex: number | null;
popup: Window | null;
tooltipTriggerList: Array<Tooltip>;
} {
return {
inTsingle: false,
inTbrowseEvent: false,
inTname: "",
inThelper: "",
inTrequired: true,
inTpreview: true,
inTwidth: 0,
inTheight: 0,
picturesList: [],
currentIndex: null,
popup: null,
tooltipTriggerList: [],
};
},
watch: {
picturesList() {
this.refreshTooltips();
},
},
methods: {
refreshTooltips() {
const imagesInput = this.$refs.imagesInput as HTMLDivElement | null;
this.$nextTick(() => {
if (imagesInput) {
this.tooltipTriggerList = [].slice
.call(imagesInput.querySelectorAll("[data-bs-toggle=\"tooltip\"]"))
.map((tooltipTriggerEl: HTMLElement) => {
return new Tooltip(tooltipTriggerEl, {
delay: { show: 1000, hide: 300 },
});
});
}
});
},
parsePictureList(picturesList: any) {
let out = [],
key = 0;
if (!(picturesList instanceof Array)) {
throw new Error("pictures as to be an array");
}
for (const picture of picturesList) {
if (!(typeof picture === "string")) {
throw new Error("pictures list has to contains strings");
}
out.push({
id: key++,
text: picture.replace(new RegExp("^[/]+"), ""),
});
}
return out;
},
_onMessage(event: MessageEvent) {
if (
window.location.origin
.toLowerCase()
.indexOf(event.origin.toLowerCase()) == 0
) {
if (event.data.sender == "boutiqueFilesBtn") {
const url = new URL(event.data.message).pathname.replace(
new RegExp("^[/]+"),
""
);
if (this.currentIndex !== null) {
this.picturesList[this.currentIndex] = {
id: this.currentIndex,
text: url,
};
}
if (this.popup) {
this.popup.close();
}
}
}
},
addFile() {
this.insertFile(this.picturesList.length);
},
insertFile(index: number) {
this.popup = window.open(
"/file-manager/fm-button?filesBtn=1",
"fm",
"width=1400,height=800"
);
this.currentIndex = index;
},
viewFile(index: number) {
window.open(`/${this.picturesList[index].text}`, "_blank");
},
removeFile(index: number) {
this.picturesList.splice(index, 1);
},
},
});
</script>
<style lang="scss">
.noselect {
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Old versions of Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently
supported by Chrome, Edge, Opera and Firefox */
}
.cursor {
cursor: grab;
}
.nestable-item,
.nestable-item-copy {
margin: 0 !important;
}
.nestable-list {
list-style: none !important;
padding: 0 !important;
}
.nestable-list-empty {
display: none !important;
}
</style>