@@ -21,7 +21,6 @@ import {
2121 DynamicFormLayout ,
2222 DynamicFormService ,
2323 DynamicInputModel ,
24- DynamicSelectModel ,
2524} from '@ng-dynamic-forms/core' ;
2625import {
2726 TranslateModule ,
@@ -31,24 +30,22 @@ import cloneDeep from 'lodash/cloneDeep';
3130import {
3231 combineLatest ,
3332 combineLatest as observableCombineLatest ,
34- EMPTY ,
3533 Observable ,
3634 of as observableOf ,
3735 Subscription ,
3836} from 'rxjs' ;
3937import {
40- expand ,
4138 filter ,
4239 map ,
43- reduce ,
4440 switchMap ,
41+ take ,
4542 tap ,
4643} from 'rxjs/operators' ;
4744
4845import { DSONameService } from '../../core/breadcrumbs/dso-name.service' ;
46+ import { FindAllDataImpl } from '../../core/data/base/find-all-data' ;
4947import { BitstreamDataService } from '../../core/data/bitstream-data.service' ;
5048import { BitstreamFormatDataService } from '../../core/data/bitstream-format-data.service' ;
51- import { PaginatedList } from '../../core/data/paginated-list.model' ;
5249import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service' ;
5350import { RemoteData } from '../../core/data/remote-data' ;
5451import { Bitstream } from '../../core/shared/bitstream.model' ;
@@ -58,7 +55,6 @@ import { Bundle } from '../../core/shared/bundle.model';
5855import { Item } from '../../core/shared/item.model' ;
5956import { Metadata } from '../../core/shared/metadata.utils' ;
6057import {
61- getAllSucceededRemoteDataPayload ,
6258 getFirstCompletedRemoteData ,
6359 getFirstSucceededRemoteData ,
6460 getFirstSucceededRemoteDataPayload ,
@@ -75,6 +71,7 @@ import { ErrorComponent } from '../../shared/error/error.component';
7571import { DynamicCustomSwitchModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/custom-switch/custom-switch.model' ;
7672import { DsDynamicInputModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model' ;
7773import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model' ;
74+ import { DynamicScrollableDropdownModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.model' ;
7875import { FormComponent } from '../../shared/form/form.component' ;
7976import { ThemedLoadingComponent } from '../../shared/loading/themed-loading.component' ;
8077import { NotificationsService } from '../../shared/notifications/notifications.service' ;
@@ -112,12 +109,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
112109 */
113110 bitstreamRD$ : Observable < RemoteData < Bitstream > > ;
114111
115- /**
116- * The formats their remote data observable
117- * Tracks changes and updates the view
118- */
119- bitstreamFormatsRD$ : Observable < RemoteData < PaginatedList < BitstreamFormat > > > ;
120-
121112 /**
122113 * The UUID of the primary bitstream for this bundle
123114 */
@@ -133,11 +124,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
133124 */
134125 originalFormat : BitstreamFormat ;
135126
136- /**
137- * A list of all available bitstream formats
138- */
139- formats : BitstreamFormat [ ] ;
140-
141127 /**
142128 * @type {string } Key prefix used to generate form messages
143129 */
@@ -224,9 +210,22 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
224210 /**
225211 * The Dynamic Input Model for the selected format
226212 */
227- selectedFormatModel = new DynamicSelectModel ( {
213+ selectedFormatModel = new DynamicScrollableDropdownModel ( {
228214 id : 'selectedFormat' ,
229215 name : 'selectedFormat' ,
216+ displayKey : 'shortDescription' ,
217+ repeatable : false ,
218+ metadataFields : [ ] ,
219+ submissionId : '' ,
220+ hasSelectableMetadata : false ,
221+ findAllFactory : this . findAllFormatsServiceFactory ( ) ,
222+ formatFunction : ( format : BitstreamFormat | string ) => {
223+ if ( format instanceof BitstreamFormat ) {
224+ return hasValue ( format ) && format . supportLevel === BitstreamFormatSupportLevel . Unknown ? this . translate . instant ( this . KEY_PREFIX + 'selectedFormat.unknown' ) : format . shortDescription ;
225+ } else {
226+ return format ;
227+ }
228+ } ,
230229 } ) ;
231230
232231 /**
@@ -444,6 +443,11 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
444443 * @private
445444 */
446445 private bundle : Bundle ;
446+ /**
447+ * The currently selected format
448+ * @private
449+ */
450+ private selectedFormat : BitstreamFormat ;
447451
448452 constructor ( private route : ActivatedRoute ,
449453 private router : Router ,
@@ -470,25 +474,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
470474 this . entityType = this . route . snapshot . queryParams . entityType ;
471475 this . bitstreamRD$ = this . route . data . pipe ( map ( ( data : any ) => data . bitstream ) ) ;
472476
473- this . bitstreamFormatsRD$ = this . bitstreamFormatService . findAll ( this . findAllOptions ) . pipe (
474- getFirstSucceededRemoteData ( ) ,
475- expand ( ( response : RemoteData < PaginatedList < BitstreamFormat > > ) => {
476- const pageInfo = response . payload . pageInfo ;
477- if ( pageInfo . currentPage < pageInfo . totalPages ) {
478- const nextPageOptions = { ...this . findAllOptions , currentPage : pageInfo . currentPage + 1 } ;
479- return this . bitstreamFormatService . findAll ( nextPageOptions ) . pipe ( getFirstSucceededRemoteData ( ) ) ;
480- } else {
481- return EMPTY ;
482- }
483- } ) ,
484- ) ;
485-
486- const bitstreamFormats$ = this . bitstreamFormatsRD$ . pipe (
487- reduce ( ( acc : BitstreamFormat [ ] , response : RemoteData < PaginatedList < BitstreamFormat > > ) => {
488- return acc . concat ( response . payload . page ) ;
489- } , [ ] ) ,
490- ) ;
491-
492477 const bitstream$ = this . bitstreamRD$ . pipe (
493478 getFirstSucceededRemoteData ( ) ,
494479 getRemoteDataPayload ( ) ,
@@ -509,24 +494,31 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
509494 switchMap ( ( bundle : Bundle ) => bundle . item ) ,
510495 getFirstSucceededRemoteDataPayload ( ) ,
511496 ) ;
497+ const format$ = bitstream$ . pipe (
498+ switchMap ( bitstream => bitstream . format ) ,
499+ getFirstSucceededRemoteDataPayload ( ) ,
500+ ) ;
501+
512502 this . subs . push (
513503 observableCombineLatest (
514504 bitstream$ ,
515- bitstreamFormats$ ,
516505 bundle$ ,
517506 primaryBitstream$ ,
518507 item$ ,
519- ) . pipe ( )
520- . subscribe ( ( [ bitstream , allFormats , bundle , primaryBitstream , item ] ) => {
521- this . bitstream = bitstream as Bitstream ;
522- this . formats = allFormats ;
523- this . bundle = bundle ;
524- // hasValue(primaryBitstream) because if there's no primaryBitstream on the bundle it will
525- // be a success response, but empty
526- this . primaryBitstreamUUID = hasValue ( primaryBitstream ) ? primaryBitstream . uuid : null ;
527- this . itemId = item . uuid ;
528- this . setIiifStatus ( this . bitstream ) ;
529- } ) ,
508+ format$ ,
509+ ) . subscribe ( ( [ bitstream , bundle , primaryBitstream , item , format ] ) => {
510+ this . bitstream = bitstream as Bitstream ;
511+ this . bundle = bundle ;
512+ this . selectedFormat = format ;
513+ // hasValue(primaryBitstream) because if there's no primaryBitstream on the bundle it will
514+ // be a success response, but empty
515+ this . primaryBitstreamUUID = hasValue ( primaryBitstream ) ? primaryBitstream . uuid : null ;
516+ this . itemId = item . uuid ;
517+ this . setIiifStatus ( this . bitstream ) ;
518+ } ) ,
519+ format$ . pipe ( take ( 1 ) ) . subscribe (
520+ ( format ) => this . originalFormat = format ,
521+ ) ,
530522 ) ;
531523
532524 this . subs . push (
@@ -542,7 +534,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
542534 */
543535 setForm ( ) {
544536 this . formGroup = this . formService . createFormGroup ( this . formModel ) ;
545- this . updateFormatModel ( ) ;
546537 this . updateForm ( this . bitstream ) ;
547538 this . updateFieldTranslations ( ) ;
548539 }
@@ -561,6 +552,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
561552 description : bitstream . firstMetadataValue ( 'dc.description' ) ,
562553 } ,
563554 formatContainer : {
555+ selectedFormat : this . selectedFormat . shortDescription ,
564556 newFormat : hasValue ( bitstream . firstMetadata ( 'dc.format' ) ) ? bitstream . firstMetadata ( 'dc.format' ) . value : undefined ,
565557 } ,
566558 } ) ;
@@ -580,36 +572,16 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
580572 } ,
581573 } ) ;
582574 }
583- this . bitstream . format . pipe (
584- getAllSucceededRemoteDataPayload ( ) ,
585- ) . subscribe ( ( format : BitstreamFormat ) => {
586- this . originalFormat = format ;
587- this . formGroup . patchValue ( {
588- formatContainer : {
589- selectedFormat : format . id ,
590- } ,
591- } ) ;
592- this . updateNewFormatLayout ( format . id ) ;
593- } ) ;
575+ this . updateNewFormatLayout ( ) ;
594576 }
595577
596- /**
597- * Create the list of unknown format IDs an add options to the selectedFormatModel
598- */
599- updateFormatModel ( ) {
600- this . selectedFormatModel . options = this . formats . map ( ( format : BitstreamFormat ) =>
601- Object . assign ( {
602- value : format . id ,
603- label : this . isUnknownFormat ( format . id ) ? this . translate . instant ( this . KEY_PREFIX + 'selectedFormat.unknown' ) : format . shortDescription ,
604- } ) ) ;
605- }
606578
607579 /**
608580 * Update the layout of the "Other Format" input depending on the selected format
609581 * @param selectedId
610582 */
611- updateNewFormatLayout ( selectedId : string ) {
612- if ( this . isUnknownFormat ( selectedId ) ) {
583+ updateNewFormatLayout ( ) {
584+ if ( this . isUnknownFormat ( ) ) {
613585 this . formLayout . newFormat . grid . host = this . newFormatBaseLayout ;
614586 } else {
615587 this . formLayout . newFormat . grid . host = this . newFormatBaseLayout + ' invisible' ;
@@ -620,9 +592,8 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
620592 * Is the provided format (id) part of the list of unknown formats?
621593 * @param id
622594 */
623- isUnknownFormat ( id : string ) : boolean {
624- const format = this . formats . find ( ( f : BitstreamFormat ) => f . id === id ) ;
625- return hasValue ( format ) && format . supportLevel === BitstreamFormatSupportLevel . Unknown ;
595+ isUnknownFormat ( ) : boolean {
596+ return hasValue ( this . selectedFormat ) && this . selectedFormat . supportLevel === BitstreamFormatSupportLevel . Unknown ;
626597 }
627598
628599 /**
@@ -654,7 +625,8 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
654625 onChange ( event ) {
655626 const model = event . model ;
656627 if ( model . id === this . selectedFormatModel . id ) {
657- this . updateNewFormatLayout ( model . value ) ;
628+ this . selectedFormat = model . value ;
629+ this . updateNewFormatLayout ( ) ;
658630 }
659631 }
660632
@@ -664,8 +636,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
664636 onSubmit ( ) {
665637 const updatedValues = this . formGroup . getRawValue ( ) ;
666638 const updatedBitstream = this . formToBitstream ( updatedValues ) ;
667- const selectedFormat = this . formats . find ( ( f : BitstreamFormat ) => f . id === updatedValues . formatContainer . selectedFormat ) ;
668- const isNewFormat = selectedFormat . id !== this . originalFormat . id ;
639+ const isNewFormat = this . selectedFormat . id !== this . originalFormat . id ;
669640 const isPrimary = updatedValues . fileNamePrimaryContainer . primaryBitstream ;
670641 const wasPrimary = this . primaryBitstreamUUID === this . bitstream . uuid ;
671642
@@ -717,7 +688,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
717688 bundle$ = observableOf ( this . bundle ) ;
718689 }
719690 if ( isNewFormat ) {
720- bitstream$ = this . bitstreamService . updateFormat ( this . bitstream , selectedFormat ) . pipe (
691+ bitstream$ = this . bitstreamService . updateFormat ( this . bitstream , this . selectedFormat ) . pipe (
721692 getFirstCompletedRemoteData ( ) ,
722693 map ( ( formatResponse : RemoteData < Bitstream > ) => {
723694 if ( hasValue ( formatResponse ) && formatResponse . hasFailed ) {
@@ -875,4 +846,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy {
875846 . forEach ( ( subscription ) => subscription . unsubscribe ( ) ) ;
876847 }
877848
849+ findAllFormatsServiceFactory ( ) {
850+ return ( ) => this . bitstreamFormatService as any as FindAllDataImpl < BitstreamFormat > ;
851+ }
878852}
0 commit comments