@@ -14,6 +14,8 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
1414 options : PluginOptions ;
1515 adminforth : IAdminForth ;
1616
17+ activationOrder : number = - 10000000 ;
18+
1719 constructor ( options : PluginOptions ) {
1820 super ( options , import . meta. url ) ;
1921 this . options = options ;
@@ -122,16 +124,15 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
122124 async modifyResourceConfig ( adminforth : IAdminForth , resourceConfig : AdminForthResource ) {
123125 super . modifyResourceConfig ( adminforth , resourceConfig ) ;
124126 this . adminforth = adminforth ;
125-
126- // get resource with foreignResourceId
127127 this . foreignResource = adminforth . config . resources . find ( ( resource ) => resource . resourceId === this . options . foreignResourceId ) ;
128+
128129 if ( ! this . foreignResource ) {
129130 const similar = suggestIfTypo ( adminforth . config . resources . map ( ( res ) => res . resourceId ) , this . options . foreignResourceId ) ;
130131 throw new Error ( `ForeignInlineListPlugin: Resource with ID "${ this . options . foreignResourceId } " not found. ${ similar ? `Did you mean "${ similar } "?` : '' } ` ) ;
131132 }
133+ const idOfNewCopy = `${ this . foreignResource . resourceId } _inline_list__from_${ this . resourceConfig . resourceId } __` ;
132134
133135
134- const defaultSort = this . foreignResource . options ?. defaultSort ;
135136 const newColumn = {
136137 name : `foreignInlineList_${ this . foreignResource . resourceId } ` ,
137138 label : 'Foreign Inline List' ,
@@ -151,15 +152,7 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
151152 ...this . options ,
152153 pluginInstanceId : this . pluginInstanceId ,
153154 disableForeignListResourceRefColumn : this . options . disableForeignListResourceRefColumn ,
154- ...( defaultSort
155- ? {
156- defaultSort : {
157- field : defaultSort . columnName ,
158- direction : defaultSort . direction ,
159- }
160- }
161- : { }
162- )
155+ foreignResourceId : idOfNewCopy
163156 }
164157 }
165158 } ,
@@ -202,5 +195,48 @@ export default class ForeignInlineListPlugin extends AdminForthPlugin {
202195 } else {
203196 resourceConfig . columns . push ( newColumn ) ;
204197 }
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+
205241 }
206242}
0 commit comments