1- import { ChangeDetectorRef , Component , EventEmitter , Input , OnInit , Output , ViewChild } from '@angular/core' ;
1+ import {
2+ ChangeDetectorRef ,
3+ Component ,
4+ EventEmitter ,
5+ Input ,
6+ OnInit ,
7+ Output ,
8+ ViewChild
9+ } from '@angular/core' ;
210import { UntypedFormGroup } from '@angular/forms' ;
311
412import {
@@ -67,6 +75,9 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
6775 previousValue : any ;
6876 inputValue : any ;
6977 preloadLevel : number ;
78+ additionalInfoSelectIsOpen = false ;
79+ alternativeNamesKey = 'alternative-names' ;
80+
7081
7182 private isHierarchicalVocabulary$ : Observable < boolean > ;
7283 private subs : Subscription [ ] = [ ] ;
@@ -94,6 +105,7 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
94105 * to display in the onebox popup.
95106 */
96107 search = ( text$ : Observable < string > ) => {
108+ this . additionalInfoSelectIsOpen = false ;
97109 return text$ . pipe (
98110 merge ( this . click$ ) ,
99111 debounceTime ( 300 ) ,
@@ -167,7 +179,7 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
167179 * @param event
168180 */
169181 onInput ( event ) {
170- if ( ! this . model . vocabularyOptions . closed && isNotEmpty ( event . target . value ) ) {
182+ if ( ! this . model . vocabularyOptions . closed && isNotEmpty ( event . target . value ) ) {
171183 this . inputValue = new FormFieldMetadataValueObject ( event . target . value ) ;
172184 if ( this . model . value ) {
173185 if ( ( this . model . value as any ) . securityLevel != null ) {
@@ -187,7 +199,7 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
187199 if ( isNotNull ( this . inputValue ) && this . model . value !== this . inputValue ) {
188200 this . dispatchUpdate ( this . inputValue ) ;
189201 }
190- this . inputValue = null ;
202+ this . inputValue = null ;
191203 }
192204 this . blur . emit ( event ) ;
193205 } else {
@@ -221,8 +233,23 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
221233 */
222234 onSelectItem ( event : NgbTypeaheadSelectItemEvent ) {
223235 this . inputValue = null ;
224- this . setCurrentValue ( event . item ) ;
225- this . dispatchUpdate ( event . item ) ;
236+ const item = event . item ;
237+
238+ if ( hasValue ( item . otherInformation ) ) {
239+ const otherInfoKeys = Object . keys ( item . otherInformation ) . filter ( ( key ) => ! key . startsWith ( 'data' ) ) ;
240+ const hasMultipleValues = otherInfoKeys . some ( key => hasValue ( item . otherInformation [ key ] ) && item . otherInformation [ key ] . includes ( '|||' ) ) ;
241+
242+ if ( hasMultipleValues ) {
243+ this . setMultipleValuesForOtherInfo ( otherInfoKeys , item ) ;
244+ } else {
245+ this . resetMultipleValuesForOtherInfo ( ) ;
246+ }
247+ } else {
248+ this . resetMultipleValuesForOtherInfo ( ) ;
249+ }
250+
251+ this . setCurrentValue ( item ) ;
252+ this . dispatchUpdate ( item ) ;
226253 }
227254
228255 /**
@@ -287,22 +314,37 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
287314 } else {
288315 result = value ;
289316 }
317+ this . currentValue = null ;
318+ this . cdr . detectChanges ( ) ;
290319
291320 this . currentValue = result ;
292321 this . previousValue = result ;
293322 this . cdr . detectChanges ( ) ;
294323 }
295-
324+ if ( hasValue ( this . currentValue . otherInformation ) ) {
325+ const infoKeys = Object . keys ( this . currentValue . otherInformation ) ;
326+ this . setMultipleValuesForOtherInfo ( infoKeys , this . currentValue ) ;
327+ }
296328 }
297329
298330 /**
299331 * Get the other information value removing the authority section (after the last ::)
300332 * @param itemValue the initial item value
333+ * @param itemKey
301334 */
302- getOtherInfoValue ( itemValue : string ) : string {
335+ getOtherInfoValue ( itemValue : string , itemKey : string ) : string {
303336 if ( ! itemValue || ! itemValue . includes ( '::' ) ) {
304337 return itemValue ;
305338 }
339+
340+ if ( itemValue . includes ( '|||' ) ) {
341+ let result = '' ;
342+ const values = itemValue . split ( '|||' ) . map ( item => item . substring ( 0 , item . lastIndexOf ( '::' ) ) ) ;
343+ const lastIndex = values . length - 1 ;
344+ values . forEach ( ( value , i ) => result += i === lastIndex ? value : value + ' · ' ) ;
345+ return result ;
346+ }
347+
306348 return itemValue . substring ( 0 , itemValue . lastIndexOf ( '::' ) ) ;
307349 }
308350
@@ -311,4 +353,64 @@ export class DsDynamicOneboxComponent extends DsDynamicVocabularyComponent imple
311353 . filter ( ( sub ) => hasValue ( sub ) )
312354 . forEach ( ( sub ) => sub . unsubscribe ( ) ) ;
313355 }
356+
357+ toggleOtherInfoSelection ( ) {
358+ this . additionalInfoSelectIsOpen = ! this . additionalInfoSelectIsOpen ;
359+ }
360+
361+ selectAlternativeInfo ( info : string ) {
362+ this . searching = true ;
363+
364+ if ( this . otherInfoKey !== this . alternativeNamesKey ) {
365+ this . otherInfoValue = info ;
366+ } else {
367+ this . otherName = info ;
368+ }
369+
370+ const temp = this . createVocabularyObject ( info , info , this . currentValue . otherInformation ) ;
371+ this . currentValue = null ;
372+ this . currentValue = temp ;
373+
374+ const event = {
375+ item : this . currentValue
376+ } as any ;
377+
378+ this . onSelectItem ( event ) ;
379+ this . searching = false ;
380+ this . toggleOtherInfoSelection ( ) ;
381+ }
382+
383+
384+ setMultipleValuesForOtherInfo ( keys : string [ ] , item : any ) {
385+ const hasAlternativeNames = keys . includes ( this . alternativeNamesKey ) ;
386+
387+ this . otherInfoKey = hasAlternativeNames ? this . alternativeNamesKey : keys . find ( key => hasValue ( item . otherInformation [ key ] ) && item . otherInformation [ key ] . includes ( '|||' ) ) ;
388+ this . otherInfoValuesUnformatted = item . otherInformation [ this . otherInfoKey ] ? item . otherInformation [ this . otherInfoKey ] . split ( '|||' ) : [ ] ;
389+ this . otherInfoValues = this . otherInfoValuesUnformatted . map ( unformattedItem => unformattedItem . substring ( 0 , unformattedItem . lastIndexOf ( '::' ) ) ) ;
390+
391+ if ( hasAlternativeNames ) {
392+ this . otherName = hasValue ( this . otherName ) ? this . otherName : this . otherInfoValues [ 0 ] ;
393+ }
394+
395+ if ( keys . length > 1 ) {
396+ this . otherInfoValue = hasValue ( this . otherInfoValue ) ? this . otherInfoValue : this . otherInfoValues [ 0 ] ;
397+ }
398+ }
399+
400+ resetMultipleValuesForOtherInfo ( ) {
401+ this . otherInfoKey = undefined ;
402+ this . otherInfoValuesUnformatted = [ ] ;
403+ this . otherInfoValues = [ ] ;
404+ this . otherInfoValue = undefined ;
405+ this . otherName = undefined ;
406+ }
407+
408+ createVocabularyObject ( display , value , otherInformation ) {
409+ return Object . assign ( new VocabularyEntry ( ) , this . model . value , {
410+ display : display ,
411+ value : value ,
412+ otherInformation : otherInformation ,
413+ type : 'vocabularyEntry'
414+ } ) ;
415+ }
314416}
0 commit comments