Files
newfreeplanet_OLD/src/components/CGallery/CGallery.ts

345 lines
8.3 KiB
TypeScript
Raw Normal View History

2021-10-28 00:37:48 +02:00
import { defineComponent, ref, PropType, watch, onMounted } from 'vue'
2021-09-16 21:08:02 +02:00
import { useI18n } from '@src/boot/i18n'
import { useUserStore } from '@store/UserStore'
import { useQuasar } from 'quasar'
import { IGallery, IImgGallery } from 'model'
import { CMyPage } from '@/components/CMyPage'
import { tools } from '@store/Modules/tools'
import { useGlobalStore } from '@store/globalStore'
export default defineComponent({
name: 'CGallery',
props: {
edit: {
type: Boolean,
required: true,
},
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,
},
2021-10-28 00:37:48 +02:00
imgGall: {
type: Object as PropType<IImgGallery | string | undefined>,
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 }) {
const $q = useQuasar()
const { t } = useI18n()
const userStore = useUserStore()
const globalStore = useGlobalStore()
2021-10-28 00:37:48 +02:00
const displayGall = ref(false)
2021-09-16 21:08:02 +02:00
2021-10-28 00:37:48 +02:00
const gallerylist = ref(<IImgGallery[]>[])
const maximizedToggle = ref(true)
function isValid(myobj: any): boolean {
return (myobj && typeof myobj !== 'string' && typeof myobj !== 'undefined')
}
2021-09-16 21:08:02 +02:00
2021-10-28 00:37:48 +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
}
2021-09-16 21:08:02 +02:00
})
function created() {
2021-10-28 00:37:48 +02:00
console.log('created cgallery')
if (isValid(props.imgGall)) {
// @ts-ignore
2021-11-22 18:28:45 +01:00
let myarr: any = props.imgGall
gallerylist.value = []
if (myarr) {
myarr.forEach((pic: any) => {
if (pic.imagefile)
gallerylist.value.push(pic)
})
}
2021-10-28 00:37:48 +02:00
} else {
gallerylist.value = [
2021-11-22 18:28:45 +01:00
]
2021-10-28 00:37:48 +02:00
}
}
function showandsave(value: any) {
console.log('EMIT: showandsave')
emit('showandsave', value)
}
function getnumimages() {
2021-11-04 16:02:14 +01:00
if (gallerylist.value)
2021-10-28 00:37:48 +02:00
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-10-28 00:37:48 +02:00
return gallerylist.value.slice().sort((a: any, b: any) => a.order! - b.order!)
2021-09-16 21:08:02 +02:00
else
return null
}
function onDragStart(e: any) {
console.log('onDragStart')
e.dataTransfer.setData('text', e.target.id)
e.dataTransfer.dropEffect = 'move'
}
function onDragEnter(e: any) {
// don't drop on other draggables
if (e.target.draggable !== true) {
e.target.classList.add('drag-enter')
}
}
function onDragLeave(e: any) {
e.target.classList.remove('drag-enter')
}
function onDragOver(e: any) {
e.preventDefault()
}
function onDrop(e: any) {
console.log('onDrop', e)
e.preventDefault()
// don't drop on other draggables
if (e.target.draggable === true) {
return
}
2021-10-28 00:37:48 +02:00
if (gallerylist.value) {
2021-10-01 19:42:21 +02:00
const draggedId = e.dataTransfer.getData('text')
let dragout = ''
try {
dragout = e.target.parentNode.parentNode.id
} catch (e) {
dragout = ''
}
const draggedEl = document.getElementById(draggedId)
console.log('draggedId', draggedId, 'draggedEl', draggedEl)
console.log('dragout', dragout)
// check if original parent node
if (draggedEl!.parentNode === e.target) {
e.target.classList.remove('drag-enter')
return
}
2021-10-28 00:37:48 +02:00
const myindex = gallerylist.value.findIndex((rec: any) => rec._id === draggedId)
const myrec: IImgGallery = gallerylist.value[myindex]
2021-10-01 19:42:21 +02:00
let myrecprec: IImgGallery
let myrecout: IImgGallery
2021-10-28 00:37:48 +02:00
const myindexout = gallerylist.value.findIndex((rec: any) => rec._id === dragout)
myrecout = gallerylist.value[myindexout]
2021-10-01 19:42:21 +02:00
let myindexprec = myindexout - 1
if (myindexprec < 0)
myindexprec = 0
if (myindex === myindexout)
return
2021-10-28 00:37:48 +02:00
myrecprec = gallerylist.value[myindexprec]
2021-10-01 19:42:21 +02:00
console.log('myrec', myrec, 'myrecprec', myrecout)
if (myrec && myrecout)
console.log('myrec', myrec, 'myrecprec', myrecout, 'ord1', myrec.order, 'myrecout', myrecout.order)
if (myrecout) {
let diff = 0
const ord2 = myrecprec.order
const ord1 = myrecout.order
diff = Math.round((ord1! - ord2!) / 2)
if (diff <= 0)
diff++
console.log('diff', diff)
let mynum = 0
mynum = myrecprec.order! + diff
console.log('mynum', mynum)
myrec.order = mynum
} else {
myrec.order = Math.round(myrec.order!) - 1
}
console.log('myrec.order', myrec.order)
// make the exchange
// draggedEl.parentNode.removeChild(draggedEl)
// e.target.appendChild(draggedEl)
2021-09-16 21:08:02 +02:00
e.target.classList.remove('drag-enter')
2021-11-04 16:02:14 +01:00
2021-10-01 19:42:21 +02:00
save()
2021-09-16 21:08:02 +02:00
}
}
function getclass() {
2021-10-28 00:37:48 +02:00
return (props.edit || displayGall.value) ? 'my-card-gallery' : 'my-card-gallery-view' + ' text-center'
2021-09-16 21:08:02 +02:00
}
function getclimg() {
2021-10-28 00:37:48 +02:00
return (props.edit || displayGall.value) ? 'myimg' : 'myimg-view'
2021-09-16 21:08:02 +02: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
}
}
function uploaded(info: any) {
2021-11-22 18:28:45 +01:00
console.log('uploaded', info)
2021-10-28 00:37:48 +02:00
if (gallerylist.value) {
2021-10-01 19:42:21 +02:00
for (const file of info.files) {
2021-10-28 00:37:48 +02:00
gallerylist.value.push({ imagefile: file.name, order: getlastord() })
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
2021-11-04 16:02:14 +01:00
if (!props.single)
save()
2021-11-22 18:28:45 +01:00
console.log('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() {
displayGall.value = true
}
2021-09-16 21:08:02 +02:00
function deleted(rec: any) {
2021-11-04 16:02:14 +01: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) {
const index = gallerylist.value.findIndex((elem: any) => elem.imagefile === rec.imagefile)
2021-10-01 19:42:21 +02:00
if (index > -1) {
2021-10-28 00:37:48 +02:00
gallerylist.value.splice(index, 1)
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +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
2021-11-04 16:02:14 +01:00
console.log('single', props.single)
if (!props.single) {
save()
}
2021-10-01 19:42:21 +02:00
}
2021-09-16 21:08:02 +02:00
}
function getfullname(rec: any) {
2021-10-28 00:37:48 +02:00
return 'upload/' + props.directory + '/' + rec.imagefile
2021-09-16 21:08:02 +02:00
}
function copytoclipboard(rec: any) {
const filename = getfullname(rec)
2021-10-02 02:03:10 +02:00
tools.copyStringToClipboard($q, filename, true)
2021-09-16 21:08:02 +02:00
}
async function deleteFile(rec: any)
{
2021-11-04 16:02:14 +01:00
console.log('deleteFile....')
2021-09-16 21:08:02 +02:00
const filename = getfullname(rec)
// Delete File on server:
const ris = await globalStore.DeleteFile({ filename })
2021-11-04 16:02:14 +01:00
console.log('ris', ris)
2021-09-16 21:08:02 +02:00
if (ris)
deleted(rec)
}
function save() {
2021-11-22 18:28:45 +01:00
console.log('CGallery save')
2021-11-04 16:02:14 +01:00
if (gallerylist.value.length > 0) {
if (!props.single) {
emit('showandsave', gallerylist.value)
} else {
emit('showandsave', gallerylist.value[0].imagefile)
}
2021-11-22 18:28:45 +01:00
} else {
emit('showandsave', '')
2021-11-04 16:02:14 +01:00
}
}
function close() {
2021-11-22 18:28:45 +01:00
return ''
2021-09-16 21:08:02 +02:00
}
2021-10-28 00:37:48 +02:00
function getsrcimg(gallerylistery: any) {
2021-09-16 21:08:02 +02:00
2021-11-22 18:28:45 +01:00
if (!gallerylistery.imagefile) {
return 'images/noimg.png';
}
2021-11-04 16:02:14 +01:00
if (gallerylistery) {
if (tools.getextfile(gallerylistery.imagefile) === 'pdf')
return 'images/images/pdf.jpg'
else
return 'upload/' + props.directory + '/' + gallerylistery.imagefile
} else {
return 'images/noimg.png';
}
2021-10-28 00:37:48 +02:00
}
function getParamDir() {
return tools.escapeslash(props.directory)
}
function getUrl() {
const myurl = tools.geturlupload() + getParamDir()
2021-11-04 16:02:14 +01:00
// console.log('myurl', myurl)
2021-10-28 00:37:48 +02:00
return myurl
2021-09-16 21:08:02 +02:00
}
2021-10-28 00:37:48 +02:00
onMounted(created)
2021-09-16 21:08:02 +02:00
return {
getlistimages,
onDragStart,
onDragEnter,
onDragLeave,
onDragOver,
onDrop,
getclass,
getclimg,
getlastord,
copytoclipboard,
deleteFile,
getsrcimg,
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-09-16 21:08:02 +02:00
}
}
})