1- import { Component , ComponentFactoryResolver , ElementRef , ViewChild , OnInit } from '@angular/core' ;
1+ import { Component , ElementRef , ViewChild , OnInit , OnDestroy , ComponentRef } from '@angular/core' ;
22
33import { BehaviorSubject , Observable } from 'rxjs' ;
44import { map , mergeMap , take , tap } from 'rxjs/operators' ;
@@ -35,6 +35,7 @@ import { SupervisionOrder } from '../../../../../core/supervision-order/models/s
3535import { PaginatedList } from '../../../../../core/data/paginated-list.model' ;
3636import { SupervisionOrderDataService } from '../../../../../core/supervision-order/supervision-order-data.service' ;
3737import { DSONameService } from '../../../../../core/breadcrumbs/dso-name.service' ;
38+ import { hasValue } from '../../../../../shared/empty.util' ;
3839
3940@listableObjectComponent ( WorkspaceItemSearchResult , ViewMode . GridElement , Context . AdminWorkflowSearch )
4041@Component ( {
@@ -45,7 +46,7 @@ import { DSONameService } from '../../../../../core/breadcrumbs/dso-name.service
4546/**
4647 * The component for displaying a grid element for an workflow item on the admin workflow search page
4748 */
48- export class WorkspaceItemSearchResultAdminWorkflowGridElementComponent extends SearchResultGridElementComponent < WorkspaceItemSearchResult , WorkspaceItem > implements OnInit {
49+ export class WorkspaceItemSearchResultAdminWorkflowGridElementComponent extends SearchResultGridElementComponent < WorkspaceItemSearchResult , WorkspaceItem > implements OnDestroy , OnInit {
4950
5051 /**
5152 * The item linked to the workspace item
@@ -77,9 +78,13 @@ export class WorkspaceItemSearchResultAdminWorkflowGridElementComponent extends
7778 */
7879 @ViewChild ( 'buttons' , { static : true } ) buttons : ElementRef ;
7980
81+ /**
82+ * The reference to the dynamic component
83+ */
84+ protected compRef : ComponentRef < Component > ;
85+
8086 constructor (
8187 public dsoNameService : DSONameService ,
82- private componentFactoryResolver : ComponentFactoryResolver ,
8388 private linkService : LinkService ,
8489 protected truncatableService : TruncatableService ,
8590 private themeService : ThemeService ,
@@ -98,24 +103,24 @@ export class WorkspaceItemSearchResultAdminWorkflowGridElementComponent extends
98103 this . dso = this . linkService . resolveLink ( this . dso , followLink ( 'item' ) ) ;
99104 this . item$ = ( this . dso . item as Observable < RemoteData < Item > > ) . pipe ( getAllSucceededRemoteData ( ) , getRemoteDataPayload ( ) ) ;
100105 this . item$ . pipe ( take ( 1 ) ) . subscribe ( ( item : Item ) => {
101- const componentFactory = this . componentFactoryResolver . resolveComponentFactory ( this . getComponent ( item ) ) ;
106+ const component : GenericConstructor < Component > = this . getComponent ( item ) ;
102107
103108 const viewContainerRef = this . dynamicComponentLoaderDirective . viewContainerRef ;
104109 viewContainerRef . clear ( ) ;
105110
106- const componentRef = viewContainerRef . createComponent (
107- componentFactory ,
108- 0 ,
109- undefined ,
110- [
111+ this . compRef = viewContainerRef . createComponent (
112+ component , {
113+ index : 0 ,
114+ projectableNodes : [
111115 [ this . badges . nativeElement ] ,
112116 [ this . buttons . nativeElement ]
113- ] ) ;
114- ( componentRef . instance as any ) . object = item ;
115- ( componentRef . instance as any ) . index = this . index ;
116- ( componentRef . instance as any ) . linkType = this . linkType ;
117- ( componentRef . instance as any ) . listID = this . listID ;
118- componentRef . changeDetectorRef . detectChanges ( ) ;
117+ ] ,
118+ } ) ;
119+ this . compRef . setInput ( 'object' , item ) ;
120+ this . compRef . setInput ( 'index' , this . index ) ;
121+ this . compRef . setInput ( 'linkType' , this . linkType ) ;
122+ this . compRef . setInput ( 'listID' , this . listID ) ;
123+ this . compRef . changeDetectorRef . detectChanges ( ) ;
119124 }
120125 ) ;
121126
@@ -128,6 +133,13 @@ export class WorkspaceItemSearchResultAdminWorkflowGridElementComponent extends
128133 } ) ;
129134 }
130135
136+ ngOnDestroy ( ) : void {
137+ if ( hasValue ( this . compRef ) ) {
138+ this . compRef . destroy ( ) ;
139+ this . compRef = undefined ;
140+ }
141+ }
142+
131143 /**
132144 * Fetch the component depending on the item's entity type, view mode and context
133145 * @returns {GenericConstructor<Component> }
0 commit comments