-import { ref, onMounted, nextTick, Ref, h, computed } from 'vue'
+import { ref, onMounted, nextTick, Ref, h, computed, watch, reactive } from 'vue'
import { Carousel } from 'flowbite';
import { callAdminForthApi } from '@/utils';
import { useI18n } from 'vue-i18n';
@@ -158,6 +189,7 @@ const emit = defineEmits(['close', 'uploadImage']);
const props = defineProps(['meta', 'record']);
const images = ref([]);
const loading = ref(false);
+const attachmentFiles = ref([])
function minifyField(field: string): string {
if (field.length > 100) {
@@ -167,7 +199,7 @@ function minifyField(field: string): string {
}
const caurosel = ref(null);
-onMounted(() => {
+onMounted(async () => {
// Initialize carousel
const context = {
field: props.meta.pathColumnLabel,
@@ -203,7 +235,24 @@ onMounted(() => {
prompt.value = template.replace(regex, (_, field) => {
return context[field.trim()] || '';
});
+
+ const recordId = props.record[props.meta.recorPkFieldName];
+ if (!recordId) return;
+
+ try {
+ const resp = await callAdminForthApi({
+ path: `/plugin/${props.meta.pluginInstanceId}/get_attachment_files`,
+ method: 'POST',
+ body: { recordId },
+ });
+ if (resp?.files?.length) {
+ attachmentFiles.value = resp.files;
+ console.log('attachmentFiles', attachmentFiles.value);
+ }
+ } catch (err) {
+ console.error('Failed to fetch attachment files', err);
+ }
});
async function slide(direction: number) {
@@ -357,6 +406,27 @@ async function generateImages() {
loading.value = false;
}
+import mediumZoom from 'medium-zoom'
+const zoomedImage = ref(null)
+const zoomedImg = ref(null)
+
+function zoomImage(img) {
+ zoomedImage.value = img
+}
+function closeZoom() {
+ zoomedImage.value = null
+}
+
+watch(zoomedImage, async (val) => {
+ await nextTick()
+ if (val && zoomedImg.value) {
+ mediumZoom(zoomedImg.value, {
+ margin: 24,
+ background: 'rgba(0, 0, 0, 0.9)',
+ scrollOffset: 150
+ }).show()
+ }
+})
diff --git a/index.ts b/index.ts
index 3fb70f0..8a8e2f0 100644
--- a/index.ts
+++ b/index.ts
@@ -423,6 +423,30 @@ export default class UploadPlugin extends AdminForthPlugin {
return null
}
});
+
+ server.endpoint({
+ method: 'POST',
+ path: `/plugin/${this.pluginInstanceId}/get_attachment_files`,
+ handler: async ({ body, adminUser }) => {
+ const { recordId } = body;
+
+ if (!recordId) return { error: 'Missing recordId' };
+
+ const record = await this.adminforth.resource(this.resourceConfig.resourceId).get([
+ Filters.EQ(this.resourceConfig.columns.find((col: any) => col.primaryKey)?.name, recordId),
+ ]);
+
+ if (!record) return { error: 'Record not found' };
+
+ if (!this.options.generation.attachFiles) return { files: [] };
+
+ const files = await this.options.generation.attachFiles({ record, adminUser });
+
+ return {
+ files: Array.isArray(files) ? files : [files],
+ };
+ },
+ });
}