@@ -29,6 +29,7 @@ import {
2929 TranslateService ,
3030} from '@ngx-translate/core' ;
3131import {
32+ BehaviorSubject ,
3233 EMPTY ,
3334 Observable ,
3435 of as observableOf ,
@@ -37,6 +38,7 @@ import {
3738 map ,
3839 switchMap ,
3940 take ,
41+ tap ,
4042} from 'rxjs/operators' ;
4143import { RegistryService } from 'src/app/core/registry/registry.service' ;
4244import { VocabularyService } from 'src/app/core/submission/vocabularies/vocabulary.service' ;
@@ -195,9 +197,9 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
195197 group = new UntypedFormGroup ( { authorityField : new UntypedFormControl ( ) } ) ;
196198
197199 /**
198- * Observable property of the model to use for editinf authorities values
200+ * Model to use for editing authorities values
199201 */
200- private model$ : Observable < DynamicOneboxModel | DynamicScrollableDropdownModel > ;
202+ private model$ : BehaviorSubject < DynamicOneboxModel | DynamicScrollableDropdownModel > = new BehaviorSubject ( null ) ;
201203
202204 /**
203205 * Observable with information about the authority vocabulary used
@@ -274,6 +276,8 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
274276 }
275277
276278 this . isAuthorityControlled$ = this . vocabulary$ . pipe (
279+ // Create the model used by the authority fields to ensure its existence when the field is initialized
280+ tap ( ( v : Vocabulary ) => this . model$ . next ( this . createModel ( v ) ) ) ,
277281 map ( ( result : Vocabulary ) => isNotEmpty ( result ) ) ,
278282 ) ;
279283
@@ -289,54 +293,62 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
289293 map ( ( result : Vocabulary ) => isNotEmpty ( result ) && ! result . hierarchical && ! result . scrollable ) ,
290294 ) ;
291295
292- this . model$ = this . vocabulary$ . pipe (
293- map ( ( vocabulary : Vocabulary ) => {
294- let formFieldValue ;
295- if ( isNotEmpty ( this . mdValue . newValue . value ) ) {
296- formFieldValue = new FormFieldMetadataValueObject ( ) ;
297- formFieldValue . value = this . mdValue . newValue . value ;
298- formFieldValue . display = this . mdValue . newValue . value ;
299- if ( this . mdValue . newValue . authority ) {
300- formFieldValue . authority = this . mdValue . newValue . authority ;
301- formFieldValue . confidence = this . mdValue . newValue . confidence ;
302- }
303- } else {
304- formFieldValue = this . mdValue . newValue . value ;
305- }
296+ }
306297
307- const vocabularyOptions = vocabulary ? {
308- closed : false ,
309- name : vocabulary . name ,
310- } as VocabularyOptions : null ;
311-
312- if ( ! vocabulary . scrollable ) {
313- const model : DsDynamicOneboxModelConfig = {
314- id : 'authorityField' ,
315- label : `${ this . dsoType } .edit.metadata.edit.value` ,
316- vocabularyOptions : vocabularyOptions ,
317- metadataFields : [ this . mdField ] ,
318- value : formFieldValue ,
319- repeatable : false ,
320- submissionId : 'edit-metadata' ,
321- hasSelectableMetadata : false ,
322- } ;
323- return new DynamicOneboxModel ( model ) ;
324- } else {
325- const model : DynamicScrollableDropdownModelConfig = {
326- id : 'authorityField' ,
327- label : `${ this . dsoType } .edit.metadata.edit.value` ,
328- placeholder : `${ this . dsoType } .edit.metadata.edit.value` ,
329- vocabularyOptions : vocabularyOptions ,
330- metadataFields : [ this . mdField ] ,
331- value : formFieldValue ,
332- repeatable : false ,
333- submissionId : 'edit-metadata' ,
334- hasSelectableMetadata : false ,
335- maxOptions : 10 ,
336- } ;
337- return new DynamicScrollableDropdownModel ( model ) ;
298+ /**
299+ * Returns a {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model based on the
300+ * vocabulary used.
301+ */
302+ private createModel ( vocabulary : Vocabulary ) : DynamicOneboxModel | DynamicScrollableDropdownModel {
303+ if ( isNotEmpty ( vocabulary ) ) {
304+ let formFieldValue ;
305+ if ( isNotEmpty ( this . mdValue . newValue . value ) ) {
306+ formFieldValue = new FormFieldMetadataValueObject ( ) ;
307+ formFieldValue . value = this . mdValue . newValue . value ;
308+ formFieldValue . display = this . mdValue . newValue . value ;
309+ if ( this . mdValue . newValue . authority ) {
310+ formFieldValue . authority = this . mdValue . newValue . authority ;
311+ formFieldValue . confidence = this . mdValue . newValue . confidence ;
338312 }
339- } ) ) ;
313+ } else {
314+ formFieldValue = this . mdValue . newValue . value ;
315+ }
316+
317+ const vocabularyOptions = vocabulary ? {
318+ closed : false ,
319+ name : vocabulary . name ,
320+ } as VocabularyOptions : null ;
321+
322+ if ( ! vocabulary . scrollable ) {
323+ const model : DsDynamicOneboxModelConfig = {
324+ id : 'authorityField' ,
325+ label : `${ this . dsoType } .edit.metadata.edit.value` ,
326+ vocabularyOptions : vocabularyOptions ,
327+ metadataFields : [ this . mdField ] ,
328+ value : formFieldValue ,
329+ repeatable : false ,
330+ submissionId : 'edit-metadata' ,
331+ hasSelectableMetadata : false ,
332+ } ;
333+ return new DynamicOneboxModel ( model ) ;
334+ } else {
335+ const model : DynamicScrollableDropdownModelConfig = {
336+ id : 'authorityField' ,
337+ label : `${ this . dsoType } .edit.metadata.edit.value` ,
338+ placeholder : `${ this . dsoType } .edit.metadata.edit.value` ,
339+ vocabularyOptions : vocabularyOptions ,
340+ metadataFields : [ this . mdField ] ,
341+ value : formFieldValue ,
342+ repeatable : false ,
343+ submissionId : 'edit-metadata' ,
344+ hasSelectableMetadata : false ,
345+ maxOptions : 10 ,
346+ } ;
347+ return new DynamicScrollableDropdownModel ( model ) ;
348+ }
349+ } else {
350+ return null ;
351+ }
340352 }
341353
342354 /**
@@ -434,11 +446,11 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
434446 }
435447
436448 /**
437- * Returns an observable with the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
449+ * Returns the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
438450 * for the authority field
439451 */
440- getModel ( ) : Observable < DynamicOneboxModel | DynamicScrollableDropdownModel > {
441- return this . model$ ;
452+ getModel ( ) : DynamicOneboxModel | DynamicScrollableDropdownModel {
453+ return this . model$ . value ;
442454 }
443455
444456 /**
0 commit comments