1- import { Component , EventEmitter , Inject , Input , OnInit , Output } from '@angular/core' ;
2- import { Observable } from 'rxjs' ;
1+ import { Component , EventEmitter , Inject , Input , OnInit , Output , OnChanges , OnDestroy } from '@angular/core' ;
2+ import { Observable , Subscription , BehaviorSubject } from 'rxjs' ;
33import { Item } from '../../../core/shared/item.model' ;
44import { MetadataValue } from '../../../core/shared/metadata.models' ;
55import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service' ;
66import { APP_CONFIG , AppConfig } from '../../../../config/app-config.interface' ;
7+ import { hasValue } from '../../../shared/empty.util' ;
8+
9+ interface ItemDTO {
10+
11+ item : Item ;
12+
13+ isSelectedVirtualMetadataItem$ : Observable < boolean > ;
14+
15+ }
716
817@Component ( {
918 selector : 'ds-virtual-metadata' ,
@@ -14,7 +23,7 @@ import { APP_CONFIG, AppConfig } from '../../../../config/app-config.interface';
1423 * The component is shown when a relationship is marked to be deleted.
1524 * Each item has a checkbox to indicate whether its virtual metadata should be saved as real metadata.
1625 */
17- export class VirtualMetadataComponent implements OnInit {
26+ export class VirtualMetadataComponent implements OnInit , OnChanges , OnDestroy {
1827
1928 /**
2029 * The current url of this page
@@ -55,9 +64,9 @@ export class VirtualMetadataComponent implements OnInit {
5564 /**
5665 * Get an array of the left and the right item of the relationship to be deleted.
5766 */
58- get items ( ) {
59- return [ this . leftItem , this . rightItem ] ;
60- }
67+ itemDTOs$ : BehaviorSubject < ItemDTO [ ] > = new BehaviorSubject ( [ ] ) ;
68+
69+ subs : Subscription [ ] = [ ] ;
6170
6271 public virtualMetadata : Map < string , VirtualMetadata [ ] > = new Map < string , VirtualMetadata [ ] > ( ) ;
6372
@@ -109,14 +118,33 @@ export class VirtualMetadataComponent implements OnInit {
109118 /**
110119 * Prevent unnecessary rerendering so fields don't lose focus
111120 */
112- trackItem ( index , item : Item ) {
113- return item && item . uuid ;
121+ trackItemDTO ( index , itemDTO : ItemDTO ) : string {
122+ return itemDTO ?. item ? .uuid ;
114123 }
115124
116125 ngOnInit ( ) : void {
117- this . items . forEach ( ( item ) => {
118- this . virtualMetadata . set ( item . uuid , this . getVirtualMetadata ( item ) ) ;
119- } ) ;
126+ this . subs . push ( this . itemDTOs$ . subscribe ( ( itemDTOs : ItemDTO [ ] ) => {
127+ itemDTOs . forEach ( ( itemDTO : ItemDTO ) => this . virtualMetadata . set ( itemDTO . item . uuid , this . getVirtualMetadata ( itemDTO . item ) ) ) ;
128+ } ) ) ;
129+ }
130+
131+ ngOnChanges ( ) : void {
132+ if ( hasValue ( this . leftItem ) && hasValue ( this . rightItem ) ) {
133+ this . itemDTOs$ . next ( [
134+ {
135+ item : this . leftItem ,
136+ isSelectedVirtualMetadataItem$ : this . isSelectedVirtualMetadataItem ( this . leftItem ) ,
137+ } ,
138+ {
139+ item : this . rightItem ,
140+ isSelectedVirtualMetadataItem$ : this . isSelectedVirtualMetadataItem ( this . rightItem ) ,
141+ } ,
142+ ] ) ;
143+ }
144+ }
145+
146+ ngOnDestroy ( ) : void {
147+ this . subs . forEach ( ( sub : Subscription ) => sub . unsubscribe ( ) ) ;
120148 }
121149}
122150
0 commit comments