@@ -8,11 +8,17 @@ import {
88 EventEmitter ,
99 Inject ,
1010 Input ,
11+ OnChanges ,
12+ OnDestroy ,
1113 OnInit ,
1214 Output ,
1315} from '@angular/core' ;
1416import { TranslateModule } from '@ngx-translate/core' ;
15- import { Observable } from 'rxjs' ;
17+ import {
18+ BehaviorSubject ,
19+ Observable ,
20+ Subscription ,
21+ } from 'rxjs' ;
1622
1723import {
1824 APP_CONFIG ,
@@ -21,9 +27,18 @@ import {
2127import { ObjectUpdatesService } from '../../../core/data/object-updates/object-updates.service' ;
2228import { Item } from '../../../core/shared/item.model' ;
2329import { MetadataValue } from '../../../core/shared/metadata.models' ;
30+ import { hasValue } from '../../../shared/empty.util' ;
2431import { ListableObjectComponentLoaderComponent } from '../../../shared/object-collection/shared/listable-object/listable-object-component-loader.component' ;
2532import { VarDirective } from '../../../shared/utils/var.directive' ;
2633
34+ interface ItemDTO {
35+
36+ item : Item ;
37+
38+ isSelectedVirtualMetadataItem$ : Observable < boolean > ;
39+
40+ }
41+
2742@Component ( {
2843 selector : 'ds-virtual-metadata' ,
2944 templateUrl : './virtual-metadata.component.html' ,
@@ -42,7 +57,7 @@ import { VarDirective } from '../../../shared/utils/var.directive';
4257 * The component is shown when a relationship is marked to be deleted.
4358 * Each item has a checkbox to indicate whether its virtual metadata should be saved as real metadata.
4459 */
45- export class VirtualMetadataComponent implements OnInit {
60+ export class VirtualMetadataComponent implements OnInit , OnChanges , OnDestroy {
4661
4762 /**
4863 * The current url of this page
@@ -83,9 +98,9 @@ export class VirtualMetadataComponent implements OnInit {
8398 /**
8499 * Get an array of the left and the right item of the relationship to be deleted.
85100 */
86- get items ( ) {
87- return [ this . leftItem , this . rightItem ] ;
88- }
101+ itemDTOs$ : BehaviorSubject < ItemDTO [ ] > = new BehaviorSubject ( [ ] ) ;
102+
103+ subs : Subscription [ ] = [ ] ;
89104
90105 public virtualMetadata : Map < string , VirtualMetadata [ ] > = new Map < string , VirtualMetadata [ ] > ( ) ;
91106
@@ -137,14 +152,33 @@ export class VirtualMetadataComponent implements OnInit {
137152 /**
138153 * Prevent unnecessary rerendering so fields don't lose focus
139154 */
140- trackItem ( index , item : Item ) {
141- return item && item . uuid ;
155+ trackItemDTO ( index , itemDTO : ItemDTO ) : string {
156+ return itemDTO ?. item ? .uuid ;
142157 }
143158
144159 ngOnInit ( ) : void {
145- this . items . forEach ( ( item ) => {
146- this . virtualMetadata . set ( item . uuid , this . getVirtualMetadata ( item ) ) ;
147- } ) ;
160+ this . subs . push ( this . itemDTOs$ . subscribe ( ( itemDTOs : ItemDTO [ ] ) => {
161+ itemDTOs . forEach ( ( itemDTO : ItemDTO ) => this . virtualMetadata . set ( itemDTO . item . uuid , this . getVirtualMetadata ( itemDTO . item ) ) ) ;
162+ } ) ) ;
163+ }
164+
165+ ngOnChanges ( ) : void {
166+ if ( hasValue ( this . leftItem ) && hasValue ( this . rightItem ) ) {
167+ this . itemDTOs$ . next ( [
168+ {
169+ item : this . leftItem ,
170+ isSelectedVirtualMetadataItem$ : this . isSelectedVirtualMetadataItem ( this . leftItem ) ,
171+ } ,
172+ {
173+ item : this . rightItem ,
174+ isSelectedVirtualMetadataItem$ : this . isSelectedVirtualMetadataItem ( this . rightItem ) ,
175+ } ,
176+ ] ) ;
177+ }
178+ }
179+
180+ ngOnDestroy ( ) : void {
181+ this . subs . forEach ( ( sub : Subscription ) => sub . unsubscribe ( ) ) ;
148182 }
149183}
150184
0 commit comments