@@ -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' ;
@@ -196,9 +198,9 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
196198 group = new UntypedFormGroup ( { authorityField : new UntypedFormControl ( ) } ) ;
197199
198200 /**
199- * Observable property of the model to use for editinf authorities values
201+ * Model to use for editing authorities values
200202 */
201- private model$ : Observable < DynamicOneboxModel | DynamicScrollableDropdownModel > ;
203+ private model$ : BehaviorSubject < DynamicOneboxModel | DynamicScrollableDropdownModel > = new BehaviorSubject ( null ) ;
202204
203205 /**
204206 * Observable with information about the authority vocabulary used
@@ -278,6 +280,8 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
278280 }
279281
280282 this . isAuthorityControlled$ = this . vocabulary$ . pipe (
283+ // Create the model used by the authority fields to ensure its existence when the field is initialized
284+ tap ( ( v : Vocabulary ) => this . model$ . next ( this . createModel ( v ) ) ) ,
281285 map ( ( result : Vocabulary ) => isNotEmpty ( result ) ) ,
282286 ) ;
283287
@@ -293,54 +297,62 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
293297 map ( ( result : Vocabulary ) => isNotEmpty ( result ) && ! result . hierarchical && ! result . scrollable ) ,
294298 ) ;
295299
296- this . model$ = this . vocabulary$ . pipe (
297- map ( ( vocabulary : Vocabulary ) => {
298- let formFieldValue ;
299- if ( isNotEmpty ( this . mdValue . newValue . value ) ) {
300- formFieldValue = new FormFieldMetadataValueObject ( ) ;
301- formFieldValue . value = this . mdValue . newValue . value ;
302- formFieldValue . display = this . mdValue . newValue . value ;
303- if ( this . mdValue . newValue . authority ) {
304- formFieldValue . authority = this . mdValue . newValue . authority ;
305- formFieldValue . confidence = this . mdValue . newValue . confidence ;
306- }
307- } else {
308- formFieldValue = this . mdValue . newValue . value ;
309- }
300+ }
310301
311- const vocabularyOptions = vocabulary ? {
312- closed : false ,
313- name : vocabulary . name ,
314- } as VocabularyOptions : null ;
315-
316- if ( ! vocabulary . scrollable ) {
317- const model : DsDynamicOneboxModelConfig = {
318- id : 'authorityField' ,
319- label : `${ this . dsoType } .edit.metadata.edit.value` ,
320- vocabularyOptions : vocabularyOptions ,
321- metadataFields : [ this . mdField ] ,
322- value : formFieldValue ,
323- repeatable : false ,
324- submissionId : 'edit-metadata' ,
325- hasSelectableMetadata : false ,
326- } ;
327- return new DynamicOneboxModel ( model ) ;
328- } else {
329- const model : DynamicScrollableDropdownModelConfig = {
330- id : 'authorityField' ,
331- label : `${ this . dsoType } .edit.metadata.edit.value` ,
332- placeholder : `${ this . dsoType } .edit.metadata.edit.value` ,
333- vocabularyOptions : vocabularyOptions ,
334- metadataFields : [ this . mdField ] ,
335- value : formFieldValue ,
336- repeatable : false ,
337- submissionId : 'edit-metadata' ,
338- hasSelectableMetadata : false ,
339- maxOptions : 10 ,
340- } ;
341- return new DynamicScrollableDropdownModel ( model ) ;
302+ /**
303+ * Returns a {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model based on the
304+ * vocabulary used.
305+ */
306+ private createModel ( vocabulary : Vocabulary ) : DynamicOneboxModel | DynamicScrollableDropdownModel {
307+ if ( isNotEmpty ( vocabulary ) ) {
308+ let formFieldValue ;
309+ if ( isNotEmpty ( this . mdValue . newValue . value ) ) {
310+ formFieldValue = new FormFieldMetadataValueObject ( ) ;
311+ formFieldValue . value = this . mdValue . newValue . value ;
312+ formFieldValue . display = this . mdValue . newValue . value ;
313+ if ( this . mdValue . newValue . authority ) {
314+ formFieldValue . authority = this . mdValue . newValue . authority ;
315+ formFieldValue . confidence = this . mdValue . newValue . confidence ;
342316 }
343- } ) ) ;
317+ } else {
318+ formFieldValue = this . mdValue . newValue . value ;
319+ }
320+
321+ const vocabularyOptions = vocabulary ? {
322+ closed : false ,
323+ name : vocabulary . name ,
324+ } as VocabularyOptions : null ;
325+
326+ if ( ! vocabulary . scrollable ) {
327+ const model : DsDynamicOneboxModelConfig = {
328+ id : 'authorityField' ,
329+ label : `${ this . dsoType } .edit.metadata.edit.value` ,
330+ vocabularyOptions : vocabularyOptions ,
331+ metadataFields : [ this . mdField ] ,
332+ value : formFieldValue ,
333+ repeatable : false ,
334+ submissionId : 'edit-metadata' ,
335+ hasSelectableMetadata : false ,
336+ } ;
337+ return new DynamicOneboxModel ( model ) ;
338+ } else {
339+ const model : DynamicScrollableDropdownModelConfig = {
340+ id : 'authorityField' ,
341+ label : `${ this . dsoType } .edit.metadata.edit.value` ,
342+ placeholder : `${ this . dsoType } .edit.metadata.edit.value` ,
343+ vocabularyOptions : vocabularyOptions ,
344+ metadataFields : [ this . mdField ] ,
345+ value : formFieldValue ,
346+ repeatable : false ,
347+ submissionId : 'edit-metadata' ,
348+ hasSelectableMetadata : false ,
349+ maxOptions : 10 ,
350+ } ;
351+ return new DynamicScrollableDropdownModel ( model ) ;
352+ }
353+ } else {
354+ return null ;
355+ }
344356 }
345357
346358 /**
@@ -438,11 +450,11 @@ export class DsoEditMetadataValueComponent implements OnInit, OnChanges {
438450 }
439451
440452 /**
441- * Returns an observable with the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
453+ * Returns the {@link DynamicOneboxModel} or {@link DynamicScrollableDropdownModel} model used
442454 * for the authority field
443455 */
444- getModel ( ) : Observable < DynamicOneboxModel | DynamicScrollableDropdownModel > {
445- return this . model$ ;
456+ getModel ( ) : DynamicOneboxModel | DynamicScrollableDropdownModel {
457+ return this . model$ . value ;
446458 }
447459
448460 /**
0 commit comments