@@ -90,28 +90,6 @@ export default class UploadPlugin extends AdminForthPlugin {
9090 // define components which will be imported from other components
9191 this . componentPath ( 'imageGenerator.vue' ) ;
9292
93- const virtualColumn : AdminForthResourceColumn = {
94- virtual : true ,
95- name : `uploader_${ this . pluginInstanceId } ` ,
96- components : {
97- edit : {
98- file : this . componentPath ( 'uploader.vue' ) ,
99- meta : pluginFrontendOptions ,
100- } ,
101- create : {
102- file : this . componentPath ( 'uploader.vue' ) ,
103- meta : pluginFrontendOptions ,
104- } ,
105- } ,
106- showIn : {
107- create : true ,
108- edit : true ,
109- list : false ,
110- show : false ,
111- filter : false ,
112- } ,
113- } ;
114-
11593 if ( ! resourceConfig . columns [ pathColumnIndex ] . components ) {
11694 resourceConfig . columns [ pathColumnIndex ] . components = { } ;
11795 }
@@ -130,38 +108,18 @@ export default class UploadPlugin extends AdminForthPlugin {
130108 } ;
131109 }
132110
133- // insert virtual column after path column if it is not already there
134- const virtualColumnIndex = resourceConfig . columns . findIndex ( ( column : any ) => column . name === virtualColumn . name ) ;
135- if ( virtualColumnIndex === - 1 ) {
136- resourceConfig . columns . splice ( pathColumnIndex + 1 , 0 , virtualColumn ) ;
137- }
138-
139-
140- // if showIn of path column has 'create' or 'edit' remove it but use it for virtual column
141- if ( pathColumn . showIn && ( pathColumn . showIn . create !== undefined ) ) {
142- virtualColumn . showIn = { ...virtualColumn . showIn , create : pathColumn . showIn . create } ;
143- pathColumn . showIn = { ...pathColumn . showIn , create : false } ;
111+ resourceConfig . columns [ pathColumnIndex ] . components . create = {
112+ file : this . componentPath ( 'uploader.vue' ) ,
113+ meta : pluginFrontendOptions ,
144114 }
145115
146- if ( pathColumn . showIn && ( pathColumn . showIn . edit !== undefined ) ) {
147- virtualColumn . showIn = { ... virtualColumn . showIn , edit : pathColumn . showIn . edit } ;
148- pathColumn . showIn = { ... pathColumn . showIn , edit : false } ;
116+ resourceConfig . columns [ pathColumnIndex ] . components . edit = {
117+ file : this . componentPath ( 'uploader.vue' ) ,
118+ meta : pluginFrontendOptions ,
149119 }
150120
151- virtualColumn . required = pathColumn . required ;
152- virtualColumn . label = pathColumn . label ;
153- virtualColumn . editingNote = pathColumn . editingNote ;
154-
155121 // ** HOOKS FOR CREATE **//
156122
157- // add beforeSave hook to save virtual column to path column
158- resourceConfig . hooks . create . beforeSave . push ( async ( { record } : { record : any } ) => {
159- if ( record [ virtualColumn . name ] ) {
160- record [ pathColumnName ] = record [ virtualColumn . name ] ;
161- delete record [ virtualColumn . name ] ;
162- }
163- return { ok : true } ;
164- } ) ;
165123
166124 // in afterSave hook, aremove tag adminforth-not-yet-used from the file
167125 resourceConfig . hooks . create . afterSave . push ( async ( { record } : { record : any } ) => {
@@ -225,20 +183,12 @@ export default class UploadPlugin extends AdminForthPlugin {
225183
226184 // ** HOOKS FOR EDIT **//
227185
228- // beforeSave
229- resourceConfig . hooks . edit . beforeSave . push ( async ( { record } : { record : any } ) => {
230- // null is when value is removed
231- if ( record [ virtualColumn . name ] || record [ virtualColumn . name ] === null ) {
232- record [ pathColumnName ] = record [ virtualColumn . name ] ;
233- }
234- return { ok : true } ;
235- } )
236186
237187
238188 // add edit postSave hook to delete old file and remove tag from new file
239189 resourceConfig . hooks . edit . afterSave . push ( async ( { updates, oldRecord } : { updates : any , oldRecord : any } ) => {
240190
241- if ( updates [ virtualColumn . name ] || updates [ virtualColumn . name ] === null ) {
191+ if ( updates [ pathColumnName ] || updates [ pathColumnName ] === null ) {
242192 if ( oldRecord [ pathColumnName ] ) {
243193 // put tag to delete old file
244194 try {
@@ -248,7 +198,7 @@ export default class UploadPlugin extends AdminForthPlugin {
248198 console . error ( `Error setting tag ${ ADMINFORTH_NOT_YET_USED_TAG } to true for object ${ oldRecord [ pathColumnName ] } . File will not be auto-cleaned up` , e ) ;
249199 }
250200 }
251- if ( updates [ virtualColumn . name ] !== null ) {
201+ if ( updates [ pathColumnName ] !== null ) {
252202 // remove tag from new file
253203 // in this case we let it crash if it fails: this is a new file which just was uploaded.
254204 await this . options . storageAdapter . markKeyForNotDeletation ( updates [ pathColumnName ] ) ;
0 commit comments