@@ -14,8 +14,6 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
1414 options : PluginOptions ;
1515 adminforth : IAdminForth ;
1616
17- activationOrder : number = - 10000000 ;
18-
1917 constructor ( options : PluginOptions ) {
2018 super ( options , import . meta. url ) ;
2119 this . options = options ;
@@ -124,15 +122,16 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
124122 async modifyResourceConfig ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
125123 super . modifyResourceConfig ( adminforth , resourceConfig ) ;
126124 this . adminforth = adminforth ;
127- this . foreignResource = adminforth . config . resources . find ( ( resource ) => resource . resourceId === this . options . foreignResourceId ) ;
128125
126+ // get resource with foreignResourceId
127+ this . foreignResource = adminforth . config . resources . find ( ( resource ) => resource . resourceId === this . options . foreignResourceId ) ;
129128 if ( ! this . foreignResource ) {
130129 const similar = suggestIfTypo ( adminforth . config . resources . map ( ( res ) => res . resourceId ) , this . options . foreignResourceId ) ;
131130 throw new Error ( `ForeignInlineListPlugin: Resource with ID "${ this . options . foreignResourceId } " not found. ${ similar ? `Did you mean "${ similar } "?` : '' } ` ) ;
132131 }
133- const idOfNewCopy = `${ this . foreignResource . resourceId } _inline_list__from_${ this . resourceConfig . resourceId } __` ;
134132
135133
134+ const defaultSort = this . foreignResource . options ?. defaultSort ;
136135 const newColumn = {
137136 name : `foreignInlineList_${ this . foreignResource . resourceId } ` ,
138137 label : 'Foreign Inline List' ,
@@ -152,7 +151,15 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
152151 ...this . options ,
153152 pluginInstanceId : this . pluginInstanceId ,
154153 disableForeignListResourceRefColumn : this . options . disableForeignListResourceRefColumn ,
155- foreignResourceId : idOfNewCopy
154+ ...( defaultSort
155+ ? {
156+ defaultSort : {
157+ field : defaultSort . columnName ,
158+ direction : defaultSort . direction ,
159+ }
160+ }
161+ : { }
162+ )
156163 }
157164 }
158165 } ,
@@ -195,48 +202,5 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
195202 } else {
196203 resourceConfig . columns . push ( newColumn ) ;
197204 }
198-
199- // get resource with foreignResourceId
200- this . copyOfForeignResource = clone ( { ...this . foreignResource , plugins : [ ] } ) ;
201-
202- // if we install on plugin which is already a copy, adjust foreignResource references
203- if ( this . resourceConfig . resourceId . includes ( '_inline_list__from_' ) ) {
204- const originalResourceIdPart = this . resourceConfig . resourceId . split ( '_inline_list__from_' ) [ 0 ] ;
205- // find column in copied resource which is foreignResource.resourceId equal to originalResourceIdPart
206- // and change it to point to this.resourceConfig.resourceId
207- const foreignRefColumn = this . copyOfForeignResource . columns . find ( col => col . foreignResource ?. resourceId === originalResourceIdPart ) ;
208- if ( foreignRefColumn ) {
209- foreignRefColumn . foreignResource . resourceId = this . resourceConfig . resourceId ;
210- }
211- }
212-
213- // if foreignInlineList_ column already created, remove it
214- this . copyOfForeignResource . columns = this . copyOfForeignResource . columns . filter ( col => ! col . name . startsWith ( 'foreignInlineList_' ) ) ;
215- // we should not cate about modifications made by other plugins, while activationOrder of this plugin is very low (negative)
216-
217- this . copyOfForeignResource . resourceId = idOfNewCopy ;
218- adminforth . config . resources . push ( this . copyOfForeignResource ) ;
219-
220- if ( this . options . modifyTableResourceConfig ) {
221- this . options . modifyTableResourceConfig ( this . copyOfForeignResource ) ;
222- }
223-
224- // now we need to create a copy of all plugins of foreignResource,
225- for ( const plugin of this . foreignResource . plugins || [ ] ) {
226- const options = plugin . pluginOptions ;
227- // call constructor
228- const pluginCopy = new ( plugin . constructor as any ) ( options ) ;
229- this . copyOfForeignResource . plugins . push ( pluginCopy ) ;
230- }
231-
232- // activate plugins for the copyOfForeignResource
233- for ( const plugin of this . copyOfForeignResource . plugins . sort ( ( a , b ) => a . activationOrder - b . activationOrder ) ) {
234- // if there already is a plugin with same instanceUniqueRepresentation, skip
235- if ( plugin . modifyResourceConfig ) {
236- await plugin . modifyResourceConfig ( adminforth , this . copyOfForeignResource ) ;
237- }
238- }
239-
240-
241205 }
242206}
0 commit comments