88 expand ,
99 Observable ,
1010 of as observableOf , reduce ,
11- Subscription
11+ Subscription , take
1212} from 'rxjs' ;
1313import { DynamicFormControlModel , DynamicFormGroupModel , DynamicFormLayout , DynamicFormService , DynamicInputModel , DynamicSelectModel } from '@ng-dynamic-forms/core' ;
1414import { UntypedFormGroup } from '@angular/forms' ;
@@ -33,6 +33,8 @@ import { Item } from '../../core/shared/item.model';
3333import { DsDynamicInputModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model' ;
3434import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model' ;
3535import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service' ;
36+ import { DynamicScrollableDropdownModel } from 'src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.model' ;
37+ import { FindAllDataImpl } from "../../core/data/base/find-all-data" ;
3638
3739@Component ( {
3840 selector : 'ds-edit-bitstream-page' ,
@@ -51,12 +53,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
5153 */
5254 bitstreamRD$ : Observable < RemoteData < Bitstream > > ;
5355
54- /**
55- * The formats their remote data observable
56- * Tracks changes and updates the view
57- */
58- bitstreamFormatsRD$ : Observable < RemoteData < PaginatedList < BitstreamFormat > > > ;
59-
6056 /**
6157 * The UUID of the primary bitstream for this bundle
6258 */
@@ -72,11 +68,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
7268 */
7369 originalFormat : BitstreamFormat ;
7470
75- /**
76- * A list of all available bitstream formats
77- */
78- formats : BitstreamFormat [ ] ;
79-
8071 /**
8172 * @type {string } Key prefix used to generate form messages
8273 */
@@ -163,9 +154,22 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
163154 /**
164155 * The Dynamic Input Model for the selected format
165156 */
166- selectedFormatModel = new DynamicSelectModel ( {
157+ selectedFormatModel = new DynamicScrollableDropdownModel ( {
167158 id : 'selectedFormat' ,
168- name : 'selectedFormat'
159+ name : 'selectedFormat' ,
160+ displayKey : 'shortDescription' ,
161+ repeatable : false ,
162+ metadataFields : [ ] ,
163+ submissionId : '' ,
164+ hasSelectableMetadata : false ,
165+ findAllFactory : this . findAllFormatsServiceFactory ( ) ,
166+ formatFunction : ( format : BitstreamFormat | string ) => {
167+ if ( format instanceof BitstreamFormat ) {
168+ return hasValue ( format ) && format . supportLevel === BitstreamFormatSupportLevel . Unknown ? this . translate . instant ( this . KEY_PREFIX + 'selectedFormat.unknown' ) : format . shortDescription ;
169+ } else {
170+ return format ;
171+ }
172+ } ,
169173 } ) ;
170174
171175 /**
@@ -380,6 +384,11 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
380384 * @private
381385 */
382386 private bundle : Bundle ;
387+ /**
388+ * The currently selected format
389+ * @private
390+ */
391+ private selectedFormat : BitstreamFormat ;
383392
384393 constructor ( private route : ActivatedRoute ,
385394 private router : Router ,
@@ -407,25 +416,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
407416 this . entityType = this . route . snapshot . queryParams . entityType ;
408417 this . bitstreamRD$ = this . route . data . pipe ( map ( ( data : any ) => data . bitstream ) ) ;
409418
410- this . bitstreamFormatsRD$ = this . bitstreamFormatService . findAll ( this . findAllOptions ) . pipe (
411- getFirstSucceededRemoteData ( ) ,
412- expand ( ( response : RemoteData < PaginatedList < BitstreamFormat > > ) => {
413- const pageInfo = response . payload . pageInfo ;
414- if ( pageInfo . currentPage < pageInfo . totalPages ) {
415- const nextPageOptions = { ...this . findAllOptions , currentPage : pageInfo . currentPage + 1 } ;
416- return this . bitstreamFormatService . findAll ( nextPageOptions ) . pipe ( getFirstSucceededRemoteData ( ) ) ;
417- } else {
418- return EMPTY ;
419- }
420- } ) ,
421- ) ;
422-
423- const bitstreamFormats$ = this . bitstreamFormatsRD$ . pipe (
424- reduce ( ( acc : BitstreamFormat [ ] , response : RemoteData < PaginatedList < BitstreamFormat > > ) => {
425- return acc . concat ( response . payload . page ) ;
426- } , [ ] )
427- )
428-
429419 const bitstream$ = this . bitstreamRD$ . pipe (
430420 getFirstSucceededRemoteData ( ) ,
431421 getRemoteDataPayload ( ) ,
@@ -446,24 +436,31 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
446436 switchMap ( ( bundle : Bundle ) => bundle . item ) ,
447437 getFirstSucceededRemoteDataPayload ( ) ,
448438 ) ;
439+ const format$ = bitstream$ . pipe (
440+ switchMap ( bitstream => bitstream . format ) ,
441+ getFirstSucceededRemoteDataPayload ( ) ,
442+ ) ;
443+
449444 this . subs . push (
450445 observableCombineLatest (
451446 bitstream$ ,
452- bitstreamFormats$ ,
453447 bundle$ ,
454448 primaryBitstream$ ,
455449 item$ ,
456- ) . pipe ( )
457- . subscribe ( ( [ bitstream , allFormats , bundle , primaryBitstream , item ] ) => {
458- this . bitstream = bitstream as Bitstream ;
459- this . formats = allFormats ;
460- this . bundle = bundle ;
461- // hasValue(primaryBitstream) because if there's no primaryBitstream on the bundle it will
462- // be a success response, but empty
463- this . primaryBitstreamUUID = hasValue ( primaryBitstream ) ? primaryBitstream . uuid : null ;
464- this . itemId = item . uuid ;
465- this . setIiifStatus ( this . bitstream ) ;
466- } )
450+ format$ ,
451+ ) . subscribe ( ( [ bitstream , bundle , primaryBitstream , item , format ] ) => {
452+ this . bitstream = bitstream as Bitstream ;
453+ this . bundle = bundle ;
454+ this . selectedFormat = format ;
455+ // hasValue(primaryBitstream) because if there's no primaryBitstream on the bundle it will
456+ // be a success response, but empty
457+ this . primaryBitstreamUUID = hasValue ( primaryBitstream ) ? primaryBitstream . uuid : null ;
458+ this . itemId = item . uuid ;
459+ this . setIiifStatus ( this . bitstream ) ;
460+ } ) ,
461+ format$ . pipe ( take ( 1 ) ) . subscribe (
462+ ( format ) => this . originalFormat = format ,
463+ ) ,
467464 ) ;
468465
469466 this . subs . push (
@@ -479,7 +476,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
479476 */
480477 setForm ( ) {
481478 this . formGroup = this . formService . createFormGroup ( this . formModel ) ;
482- this . updateFormatModel ( ) ;
483479 this . updateForm ( this . bitstream ) ;
484480 this . updateFieldTranslations ( ) ;
485481 }
@@ -498,8 +494,9 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
498494 description : bitstream . firstMetadataValue ( 'dc.description' )
499495 } ,
500496 formatContainer : {
501- newFormat : hasValue ( bitstream . firstMetadata ( 'dc.format' ) ) ? bitstream . firstMetadata ( 'dc.format' ) . value : undefined
502- }
497+ selectedFormat : this . selectedFormat . shortDescription ,
498+ newFormat : hasValue ( bitstream . firstMetadata ( 'dc.format' ) ) ? bitstream . firstMetadata ( 'dc.format' ) . value : undefined ,
499+ } ,
503500 } ) ;
504501 if ( this . isIIIF ) {
505502 this . formGroup . patchValue ( {
@@ -517,36 +514,16 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
517514 }
518515 } ) ;
519516 }
520- this . bitstream . format . pipe (
521- getAllSucceededRemoteDataPayload ( )
522- ) . subscribe ( ( format : BitstreamFormat ) => {
523- this . originalFormat = format ;
524- this . formGroup . patchValue ( {
525- formatContainer : {
526- selectedFormat : format . id
527- }
528- } ) ;
529- this . updateNewFormatLayout ( format . id ) ;
530- } ) ;
517+ this . updateNewFormatLayout ( ) ;
531518 }
532519
533- /**
534- * Create the list of unknown format IDs an add options to the selectedFormatModel
535- */
536- updateFormatModel ( ) {
537- this . selectedFormatModel . options = this . formats . map ( ( format : BitstreamFormat ) =>
538- Object . assign ( {
539- value : format . id ,
540- label : this . isUnknownFormat ( format . id ) ? this . translate . instant ( this . KEY_PREFIX + 'selectedFormat.unknown' ) : format . shortDescription
541- } ) ) ;
542- }
543520
544521 /**
545522 * Update the layout of the "Other Format" input depending on the selected format
546523 * @param selectedId
547524 */
548- updateNewFormatLayout ( selectedId : string ) {
549- if ( this . isUnknownFormat ( selectedId ) ) {
525+ updateNewFormatLayout ( ) {
526+ if ( this . isUnknownFormat ( ) ) {
550527 this . formLayout . newFormat . grid . host = this . newFormatBaseLayout ;
551528 } else {
552529 this . formLayout . newFormat . grid . host = this . newFormatBaseLayout + ' invisible' ;
@@ -557,9 +534,8 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
557534 * Is the provided format (id) part of the list of unknown formats?
558535 * @param id
559536 */
560- isUnknownFormat ( id : string ) : boolean {
561- const format = this . formats . find ( ( f : BitstreamFormat ) => f . id === id ) ;
562- return hasValue ( format ) && format . supportLevel === BitstreamFormatSupportLevel . Unknown ;
537+ isUnknownFormat ( ) : boolean {
538+ return hasValue ( this . selectedFormat ) && this . selectedFormat . supportLevel === BitstreamFormatSupportLevel . Unknown ;
563539 }
564540
565541 /**
@@ -591,7 +567,8 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
591567 onChange ( event ) {
592568 const model = event . model ;
593569 if ( model . id === this . selectedFormatModel . id ) {
594- this . updateNewFormatLayout ( model . value ) ;
570+ this . selectedFormat = model . value ;
571+ this . updateNewFormatLayout ( ) ;
595572 }
596573 }
597574
@@ -601,8 +578,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
601578 onSubmit ( ) {
602579 const updatedValues = this . formGroup . getRawValue ( ) ;
603580 const updatedBitstream = this . formToBitstream ( updatedValues ) ;
604- const selectedFormat = this . formats . find ( ( f : BitstreamFormat ) => f . id === updatedValues . formatContainer . selectedFormat ) ;
605- const isNewFormat = selectedFormat . id !== this . originalFormat . id ;
581+ const isNewFormat = this . selectedFormat . id !== this . originalFormat . id ;
606582 const isPrimary = updatedValues . fileNamePrimaryContainer . primaryBitstream ;
607583 const wasPrimary = this . primaryBitstreamUUID === this . bitstream . uuid ;
608584
@@ -654,7 +630,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
654630 bundle$ = observableOf ( this . bundle ) ;
655631 }
656632 if ( isNewFormat ) {
657- bitstream$ = this . bitstreamService . updateFormat ( this . bitstream , selectedFormat ) . pipe (
633+ bitstream$ = this . bitstreamService . updateFormat ( this . bitstream , this . selectedFormat ) . pipe (
658634 getFirstCompletedRemoteData ( ) ,
659635 map ( ( formatResponse : RemoteData < Bitstream > ) => {
660636 if ( hasValue ( formatResponse ) && formatResponse . hasFailed ) {
@@ -812,4 +788,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
812788 . forEach ( ( subscription ) => subscription . unsubscribe ( ) ) ;
813789 }
814790
791+ findAllFormatsServiceFactory ( ) {
792+ return ( ) => this . bitstreamFormatService as any as FindAllDataImpl < BitstreamFormat > ;
793+ }
815794}
0 commit comments