diff --git a/src/common/shared_vuejs.ts b/src/common/shared_vuejs.ts index 0f483b48..a44937f5 100755 --- a/src/common/shared_vuejs.ts +++ b/src/common/shared_vuejs.ts @@ -2016,17 +2016,6 @@ export const shared_consts = { label: 'Sezione', icon: 'fas fa-th-large', }, - { - value: 1100, //ELEMTYPE.ROW, - label: 'Riga', - icon: 'fas fa-th-large', - }, - { - value: 1200, // ELEMTYPE.COLUMN, - label: 'Colonna', - icon: 'fas fa-th-large', - }, - { value: 20, label: 'Testo', diff --git a/src/components/CMyEditElem/CMyEditElem.ts b/src/components/CMyEditElem/CMyEditElem.ts index 16dbb255..b63c67a9 100755 --- a/src/components/CMyEditElem/CMyEditElem.ts +++ b/src/components/CMyEditElem/CMyEditElem.ts @@ -14,10 +14,11 @@ import type { } from '@src/model'; import { IImgGallery, ILabelValue, IMyPage, IOperators } from '@src/model'; import { useGlobalStore } from '@store/globalStore'; -import { useCatalogStore } from '@store/catalogStore'; +import { useCatalogStore } from '@store/CatalogStore'; import { CImgTitle } from '@src/components/CImgTitle'; import { CImgPoster } from '@src/components/CImgPoster'; +import { CMyElemAdd } from '@src/components/CMyElemAdd'; import { CTitle } from '@src/components/CTitle/index'; import { tools } from '@tools'; import { useRouter } from 'vue-router'; @@ -72,9 +73,19 @@ export default defineComponent({ CMySize, CBorders, CMyDimensioni, - CMyText, CMySlideNumber, + CMyText, + CMySlideNumber, + CMyElemAdd, }, - emits: ['saveElem', 'selElemClick', 'toggleSize', 'deleteElem', 'dupPage', 'expPage', 'impPage'], + emits: [ + 'saveElem', + 'selElemClick', + 'toggleSize', + 'deleteElem', + 'dupPage', + 'expPage', + 'impPage', + ], props: { myelem: { type: Object as PropType, @@ -178,38 +189,6 @@ export default defineComponent({ return neword; } - async function addNewElem(elemsel: any, direz: number) { - visuadd.value = false; - - let neword = 0; - - let recfound = null; - - if (direz === -1) { - // Sopra - // ottieni l'elemento precedente, mantenendo l'ordinamento - recfound = globalStore.getMyElemPrecThisElemId(props.idPage, elemsel._id); - } else if (direz === 1) { - // Sotto - // ottieni l'elemento precedente, mantenendo l'ordinamento - recfound = globalStore.getMyElemNextThisElemId(props.idPage, elemsel._id); - } - - if (recfound) { - // get the middle of the order number between thie 2 elements - neword = Math.round((recfound.order + elemsel.order) / 2); - } - - let myelem = props.myelem; - myelem.idPage = props.idPage; - - const newrec = await globalStore.prepareAddNewElem(neword, $q, t, myelem, newtype.value); - - emit('selElemClick', newrec); - - // emit('updateAll', newrec) - } - async function dupPage() { emit('dupPage', null); } @@ -228,7 +207,7 @@ export default defineComponent({ newrec.containerHtml = 'Copia di ' + newrec.containerHtml; } - const mynewelem = await globalStore.addNewElem($q, t, newrec); + const mynewelem = await globalStore.addNewElem($q, t, newrec, newrec); emit('selElemClick', mynewelem); } @@ -291,12 +270,20 @@ export default defineComponent({ if (myel.value.catalogo && myel.value.catalogo?.arrSchede) { for (const recscheda of myel.value.catalogo!.arrSchede) { if (recscheda.scheda) { - recscheda.scheda.testo_right_attaccato = tools.resetIText(recscheda.scheda?.testo_right_attaccato); - recscheda.scheda.testo_right = tools.resetIText(recscheda.scheda?.testo_right); - recscheda.scheda.testo_bottom = tools.resetIText(recscheda.scheda?.testo_bottom); + recscheda.scheda.testo_right_attaccato = tools.resetIText( + recscheda.scheda?.testo_right_attaccato + ); + recscheda.scheda.testo_right = tools.resetIText( + recscheda.scheda?.testo_right + ); + recscheda.scheda.testo_bottom = tools.resetIText( + recscheda.scheda?.testo_bottom + ); if (!recscheda.scheda?.dimensioni.pagina.dimensioni) { - recscheda.scheda.dimensioni.pagina = tools.resetRecIPagina(recscheda.scheda?.dimensioni.pagina); + recscheda.scheda.dimensioni.pagina = tools.resetRecIPagina( + recscheda.scheda?.dimensioni.pagina + ); } if (!recscheda.scheda?.dimensioni?.pagina?.testo_up) { recscheda.scheda!.dimensioni.pagina.testo_up = tools.resetIText(null); @@ -342,30 +329,40 @@ export default defineComponent({ myel.value.catalogo.first_page = tools.resetRecIDimensioni(null); } - myel.value.catalogo.first_page = tools.resetRecIDimensioni(myel.value.catalogo.first_page); + myel.value.catalogo.first_page = tools.resetRecIDimensioni( + myel.value.catalogo.first_page + ); } if (myel.value.catalogo) { - if (!myel.value.catalogo.last_page) myel.value.catalogo.last_page = tools.resetRecIDimensioni(null); + if (!myel.value.catalogo.last_page) + myel.value.catalogo.last_page = tools.resetRecIDimensioni(null); - myel.value.catalogo.last_page = tools.resetRecIDimensioni(myel.value.catalogo.last_page); + myel.value.catalogo.last_page = tools.resetRecIDimensioni( + myel.value.catalogo.last_page + ); } if (myel.value.catalogo) { if (!myel.value.catalogo.areadistampa) { myel.value.catalogo.areadistampa = tools.resetRecIAreaDiStampa(null); - myel.value.catalogo.areadistampa.margini = { left: '0.59', top: '0.59', right: '0.59', bottom: '0.28' }; + myel.value.catalogo.areadistampa.margini = { + left: '0.59', + top: '0.59', + right: '0.59', + bottom: '0.28', + }; myel.value.catalogo.print_isTemplate = false; myel.value.catalogo.print_linkIdTemplate = ''; } - myel.value.catalogo.areadistampa = tools.resetRecIAreaDiStampa(myel.value.catalogo.areadistampa); + myel.value.catalogo.areadistampa = tools.resetRecIAreaDiStampa( + myel.value.catalogo.areadistampa + ); } if (myel.value.catalogo && !myel.value.catalogo.dimensioni_def) { myel.value.catalogo.dimensioni_def = { isTemplate: false, linkIdTemplate: '', - scalexscheda: 1, - scaleyscheda: 1, name: '', pagina: {}, }; @@ -390,7 +387,13 @@ export default defineComponent({ function addNewCard() { if (!myel.value.listcards) myel.value.listcards = []; - myel.value.listcards.push({ _id: objectId(), imagefile: '', alt: '', description: '', vers_img: 0 }); + myel.value.listcards.push({ + _id: objectId(), + imagefile: '', + alt: '', + description: '', + vers_img: 0, + }); modifElem(); } function addNewScheda() { @@ -420,7 +423,12 @@ export default defineComponent({ if (myschedatocopy) { const newscheda = myschedatocopy.scheda!.name; - const msg = 'Sostituisci questa scheda (' + schedadest.scheda?.name + ') con "' + newscheda + '" ?'; + const msg = + 'Sostituisci questa scheda (' + + schedadest.scheda?.name + + ') con "' + + newscheda + + '" ?'; $q.dialog({ message: msg, @@ -447,7 +455,9 @@ export default defineComponent({ function dupNewScheda(id: string) { if (!myel.value.catalogo!.arrSchede) return; - const myfindscheda = myel.value.catalogo!.arrSchede.find((scheda: ISchedaSingola) => scheda._id === id); + const myfindscheda = myel.value.catalogo!.arrSchede.find( + (scheda: ISchedaSingola) => scheda._id === id + ); if (myfindscheda) { const myscheda = tools.jsonCopy(myfindscheda); @@ -513,20 +523,25 @@ export default defineComponent({ } function showFit() { - if (props.myelem.type) return [shared_consts.ELEMTYPE.TEXT].includes(props.myelem.type); + if (props.myelem.type) + return [shared_consts.ELEMTYPE.TEXT].includes(props.myelem.type); else return false; } function delRecCard(id: string, myel: IMyElem) { // if (props.myelem.type === shared_consts.ELEMTYPE.CARD) { - if (myel.listcards) myel.listcards = myel.listcards.filter((card: IMyCard) => card._id !== id); + if (myel.listcards) + myel.listcards = myel.listcards.filter((card: IMyCard) => card._id !== id); } else if (props.myelem.type === shared_consts.ELEMTYPE.IMGPOSTER) { - if (myel.elemsText) myel.elemsText = myel.elemsText.filter((rec: IElemText) => rec._id !== id); + if (myel.elemsText) + myel.elemsText = myel.elemsText.filter((rec: IElemText) => rec._id !== id); } } function delRecScheda(id: string, myel: IMyElem) { - const myscheda = myel.catalogo!.arrSchede!.find((scheda: ISchedaSingola) => scheda._id === id); + const myscheda = myel.catalogo!.arrSchede!.find( + (scheda: ISchedaSingola) => scheda._id === id + ); if (myscheda) { $q.dialog({ @@ -541,7 +556,9 @@ export default defineComponent({ persistent: false, }).onOk(async () => { // - myel.catalogo!.arrSchede = myel.catalogo!.arrSchede!.filter((scheda: ISchedaSingola) => scheda._id !== id); + myel.catalogo!.arrSchede = myel.catalogo!.arrSchede!.filter( + (scheda: ISchedaSingola) => scheda._id !== id + ); modifElem(); }); @@ -553,7 +570,9 @@ export default defineComponent({ const iscatalogo = costanti.CATALOGO_FIELDS.includes(col.name); const isscheda = costanti.SCHEDA_FIELDS.includes(col.name); - const isIImg = costanti.IMG_FIELDS.includes(col.name) && col.fieldtype === costanti.FieldType.imagerec; + const isIImg = + costanti.IMG_FIELDS.includes(col.name) && + col.fieldtype === costanti.FieldType.imagerec; if (col.fieldtype === costanti.FieldType.imagerec) { if (iscatalogo) { @@ -569,15 +588,27 @@ export default defineComponent({ } else { myel.value[col.name] = newval.imagefile; } + if (newval.idElemParent) { + rec['idElemParent'] = newval.idElemParent; + } } else { if (iscatalogo) { myel.value.catalogo[col.name] = newval; - console.log('SALVATO IN', col.name, newval, 'RIS', myel.value.catalogo[col.name]); + console.log( + 'SALVATO IN', + col.name, + newval, + 'RIS', + myel.value.catalogo[col.name] + ); } else if (isscheda) { rec[col.name] = newval; } else { myel.value[col.name] = newval; } + /*if (newval.idElemParent) { + myel.value['idElemParent'] = newval.idElemParent; + }*/ } } @@ -711,7 +742,10 @@ export default defineComponent({ elemprec.order = oldorder; if (oldorder === elemprec.order!) { - const elemprec2 = globalStore.getMyElemPrecThisElemId(props.idPage, elemprec._id); + const elemprec2 = globalStore.getMyElemPrecThisElemId( + props.idPage, + elemprec._id + ); neworder = (elemprec2.order! + elemprec.order) / 2; elemprec.order = Math.round((oldorder + neworder) / 2); @@ -725,7 +759,10 @@ export default defineComponent({ elemnext.order = oldorder; if (oldorder === elemnext.order!) { - const elemnext2 = globalStore.getMyElemNextThisElemId(props.idPage, elemnext._id); + const elemnext2 = globalStore.getMyElemNextThisElemId( + props.idPage, + elemnext._id + ); neworder = (elemnext2.order! + elemnext.order) / 2; elemnext.order = Math.round((oldorder + neworder) / 2); @@ -770,6 +807,10 @@ export default defineComponent({ return myel.value.type === shared_consts.ELEMTYPE.CATALOGO; } + function AddedNewElem(newrec: any) { + emit('selElemClick', newrec); + } + onMounted(mounted); return { @@ -786,7 +827,6 @@ export default defineComponent({ disableSave, modifElem, delElem, - addNewElem, newtype, neworder, elemChanged, @@ -838,6 +878,7 @@ export default defineComponent({ catalogStore, naviga, isElementoSpecifico, + AddedNewElem, }; }, }); diff --git a/src/components/CMyEditElem/CMyEditElem.vue b/src/components/CMyEditElem/CMyEditElem.vue index 67d1fd19..2755f370 100755 --- a/src/components/CMyEditElem/CMyEditElem.vue +++ b/src/components/CMyEditElem/CMyEditElem.vue @@ -2705,115 +2705,19 @@ transition-show="slide-up" transition-hide="slide-down" > - - - Aggiungi Elemento: - - - - -
-
- - -
-
- - -
-
-
- -
-
- - -
-
-
- -
-
- - -
-
-
-
-
-
-
+ + diff --git a/src/components/CMyElem/CMyElem.ts b/src/components/CMyElem/CMyElem.ts index fcc7ae42..b093e766 100755 --- a/src/components/CMyElem/CMyElem.ts +++ b/src/components/CMyElem/CMyElem.ts @@ -241,6 +241,7 @@ export default defineComponent({ $q, t, props.myelem, + {idPage: props.idPage, path: props.path}, newtype.value ); } @@ -251,7 +252,7 @@ export default defineComponent({ newrec._id = undefined; newrec.order = order ? order : newrec.order! + 10; - globalStore.addNewElem($q, t, newrec); + globalStore.addNewElem($q, t, newrec, newrec); } function modifElem() { @@ -320,7 +321,7 @@ export default defineComponent({ } async function clickshare() { - tools.addToTemporaryLinkReg(); + tools.addToTemporaryLinkReg(t); const mytext = await tools.sendMsgTelegramCmd( $q, @@ -329,10 +330,6 @@ export default defineComponent({ true ); - if (false) { - social.value.description = mytext; - visushare.value = true; - } } // Classe per le colonne delle card diff --git a/src/components/CMyElem/CMyElem.vue b/src/components/CMyElem/CMyElem.vue index 2316a410..5e328da3 100755 --- a/src/components/CMyElem/CMyElem.vue +++ b/src/components/CMyElem/CMyElem.vue @@ -10,37 +10,6 @@ " >
-
- -
diff --git a/src/components/CMyValueDb/CMyValueDb.ts b/src/components/CMyValueDb/CMyValueDb.ts index 3318ebcd..4e96a703 100755 --- a/src/components/CMyValueDb/CMyValueDb.ts +++ b/src/components/CMyValueDb/CMyValueDb.ts @@ -174,7 +174,7 @@ export default defineComponent({ // console.log('showandsave CMyFieldDb', row, col, newval) if (newval !== valinitial) { - const ris = tools.saveInDBForTypes($q, props.mykey, newval, props.type, props.serv, props.table, props.mysubkey, props.id, props.indrec, props.mysubsubkey, props.specialField) + const ris = tools.saveInDBForTypes($q, props.mykey, newval, props.type, props.serv, props.table, props.mysubkey, props.id, props.indrec, props.mysubsubkey, props.specialField, row) save(newval) return ris diff --git a/src/mixins/mixin-base.ts b/src/mixins/mixin-base.ts index 15d6dff3..28e670f7 100755 --- a/src/mixins/mixin-base.ts +++ b/src/mixins/mixin-base.ts @@ -1,103 +1,159 @@ -import { toolsext } from '@src/store/Modules/toolsext' +import { toolsext } from '@src/store/Modules/toolsext'; -import { i18n } from 'src/boot/i18n' // Importa l'istanza di i18n configurata in Quasar +import { i18n } from 'src/boot/i18n'; // Importa l'istanza di i18n configurata in Quasar -import { useUserStore } from '@store/UserStore' -import { useGlobalStore } from '@store/globalStore' -import { useQuasar } from 'quasar' -import type { ICatalog, IDataPass, IProduct, IProductInfo, IRaccoltaCatalogo, ISpecialField, IVariazione } from '@model'; -import { tools } from '../store/Modules/tools' -import { costanti } from '@costanti' -import { fieldsTable } from '@store/Modules/fieldsTable' +import { useUserStore } from '@store/UserStore'; +import { useGlobalStore } from '@store/globalStore'; +import { useQuasar } from 'quasar'; +import type { + ICatalog, + IDataPass, + IProduct, + IProductInfo, + IRaccoltaCatalogo, + ISpecialField, + IVariazione, +} from '@model'; +import { tools } from '../store/Modules/tools'; +import { costanti } from '@costanti'; +import { fieldsTable } from '@store/Modules/fieldsTable'; -import { useProducts } from '@store/Products' -import { useCatalogStore } from '@store/CatalogStore' +import { useProducts } from '@store/Products'; +import { useCatalogStore } from '@store/CatalogStore'; const { t } = i18n.global; // You can declare a mixin as the same style as components. export default function () { function showNotif(msg: string) { - const $q = useQuasar() + const $q = useQuasar(); - - - tools.showNotif($q, t(msg)) + tools.showNotif($q, t(msg)); } - function db_fieldsTable() { - return fieldsTable + return fieldsTable; } - function getValDb(keystr: string, serv: boolean, def?: any, table?: string, subkey?: any, id?: any, idmain?: any, indrec?: number, subsubkey?: string, specialField?: ISpecialField) { + function getValDb( + keystr: string, + serv: boolean, + def?: any, + table?: string, + subkey?: any, + id?: any, + idmain?: any, + indrec?: number, + subsubkey?: string, + specialField?: ISpecialField + ) { // console.log('getValDb') - return toolsext.getValDb(keystr, serv, def, table, subkey, id, idmain, indrec, subsubkey, specialField) + return toolsext.getValDb( + keystr, + serv, + def, + table, + subkey, + id, + idmain, + indrec, + subsubkey, + specialField + ); } - - function getValDbLang(keystr: string, serv: boolean, def?: any, table?: string, subkey?: any, indrec?: number, subsubkey?: string) { - let ris = toolsext.getValDb(`${keystr}_${toolsext.getLocale()}`, serv, def, table, subkey, indrec, subsubkey) - if (ris === def) ris = toolsext.getValDb(`${keystr}_it`, serv, def, table, subkey) - return ris + function getValDbLang( + keystr: string, + serv: boolean, + def?: any, + table?: string, + subkey?: any, + indrec?: number, + subsubkey?: string + ) { + let ris = toolsext.getValDb( + `${keystr}_${toolsext.getLocale()}`, + serv, + def, + table, + subkey, + indrec, + subsubkey + ); + if (ris === def) ris = toolsext.getValDb(`${keystr}_it`, serv, def, table, subkey); + return ris; } - async function setValDb($q: any, key: string, value: any, type: any, serv: boolean, table?: string, subkey?: string, id?: any, indrec?: number, subsubkey?: string, specialField?: ISpecialField) { - const userStore = useUserStore() - const globalStore = useGlobalStore() + async function setValDb( + $q: any, + key: string, + value: any, + type: any, + serv: boolean, + table?: string, + subkey?: string, + id?: any, + indrec?: number, + subsubkey?: string, + specialField?: ISpecialField, + recordFull?: any + ) { + const userStore = useUserStore(); + const globalStore = useGlobalStore(); // console.log('setValDb', key, value, serv, table, subkey, indrec, subsubkey, specialField) - let mydatatosave: IDataPass | null = null + let mydatatosave: IDataPass | null = null; if (table === 'users') { - const myid = userStore.my._id + const myid = userStore.my._id; - const myfield: any = {} + const myfield: any = {}; if (key === 'profile') { - if (subsubkey && !!indrec && indrec >= 0) { // @ts-ignore - userStore.my.profile[subkey][indrec][subsubkey] = value - + userStore.my.profile[subkey][indrec][subsubkey] = value; } else { - if (!!specialField && subkey) { // @ts-ignore - const myrec = userStore.my.profile[subkey].filter(specialField.findsubkey) - if (myrec && tools.isArray(myrec) && myrec.length > 0 && !!specialField.paramtosetsubkey) { - myrec[0][specialField.paramtosetsubkey] = value + const myrec = userStore.my.profile[subkey].filter(specialField.findsubkey); + if ( + myrec && + tools.isArray(myrec) && + myrec.length > 0 && + !!specialField.paramtosetsubkey + ) { + myrec[0][specialField.paramtosetsubkey] = value; // console.log('myrec[specialField.paramtosetsubkey]', myrec[0][specialField.paramtosetsubkey], myrec) // @ts-ignore } else { //let mynewrec = tools.getDefaultRecByTableAndSpecialField(table, specialField) // @ts-ignore - let mynewrec = specialField.defaultnewrec + let mynewrec = specialField.defaultnewrec; // @ts-ignore - mynewrec[specialField.paramtosetsubkey] = value + mynewrec[specialField.paramtosetsubkey] = value; // console.log('mynewrec', mynewrec) // @ts-ignore - userStore.my.profile[subkey].push(mynewrec) + userStore.my.profile[subkey].push(mynewrec); } // @ts-ignore // console.log('saved', userStore.my.profile[subkey]) } else { // @ts-ignore - userStore.my.profile[subkey] = value + userStore.my.profile[subkey] = value; } } // Save to the DB: // @ts-ignore - myfield[`${key}.${subkey}`] = userStore.my.profile[subkey] - + myfield[`${key}.${subkey}`] = userStore.my.profile[subkey]; } else { // Save to the DB: - myfield[key] = value + myfield[key] = value; // @ts-ignore - userStore.my[key] = value + userStore.my[key] = value; } // Save to the DB: @@ -108,15 +164,15 @@ export default function () { id: myid, table, fieldsvalue: myfield, - } + }; } else if (table === 'todos') { - const myfield: any = {} + const myfield: any = {}; // Save to the DB: if (subkey) { - myfield[`${key}.${subkey}`] = value + myfield[`${key}.${subkey}`] = value; } else { - myfield[key] = value + myfield[key] = value; } // console.log('myfield', myfield) @@ -125,36 +181,42 @@ export default function () { id, table, fieldsvalue: myfield, - } + }; } else if (table === 'settings') { - globalStore.setValueSettingsByKey({ key, value, serv }) + globalStore.setValueSettingsByKey({ key, value, serv }); - let myrec = globalStore.getrecSettingsByKey(key, serv) + let myrec = globalStore.getrecSettingsByKey(key, serv); // console.log('settings... myrec ', myrec, 'key=', key, 'serv', serv) if (myrec === undefined) { myrec = { idapp: tools.getEnv('VITE_APP_ID'), key, type, - } - myrec.serv = serv - if ((myrec.type === costanti.FieldType.date) || (myrec.type === costanti.FieldType.onlydate)) myrec.value_date = value - else if ((myrec.type === costanti.FieldType.number) || (myrec.type === costanti.FieldType.hours)) myrec.value_num = value - else if (myrec.type === costanti.FieldType.boolean) myrec.value_bool = value - else myrec.value_str = value - - myrec = await tools.createNewRecord($q, 'settings', myrec).then( - (myrecris) => { - // console.log('myrec') - let recsett = null - if (serv) recsett = globalStore.serv_settings - else recsett = globalStore.settings - - if (myrecris) recsett.push(myrecris) - // @ts-ignore - return recsett.find((rec) => rec.key === key) - }, + }; + myrec.serv = serv; + if ( + myrec.type === costanti.FieldType.date || + myrec.type === costanti.FieldType.onlydate ) + myrec.value_date = value; + else if ( + myrec.type === costanti.FieldType.number || + myrec.type === costanti.FieldType.hours + ) + myrec.value_num = value; + else if (myrec.type === costanti.FieldType.boolean) myrec.value_bool = value; + else myrec.value_str = value; + + myrec = await tools.createNewRecord($q, 'settings', myrec).then((myrecris) => { + // console.log('myrec') + let recsett = null; + if (serv) recsett = globalStore.serv_settings; + else recsett = globalStore.settings; + + if (myrecris) recsett.push(myrecris); + // @ts-ignore + return recsett.find((rec) => rec.key === key); + }); } // console.log('myrec', myrec) @@ -164,15 +226,19 @@ export default function () { table: 'settings', // @ts-ignore fieldsvalue: myrec, - } + }; } else { - const myfield: any = {} + const myfield: any = {}; // Save to the DB: if (subkey) { - myfield[`${key}.${subkey}`] = value + myfield[`${key}.${subkey}`] = value; } else { - myfield[key] = value + myfield[key] = value; + } + + if (table === 'myelems' && !!recordFull) { + myfield.idElemParent = recordFull.idElemParent; } // console.log('myfield', myfield) @@ -181,16 +247,16 @@ export default function () { id, table: table || '', fieldsvalue: myfield, - } + }; } if (table === 'products') { - const productStore = useProducts() - const idprod = productStore.products.findIndex((rec: IProduct) => rec._id === id) + const productStore = useProducts(); + const idprod = productStore.products.findIndex((rec: IProduct) => rec._id === id); if (idprod >= 0 && key) { - productStore.products[idprod][key as keyof IProduct] = value + productStore.products[idprod][key as keyof IProduct] = value; } - /*} else if (table === 'productinfos') { + /*} else if (table === 'productinfos') { const productStore = useProducts() const idprod = productStore.products.findIndex((rec: IProduct) => rec.productInfo._id === id) if (idprod >= 0 && key) { @@ -198,25 +264,27 @@ export default function () { productStore.products[idprod].productInfo[myfield] = value }*/ } else if (table === 'arrvariazioni') { - const productStore = useProducts() - const idprod = productStore.products.findIndex((rec: IProduct) => rec._id === id) + const productStore = useProducts(); + const idprod = productStore.products.findIndex((rec: IProduct) => rec._id === id); if (idprod >= 0 && key) { - const myfield = key as keyof IVariazione - productStore.products[idprod].arrvariazioni[0][myfield] = value + const myfield = key as keyof IVariazione; + productStore.products[idprod].arrvariazioni[0][myfield] = value; } } else if (table === 'catalogs') { - const catalogStore = useCatalogStore() - const idcat = catalogStore.catalogs.findIndex((rec: ICatalog) => rec._id === id) + const catalogStore = useCatalogStore(); + const idcat = catalogStore.catalogs.findIndex((rec: ICatalog) => rec._id === id); if (idcat >= 0 && key) { - const myfield = key as keyof ICatalog - catalogStore.catalogs[idcat][myfield] = value + const myfield = key as keyof ICatalog; + catalogStore.catalogs[idcat][myfield] = value; } } else if (table === 'raccoltacataloghis') { - const catalogStore = useCatalogStore() - const idcat = catalogStore.raccoltacataloghis.findIndex((rec: IRaccoltaCatalogo) => rec._id === id) + const catalogStore = useCatalogStore(); + const idcat = catalogStore.raccoltacataloghis.findIndex( + (rec: IRaccoltaCatalogo) => rec._id === id + ); if (idcat >= 0 && key) { - const myfield = key as keyof IRaccoltaCatalogo - catalogStore.raccoltacataloghis[idcat][myfield] = value + const myfield = key as keyof IRaccoltaCatalogo; + catalogStore.raccoltacataloghis[idcat][myfield] = value; } } @@ -225,26 +293,26 @@ export default function () { // @ts-ignore globalStore.saveFieldValue(mydatatosave).then((esito) => { if (esito) { - tools.showPositiveNotif($q, t('db.recupdated')) + tools.showPositiveNotif($q, t('db.recupdated')); } else { - tools.showNegativeNotif($q, t('db.recfailed')) + tools.showNegativeNotif($q, t('db.recfailed')); // Undo... } - }) + }); } function getarrValDb(keystr: string, serv: boolean) { - const globalStore = useGlobalStore() + const globalStore = useGlobalStore(); - const myval = globalStore.getValueSettingsByKey(keystr, serv) + const myval = globalStore.getValueSettingsByKey(keystr, serv); // console.log('myval', myval) try { if (myval) { - return JSON.parse(myval) + return JSON.parse(myval); } - return [] + return []; } catch (e) { - return [] + return []; } } @@ -255,5 +323,5 @@ export default function () { getValDbLang, setValDb, getarrValDb, - } + }; } diff --git a/src/model/GlobalStore.ts b/src/model/GlobalStore.ts index d1d93717..9b0fdbc4 100755 --- a/src/model/GlobalStore.ts +++ b/src/model/GlobalStore.ts @@ -148,6 +148,7 @@ export interface IMyElem { type?: number path?: string idPage?: string + idElemParent?: string img?: string container?: string container2?: string @@ -195,7 +196,9 @@ export interface IMyElem { titleBanner?: string classBanner?: string color?: string - children?: any[] + rows?: any[] + columns?: any[] + elems?: any[] } export interface IElemText { diff --git a/src/store/Modules/fieldsTable.ts b/src/store/Modules/fieldsTable.ts index 7a69ff7e..373616fe 100755 --- a/src/store/Modules/fieldsTable.ts +++ b/src/store/Modules/fieldsTable.ts @@ -870,6 +870,10 @@ export const colmyelems = [ label_trans: 'myelems.image', fieldtype: costanti.FieldType.imagerec, }), + AddCol({ + name: 'idElemParent', + label_trans: 'myelems.idElemParent', + }), AddCol(DeleteRec), AddCol(DuplicateRec), ]; diff --git a/src/store/Modules/tools.ts b/src/store/Modules/tools.ts index c6a4c16d..f4fae88b 100644 --- a/src/store/Modules/tools.ts +++ b/src/store/Modules/tools.ts @@ -9010,11 +9010,11 @@ export const tools = { ); } else return ''; } else if (elem.type === shared_consts.ELEMTYPE.IMAGE) { - return tools.getDirUpload() + 'pages/' + mypath + '/' + elem.container + addtourl; + return elem.container ? tools.getDirUpload() + 'pages/' + mypath + '/' + elem.container + addtourl : ''; } else if (elem.type === shared_consts.ELEMTYPE.QRCODE) { - return tools.getDirUpload() + 'pages/' + mypath + '/' + elem.image + addtourl; + return elem.image ? tools.getDirUpload() + 'pages/' + mypath + '/' + elem.image + addtourl : ''; } else { - return tools.getDirUpload() + 'pages/' + mypath + '/' + elem.image + addtourl; + return elem.image ? tools.getDirUpload() + 'pages/' + mypath + '/' + elem.image + addtourl : ''; } } }, @@ -10169,7 +10169,8 @@ export const tools = { id?: any, indrec?: number, mysubsubkey?: string, - specialField?: ISpecialField + specialField?: ISpecialField, + recordFull?: any ) { const { setValDb, getValDb } = MixinBase(); @@ -10189,7 +10190,8 @@ export const tools = { id, indrec, mysubsubkey, - specialField + specialField, + recordFull, ); myval = newval.vers_img; const mykey2 = 'vers_img'; @@ -10204,7 +10206,8 @@ export const tools = { id, indrec, mysubsubkey, - specialField + specialField, + recordFull, ); eseguito = true; diff --git a/src/store/globalStore.ts b/src/store/globalStore.ts index 73394be9..e04dba53 100644 --- a/src/store/globalStore.ts +++ b/src/store/globalStore.ts @@ -1392,7 +1392,7 @@ export const useGlobalStore = defineStore('GlobalStore', { // console.table(res) return res.data; }) - .catch((error) => { + .catch((error: Types.AxiosError) => { console.log('error loadPickup', error); userStore.setErrorCatch(error); return null; @@ -2747,52 +2747,240 @@ export const useGlobalStore = defineStore('GlobalStore', { ); }, - async saveMyElem($q: any, t: any, myelem: IMyElem) { - const mydata = { - table: 'myelems', - data: {}, - }; - console.log(' DA SALVARE', myelem); + // Tipi base (adatta se già definiti altrove) + // type ElemType = typeof shared_consts.ELEMTYPE[keyof typeof shared_consts.ELEMTYPE]; - mydata.data = myelem; + /* =========================== + * UTILITY / FINDERS + * =========================== */ - return await this.saveTable(mydata) - .then(async (newelem) => { - if (newelem) { - console.log('*** newelem', newelem); - - // Save in Memory - for (let i = 0; i < this.myelems.length; i++) { - if (this.myelems[i]._id === newelem._id) { - this.myelems[i] = newelem; - - console.log('SALVATO', this.myelems[i]); - break; - } - } - - // Aggiorna anche tutto il sito... - // await this.loadSite() - - tools.showPositiveNotif($q, t('db.recupdated')); - } else { - tools.showNegativeNotif($q, t('db.recfailed')); - } - return newelem; - }) - .catch((e) => { - tools.showNegativeNotif($q, t('db.recfailed')); - return false; - }); + // Trova una section top-level per _id + findSectionById(this: any, sectionId: string) { + return this.myelems.find((e: IMyElem) => e._id === sectionId); }, - async addNewElem($q: any, t: any, myelem: IMyElem) { - const newelem = await this.saveMyElem($q, t, myelem); - if (newelem) { - this.myelems.push(newelem); + // Trova { row, section } a partire dall'id della row + findRowWithSection(this: any, rowId: string): { row?: IMyElem; section?: IMyElem } { + for (const section of this.myelems as IMyElem[]) { + const row = (section.rows || []).find((r) => r._id === rowId); + if (row) return { row, section }; + } + return {}; + }, + + // Trova { column, row, section } a partire dall'id della column + findColumnWithParents( + this: any, + columnId: string + ): { column?: IMyElem; row?: IMyElem; section?: IMyElem } { + for (const section of this.myelems as IMyElem[]) { + for (const row of section.rows || []) { + const column = (row.columns || []).find((c) => c._id === columnId); + if (column) return { column, row, section }; + } + } + return {}; + }, + + // Piccolo helper per assicurare che la proprietà sia un array + ensureArray(obj: T, key: K) { + if (!Array.isArray(obj[key])) { + // @ts-ignore + obj[key] = []; + } + }, + + // Restituisce SEMPRE il contenitore top-level (section) da salvare. + // Se gli passi già una section, ritorna quella. + // Se gli passi row/column/elem, risale a partire dagli id. + resolveTopContainer(this: any, elem: IMyElem): IMyElem | null { + // 1) Se è già una section presente a top-level + if ( + elem && + (elem.rows || []).length >= 0 && + this.myelems.some((s: IMyElem) => s._id === elem._id) + ) { + return elem; } - return newelem; + // 2) Prova con gli id noti + // - se elem è una row + if (elem?._id) { + const { row, section } = this.findRowWithSection.call(this, elem._id); + if (row && section) return section; + } + // - se elem è una column + if (elem?._id) { + const { column, section } = this.findColumnWithParents.call(this, elem._id); + if (column && section) return section; + } + + // 3) Se ha idElemParent, risali finché non trovi la section top-level + let cursor: IMyElem | undefined = elem; + const byId = (id: string | undefined) => { + if (!id) return undefined; + // cerca a TUTTI i livelli + return ( + (this.myelems as IMyElem[]).find((e) => e._id === id) || + (this.myelems as IMyElem[]) + .flatMap((s) => s.rows || []) + .find((r) => r._id === id) || + (this.myelems as IMyElem[]) + .flatMap((s) => s.rows || []) + .flatMap((r) => r.columns || []) + .find((c) => c._id === id) || + (this.myelems as IMyElem[]) + .flatMap((s) => s.rows || []) + .flatMap((r) => r.columns || []) + .flatMap((c) => c.elems || []) + .find((el) => el._id === id) + ); + }; + + // risali la catena + while (cursor?.idElemParent) { + const parent = byId(cursor.idElemParent); + if (!parent) break; + // se il parent è una section top-level, fermati + if (this.myelems.some((s: IMyElem) => s._id === parent._id)) { + return parent; + } + cursor = parent; + } + + return null; + }, + + /* =========================== + * SALVATAGGIO + * =========================== */ + + async saveContainer(this: any, $q: any, t: any, container: IMyElem) { + const payload = { table: 'myelems', data: container }; + + try { + const saved = await this.saveTable(payload); + if (saved) { + // aggiorna memoria: replace o push + const idx = (this.myelems as IMyElem[]).findIndex((e) => e._id === saved._id); + if (idx >= 0) this.myelems[idx] = saved; + else this.myelems.push(saved); + + tools.showPositiveNotif($q, t('db.recupdated')); + return saved as IMyElem; + } else { + tools.showNegativeNotif($q, t('db.recfailed')); + return false; + } + } catch (e) { + tools.showNegativeNotif($q, t('db.recfailed')); + return false; + } + }, + + /* =========================== + * API PUBBLICHE + * =========================== */ + + async saveMyElem(this: any, $q: any, t: any, myelem: IMyElem) { + // Decide cosa salvare: + // - se è un semplice nuovo top-level, salva lui + // - altrimenti risali al contenitore top-level (section) + const container = this.resolveTopContainer.call(this, myelem) ?? myelem; + return await this.saveContainer.call(this, $q, t, container); + }, + + // Dispatcher snello (compatibile con la tua firma esistente) + async addNewElem( + this: any, + $q: any, + t: any, + myelemOrig: IMyElem, // "dove" sto aggiungendo + myelemDest: IMyElem // "cosa" sto aggiungendo + ) { + switch (true) { + case myelemOrig.type === shared_consts.ELEMTYPE.SECTION && + myelemDest && myelemDest.type === shared_consts.ELEMTYPE.ROW: + return await this.addRowToSection.call( + this, + $q, + t, + myelemOrig._id!, + myelemDest + ); + + case myelemOrig.type === shared_consts.ELEMTYPE.ROW && + myelemDest && myelemDest.type === shared_consts.ELEMTYPE.COLUMN: + return await this.addColumnToRow.call(this, $q, t, myelemOrig._id!, myelemDest); + + case myelemOrig.type === shared_consts.ELEMTYPE.COLUMN: + return await this.addElemToColumn.call( + this, + $q, + t, + myelemOrig._id!, + myelemDest + ); + + default: + // fallback: crea/salva un record "piatto" top-level + const saved = await this.saveMyElem.call(this, $q, t, myelemDest); + if (saved && !(this.myelems as IMyElem[]).some((e) => e._id === saved._id)) { + this.myelems.push(saved); + } + return saved; + } + }, + + /* =========================== + * HELPERS DI AGGIUNTA + * =========================== */ + + async addRowToSection( + this: any, + $q: any, + t: any, + sectionId: string, + newRow: IMyElem + ) { + const section = this.findSectionById.call(this, sectionId); + if (!section) return false; + + this.ensureArray(section, 'rows'); + newRow.idElemParent = section._id!; + (section.rows as IMyElem[]).push(newRow); + + return await this.saveMyElem.call(this, $q, t, section); + }, + + async addColumnToRow(this: any, $q: any, t: any, rowId: string, newCol: IMyElem) { + const { row, section } = this.findRowWithSection.call(this, rowId); + if (!row || !section) return false; + + this.ensureArray(row, 'columns'); + newCol.idElemParent = row._id!; + (row.columns as IMyElem[]).push(newCol); + + // Salva la SECTION (contenitore) + return await this.saveMyElem.call(this, $q, t, section); + }, + + async addElemToColumn( + this: any, + $q: any, + t: any, + columnId: string, + newElem: IMyElem + ) { + const { column, row, section } = this.findColumnWithParents.call(this, columnId); + if (!column || !row || !section) return false; + + this.ensureArray(column, 'elems'); + newElem.idElemParent = column._id!; + (column.elems as IMyElem[]).push(newElem); + + // Salva la SECTION (contenitore) + return await this.saveMyElem.call(this, $q, t, section); }, changeVisuDrawer(path: string, edit: boolean) { @@ -2854,17 +3042,25 @@ export const useGlobalStore = defineStore('GlobalStore', { }; }, - async prepareAddNewElem(order: any, $q: any, t: any, myelem: any, newtype: any) { + async prepareAddNewElem( + order: any, + $q: any, + t: any, + myelemOrig: any, + myelemDest: any, + newtype: any + ) { const newrec: IMyElem = { _id: undefined, type: newtype, - path: myelem.path, - idPage: myelem.idPage, + path: myelemDest.path, + idPage: myelemDest.idPage, order: order ? order : 1000, active: true, container: '', }; + // Gestisci altri tipi di elementi con configurazioni specifiche if (newrec.type === shared_consts.ELEMTYPE.CAROUSEL_IMGS) { newrec.container2 = '8'; newrec.height = 600; @@ -2876,9 +3072,16 @@ export const useGlobalStore = defineStore('GlobalStore', { newrec.catalogo = this.createCatalogoVuoto(); } else if (newrec.type === shared_consts.ELEMTYPE.RACCOLTA) { newrec.catalogo = this.createRaccoltaCataloghiVuoto(); + } else if (newrec.type === shared_consts.ELEMTYPE.TEXT) { + newrec.container = "Inserisci qui il testo" + } else if (newrec.type === shared_consts.ELEMTYPE.HTML) { + newrec.containerHtml = "Inserisci qui il testo" + } else if (newrec.type === shared_consts.ELEMTYPE.IMAGEUPLOAD) { + newrec.containerHtml } - const mynewrec = await this.addNewElem($q, t, newrec); + // Aggiungi il nuovo elemento alla struttura + const mynewrec = await this.addNewElem($q, t, myelemOrig, newrec); return mynewrec; },