Files
myprojplanet_vite/src/components/CGallery/CGallery.ts

522 lines
13 KiB
TypeScript
Raw Normal View History

2025-03-01 14:14:43 +01:00
import type { PropType } from 'vue';
2025-08-12 19:43:36 +02:00
import { defineComponent, ref, watch, onMounted, computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useUserStore } from '@store/UserStore';
import { useQuasar } from 'quasar';
2025-03-01 14:14:43 +01:00
import type { IImgGallery } from 'model';
2025-08-12 19:43:36 +02:00
import { IGallery } from 'model';
import { CMyPage } from '@src/components/CMyPage';
import { tools } from '@tools';
import { useGlobalStore } from '@store/globalStore';
import { costanti } from '@costanti';
import { shared_consts } from 'app/src/common/shared_vuejs';
2025-08-12 19:43:36 +02:00
import { Api } from 'app/src/store/Api';
import axios from 'app/src/boot/axios';
import { toolsext } from 'app/src/store/Modules/toolsext';
2021-09-16 21:08:02 +02:00
export default defineComponent({
name: 'CGallery',
props: {
edit: {
type: Boolean,
required: true,
},
2021-12-11 22:12:44 +01:00
canModify: {
type: Boolean,
required: true,
},
isInModif: {
type: Boolean,
required: false,
default: false,
},
2021-11-04 16:02:14 +01:00
single: {
type: Boolean,
required: false,
default: false,
},
2021-10-28 00:37:48 +02:00
title: String,
directory: {
type: String,
2021-09-16 21:08:02 +02:00
required: true,
},
imagebak: {
type: String,
required: false,
default: '',
},
filetype: {
type: Number,
required: false,
default: shared_consts.FILETYPE.IMG,
},
quality: {
type: String,
required: false,
default: 'original',
},
resize: {
type: Boolean,
required: false,
default: false,
},
2025-08-12 19:43:36 +02:00
fieldtype: {
type: Number,
required: false,
default: 0,
},
2021-10-28 00:37:48 +02:00
imgGall: {
2021-12-03 22:47:53 +01:00
type: Object as PropType<IImgGallery[] | string | undefined | null>,
2021-09-16 21:08:02 +02:00
required: true,
},
},
2021-10-28 00:37:48 +02:00
emits: ['showandsave'],
2021-09-16 21:08:02 +02:00
components: { CMyPage },
setup(props, { emit }) {
2025-08-12 19:43:36 +02:00
const $q = useQuasar();
const { t } = useI18n();
const userStore = useUserStore();
const globalStore = useGlobalStore();
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
const displayGall = ref(false);
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
const gallerylist = ref(<IImgGallery[]>[]);
const maximizedToggle = ref(true);
2021-10-28 00:37:48 +02:00
2025-08-12 19:43:36 +02:00
const fullscreen = ref(false);
const fullscreensrc = ref('');
2021-12-11 22:12:44 +01:00
2025-08-12 19:43:36 +02:00
const upl = ref();
const uploadOptions = ref({
// qui definisci le opzioni da passare
quality: 'original', // esempio di opzione di qualità
2025-08-12 19:43:36 +02:00
resize: true, // opzione per abilitare il ridimensionamento
});
2021-10-28 00:37:48 +02:00
function isValid(myobj: any): boolean {
2025-08-12 19:43:36 +02:00
return myobj && typeof myobj !== 'string' && typeof myobj !== 'undefined';
2021-10-28 00:37:48 +02:00
}
2021-09-16 21:08:02 +02:00
const isListImgValid = computed(() => {
2025-08-12 19:43:36 +02:00
const arr = getlistimages();
if (arr && tools.isArray(arr)) {
2025-08-12 19:43:36 +02:00
return arr.length > 0;
} else {
2025-08-12 19:43:36 +02:00
return !!arr;
}
2025-08-12 19:43:36 +02:00
});
watch(
() => props.imgGall,
(newval, oldval) => {
if (isValid(props.imgGall)) {
// @ts-ignore
gallerylist.value = props.imgGall;
}
2021-10-01 19:42:21 +02:00
}
2025-08-12 19:43:36 +02:00
);
2021-09-16 21:08:02 +02:00
function created() {
2021-12-03 22:47:53 +01:00
// console.log('created cgallery')
2021-10-28 00:37:48 +02:00
if (isValid(props.imgGall)) {
// @ts-ignore
2025-08-12 19:43:36 +02:00
let myarr: any = props.imgGall;
gallerylist.value = [];
2022-02-03 00:33:05 +01:00
if (Array.isArray(myarr)) {
2021-11-22 18:28:45 +01:00
myarr.forEach((pic: any) => {
2025-08-12 19:43:36 +02:00
if (pic?.imagefile) {
gallerylist.value.push(pic);
} else {
gallerylist.value.push(pic);
2021-11-23 15:59:26 +01:00
}
2025-08-12 19:43:36 +02:00
});
2021-11-22 18:28:45 +01:00
}
2021-10-28 00:37:48 +02:00
} else {
2025-08-12 19:43:36 +02:00
gallerylist.value = [];
2021-10-28 00:37:48 +02:00
}
2021-12-11 00:25:35 +01:00
uploadOptions.value = {
quality: props.quality,
resize: props.resize,
2025-08-12 19:43:36 +02:00
};
2021-10-28 00:37:48 +02:00
}
function getnumimages() {
2025-08-12 19:43:36 +02:00
if (gallerylist.value) return gallerylist.value.length;
else return 0;
2021-09-16 21:08:02 +02:00
}
function getlistimages() {
2021-11-04 16:02:14 +01:00
if (gallerylist.value)
2021-11-23 15:59:26 +01:00
// return gallerylist.value.slice().sort((a: any, b: any) => a.order! - b.order!)
2024-02-03 23:05:06 +01:00
//return gallerylist.value.filter(filename => !filename)
2025-08-12 19:43:36 +02:00
return gallerylist.value;
else return null;
2021-09-16 21:08:02 +02:00
}
function onDragStart(e: any) {
2025-08-12 19:43:36 +02:00
console.log('onDragStart');
e.dataTransfer.setData('text', e.target.id);
e.dataTransfer.dropEffect = 'move';
2021-09-16 21:08:02 +02:00
}
function onDragEnter(e: any) {
2021-12-11 22:12:44 +01:00
if (props.canModify) {
// don't drop on other draggables
if (e.target.draggable !== true) {
2025-08-12 19:43:36 +02:00
e.target.classList.add('drag-enter');
2021-12-11 22:12:44 +01:00
}
2021-09-16 21:08:02 +02:00
}
}
function onDragLeave(e: any) {
2021-12-11 22:12:44 +01:00
if (props.canModify) {
2025-08-12 19:43:36 +02:00
e.target.classList.remove('drag-enter');
2021-12-11 22:12:44 +01:00
}
2021-09-16 21:08:02 +02:00
}
function onDragOver(e: any) {
2021-12-11 22:12:44 +01:00
if (props.canModify) {
2025-08-12 19:43:36 +02:00
e.preventDefault();
2021-12-11 22:12:44 +01:00
}
2021-09-16 21:08:02 +02:00
}
function onDrop(e: any) {
2021-12-11 22:12:44 +01:00
if (props.canModify) {
2025-08-12 19:43:36 +02:00
console.log('onDrop', e);
e.preventDefault();
2021-09-16 21:08:02 +02:00
2021-12-11 22:12:44 +01:00
// don't drop on other draggables
if (e.target.draggable === true) {
2025-08-12 19:43:36 +02:00
return;
2021-12-11 22:12:44 +01:00
}
2021-09-16 21:08:02 +02:00
2021-12-11 22:12:44 +01:00
if (gallerylist.value) {
2025-08-12 19:43:36 +02:00
const draggedId = e.dataTransfer.getData('text');
let dragout = '';
2021-12-11 22:12:44 +01:00
try {
2025-08-12 19:43:36 +02:00
dragout = e.target.parentNode.parentNode.parentNode.id;
2021-12-11 22:12:44 +01:00
} catch (err) {
2025-08-12 19:43:36 +02:00
dragout = '';
2021-12-11 22:12:44 +01:00
}
2025-08-12 19:43:36 +02:00
const draggedEl = document.getElementById(draggedId);
console.log('draggedId', draggedId, 'draggedEl', draggedEl);
console.log('dragout', dragout);
2021-12-11 22:12:44 +01:00
// check if original parent node
if (draggedEl) {
if (draggedEl.parentNode === e.target) {
2025-08-12 19:43:36 +02:00
e.target.classList.remove('drag-enter');
return;
2021-12-11 22:12:44 +01:00
}
2021-11-23 15:59:26 +01:00
}
2021-10-01 19:42:21 +02:00
2025-08-12 19:43:36 +02:00
const myindexIn = gallerylist.value.findIndex(
(rec: any) => rec._id === draggedId
);
const myrecIn: IImgGallery = gallerylist.value[myindexIn];
2021-10-01 19:42:21 +02:00
2025-08-12 19:43:36 +02:00
let myrecOut: IImgGallery;
const myindexout = gallerylist.value.findIndex(
(rec: any) => rec._id === dragout
);
myrecOut = gallerylist.value[myindexout];
2021-10-01 19:42:21 +02:00
2025-08-12 19:43:36 +02:00
if (myindexIn === myindexout) return;
2021-10-01 19:42:21 +02:00
2025-08-12 19:43:36 +02:00
tools.array_move(gallerylist.value, myindexIn, myindexout);
2021-10-01 19:42:21 +02:00
2021-12-11 22:12:44 +01:00
// make the exchange
// draggedEl.parentNode.removeChild(draggedEl)
// e.target.appendChild(draggedEl)
2025-08-12 19:43:36 +02:00
e.target.classList.remove('drag-enter');
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
save();
2021-12-11 22:12:44 +01:00
}
2021-09-16 21:08:02 +02:00
}
}
function getclass() {
2025-08-12 19:43:36 +02:00
return props.edit || displayGall.value
? props.isInModif
? 'my-card-gallery'
: 'my-card-gallery-noModif'
: 'my-card-gallery-view' + ' text-center';
2021-09-16 21:08:02 +02:00
}
function getclimg() {
2025-08-12 19:43:36 +02:00
let mycl = props.edit || displayGall.value ? 'myimg' : 'myimg-view';
if (props.canModify && props.edit) mycl = mycl + ' myimg-modify';
return mycl;
2021-09-16 21:08:02 +02:00
}
2021-11-23 15:59:26 +01:00
/*function getlastord() {
2021-10-28 00:37:48 +02:00
if (gallerylist.value) {
2021-10-01 19:42:21 +02:00
let myord = 0
2021-10-28 00:37:48 +02:00
for (const file of gallerylist.value) {
2021-10-01 19:42:21 +02:00
if (file.order! > myord)
myord = file.order!
}
return myord + 10
2021-09-16 21:08:02 +02:00
}
2021-11-23 15:59:26 +01:00
}*/
2021-09-16 21:08:02 +02:00
function uploaded(info: any) {
2025-08-12 19:43:36 +02:00
console.log('uploaded', info);
2025-08-12 19:43:36 +02:00
let vers_img = tools.getGenerateVersionImage();
2021-10-28 00:37:48 +02:00
if (gallerylist.value) {
2025-08-12 19:43:36 +02:00
console.log('vers_img', vers_img);
if (props.single && info.files) {
2025-08-12 19:43:36 +02:00
console.log('gallerylist.value[0]', info.files[0].name);
if (info.files[0].name.imagefile) {
2025-08-12 19:43:36 +02:00
gallerylist.value[0] = info.files[0].name;
} else {
2025-08-12 19:43:36 +02:00
gallerylist.value[0] = { imagefile: info.files[0].name, vers_img, fieldtype: props.fieldtype };
}
} else {
for (const file of info.files) {
if (file.name.imagefile) {
2025-08-12 19:43:36 +02:00
gallerylist.value.push(file.name);
} else {
2025-08-12 19:43:36 +02:00
gallerylist.value.push({ imagefile: file.name, vers_img, fieldtype: props.fieldtype });
}
}
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
save();
2021-11-22 18:28:45 +01:00
2025-08-12 19:43:36 +02:00
console.log('CGALLERY gallerylist', gallerylist.value);
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
}
2021-10-28 00:37:48 +02:00
function apri() {
2025-08-12 19:43:36 +02:00
displayGall.value = true;
2021-10-28 00:37:48 +02:00
}
2021-09-16 21:08:02 +02:00
function deleted(rec: any) {
2025-08-12 19:43:36 +02:00
console.log('deleted', rec.imagefile);
2021-09-16 21:08:02 +02:00
// console.table(mylistimages)
2021-10-28 00:37:48 +02:00
if (gallerylist.value) {
2025-08-12 19:43:36 +02:00
const index = gallerylist.value.findIndex(
(elem: any) => elem.imagefile === rec.imagefile
);
2021-10-01 19:42:21 +02:00
if (index > -1) {
2025-08-12 19:43:36 +02:00
gallerylist.value.splice(index, 1);
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
gallerylist.value = gallerylist.value.filter(
(elem: any) => typeof elem.imagefile === 'string' && elem.imagefile
);
2025-05-11 21:59:16 +02:00
2021-10-01 19:42:21 +02:00
// mylistimages = mylistimages.pop((elem) => elem.imagefile !== rec.imagefile)
2021-09-16 21:08:02 +02:00
2021-10-01 19:42:21 +02:00
// console.table(mylistimages)
2021-09-16 21:08:02 +02:00
2025-08-12 19:43:36 +02:00
console.log('single', props.single);
2021-11-04 16:02:14 +01:00
2025-08-12 19:43:36 +02:00
save();
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
}
function getfullname(rec: any) {
if (rec) {
2025-08-12 19:43:36 +02:00
return tools.getDirUpload() + props.directory + '/' + rec.imagefile;
} else {
2025-08-12 19:43:36 +02:00
return props.imagebak;
}
2021-09-16 21:08:02 +02:00
}
function copytoclipboard(rec: any) {
2025-08-12 19:43:36 +02:00
const filename = getfullname(rec);
tools.copyStringToClipboard($q, filename, true);
2021-09-16 21:08:02 +02:00
}
function deleteFile(rec: any) {
2025-08-12 19:43:36 +02:00
console.log('deleteFile....');
const filename = getfullname(rec);
const filenamerel = filename.replace(/^.*[\\\/]/, '');
2021-11-23 15:59:26 +01:00
$q.dialog({
message: 'Eliminare il file ' + filenamerel + '?',
html: true,
ok: {
label: 'Elimina',
push: true,
},
title: filenamerel,
cancel: true,
persistent: false,
}).onOk(async () => {
// Delete File on server:
2025-08-12 19:43:36 +02:00
const ris = await globalStore.DeleteFile({ filename });
// console.log('ris', ris)
2025-05-11 21:59:16 +02:00
//if (ris)
2025-08-12 19:43:36 +02:00
deleted(rec);
});
2021-09-16 21:08:02 +02:00
}
function save() {
2025-08-12 19:43:36 +02:00
console.log('CGallery save', gallerylist.value);
2021-11-04 16:02:14 +01:00
if (gallerylist.value.length > 0) {
if (!props.single) {
2025-08-12 19:43:36 +02:00
emit('showandsave', gallerylist.value);
2021-11-04 16:02:14 +01:00
} else {
2025-08-12 19:43:36 +02:00
emit('showandsave', gallerylist.value[0]);
2021-11-04 16:02:14 +01:00
}
2021-11-22 18:28:45 +01:00
} else {
2025-08-12 19:43:36 +02:00
emit('showandsave', !props.single ? [] : '');
2021-11-04 16:02:14 +01:00
}
}
function close() {
2025-08-12 19:43:36 +02:00
return '';
2021-09-16 21:08:02 +02:00
}
function getrealdirectory() {
2025-08-12 19:43:36 +02:00
if (props.directory == 'productinfos') return 'products';
else return props.directory;
}
2021-10-28 00:37:48 +02:00
function getParamDir() {
2025-08-12 19:43:36 +02:00
return tools.escapeslash(getrealdirectory());
2021-10-28 00:37:48 +02:00
}
function getUrl() {
2025-08-12 19:43:36 +02:00
const myurl = tools.geturlupload() + getParamDir();
console.log('myurl', myurl);
return myurl;
2021-09-16 21:08:02 +02:00
}
2021-12-11 22:12:44 +01:00
function ImgFullScreen(mygallery: IImgGallery) {
2025-08-12 19:43:36 +02:00
fullscreen.value = true;
fullscreensrc.value = getfullname(mygallery);
2021-12-11 22:12:44 +01:00
}
function onRejected(rejectedEntries: any) {
2022-01-23 23:25:19 +01:00
// Notify plugin needs to be installed
// https://quasar.dev/quasar-plugins/notify#Installation
2025-08-12 19:43:36 +02:00
console.log('rejectedEntries', rejectedEntries);
2022-01-23 23:25:19 +01:00
$q.notify({
type: 'negative',
2025-08-12 19:43:36 +02:00
message: "La Dimensione massima dell'immagine è di 2 MB",
});
2022-01-23 23:25:19 +01:00
}
function getFileTypeStr() {
2025-08-12 19:43:36 +02:00
let tipo = '';
if (props.filetype === shared_consts.FILETYPE.IMG) tipo = 'Immagine';
else if (props.filetype === shared_consts.FILETYPE.PDF) tipo = 'PDF';
2025-08-12 19:43:36 +02:00
return tipo;
}
function getAccept() {
2025-08-12 19:43:36 +02:00
let tipo = '';
if (props.filetype === shared_consts.FILETYPE.IMG) tipo = 'image/*';
else if (props.filetype === shared_consts.FILETYPE.PDF) tipo = 'application/pdf';
2025-08-12 19:43:36 +02:00
return tipo;
}
function isPDF() {
2025-08-12 19:43:36 +02:00
return props.filetype === shared_consts.FILETYPE.PDF;
}
function isIMG() {
2025-08-12 19:43:36 +02:00
return props.filetype === shared_consts.FILETYPE.IMG;
}
2025-08-12 19:43:36 +02:00
const uploadFactory = async (files: readonly File[]) => {
const userStore = useUserStore();
const url = getUrl();
const buildFormData = () => {
const fd = new FormData();
// "file" è il fieldName atteso dal backend (adegua se diverso)
files.forEach((f) => fd.append('file', f, f.name));
// opzionale: passaggio di options come nel tuo backend
fd.append('options', JSON.stringify({ quality: 'original' }));
return fd;
};
const sendOnce = async () => {
return Api.SendReq(url, 'POST', buildFormData());
};
try {
await sendOnce();
} catch (err: any) {
const status = err?.response?.status;
try {
// usa la tua logica centralizzata
Api.checkTokenScaduto(
status,
/*evitaloop*/ false,
url,
'POST',
null,
/*setAuthToken*/ true
);
if (ret !== null) {
// token aggiornato -> ritenta UNA volta
await sendOnce();
} else {
throw err;
}
} catch (err2: any) {
// se lhandler segnala re-login, mostra messaggio e rilancia
const mystatus = err2?.status || err2?.code;
if (mystatus === toolsext.ERR_RETRY_LOGIN) {
$q.notify({
type: 'warning',
message: 'Sessione scaduta. Effettua nuovamente il login.',
});
}
throw err2;
}
}
};
2025-08-12 19:43:36 +02:00
onMounted(created);
2021-09-16 21:08:02 +02:00
return {
getlistimages,
onDragStart,
onDragEnter,
onDragLeave,
onDragOver,
onDrop,
getclass,
getclimg,
copytoclipboard,
deleteFile,
2021-10-01 03:08:43 +02:00
tools,
uploaded,
2021-10-28 00:37:48 +02:00
gallerylist,
getnumimages,
apri,
displayGall,
save,
maximizedToggle,
getUrl,
2021-11-04 16:02:14 +01:00
close,
2021-12-11 22:12:44 +01:00
ImgFullScreen,
fullscreen,
fullscreensrc,
2022-01-23 23:25:19 +01:00
onRejected,
isListImgValid,
costanti,
getrealdirectory,
uploadOptions,
getFileTypeStr,
getAccept,
shared_consts,
isIMG,
isPDF,
2025-08-12 19:43:36 +02:00
upl,
uploadFactory,
};
},
});