Skip to content

Commit 903daad

Browse files
[DURACOM-304] add support for findAll method in dynamic-scrollable-dropdown.component.ts
1 parent 134ecd6 commit 903daad

File tree

6 files changed

+133
-100
lines changed

6 files changed

+133
-100
lines changed

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ng-container *ngVar="(bitstreamRD$ | async) as bitstreamRD">
2-
<div class="container" *ngVar="(bitstreamFormatsRD$ | async) as formatsRD">
3-
<div class="row" *ngIf="bitstreamRD?.hasSucceeded && formatsRD?.hasSucceeded">
2+
<div class="container">
3+
<div class="row" *ngIf="bitstreamRD?.hasSucceeded">
44
<div class="col-md-2">
55
<ds-thumbnail [thumbnail]="bitstreamRD?.payload"></ds-thumbnail>
66
</div>
@@ -27,7 +27,7 @@ <h1 class="h2">{{dsoNameService.getName(bitstreamRD?.payload)}} <span class="tex
2727
</div>
2828
</div>
2929
<ds-error *ngIf="bitstreamRD?.hasFailed" message="{{'error.bitstream' | translate}}"></ds-error>
30-
<ds-loading *ngIf="!bitstreamRD || !formatsRD || bitstreamRD?.isLoading || formatsRD?.isLoading"
30+
<ds-loading *ngIf="!bitstreamRD || bitstreamRD?.isLoading"
3131
message="{{'loading.bitstream' | translate}}"></ds-loading>
3232
</div>
3333
</ng-container>

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ describe('EditBitstreamPageComponent', () => {
261261
});
262262

263263
it('should select the correct format', () => {
264-
expect(rawForm.formatContainer.selectedFormat).toEqual(selectedFormat.id);
264+
expect(rawForm.formatContainer.selectedFormat).toEqual(selectedFormat.shortDescription);
265265
});
266266

267267
it('should put the \"New Format\" input on invisible', () => {
@@ -292,7 +292,13 @@ describe('EditBitstreamPageComponent', () => {
292292

293293
describe('when an unknown format is selected', () => {
294294
beforeEach(() => {
295-
comp.updateNewFormatLayout(allFormats[0].id);
295+
comp.onChange({
296+
model: {
297+
id: 'selectedFormat',
298+
value: allFormats[0],
299+
},
300+
});
301+
comp.updateNewFormatLayout();
296302
});
297303

298304
it('should remove the invisible class from the \"New Format\" input', () => {
@@ -394,9 +400,10 @@ describe('EditBitstreamPageComponent', () => {
394400

395401
describe('when selected format has changed', () => {
396402
beforeEach(() => {
397-
comp.formGroup.patchValue({
398-
formatContainer: {
399-
selectedFormat: allFormats[2].id,
403+
comp.onChange({
404+
model: {
405+
id: 'selectedFormat',
406+
value: allFormats[2],
400407
},
401408
});
402409
fixture.detectChanges();

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts

Lines changed: 54 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
DynamicFormLayout,
2222
DynamicFormService,
2323
DynamicInputModel,
24-
DynamicSelectModel,
2524
} from '@ng-dynamic-forms/core';
2625
import {
2726
TranslateModule,
@@ -31,24 +30,22 @@ import cloneDeep from 'lodash/cloneDeep';
3130
import {
3231
combineLatest,
3332
combineLatest as observableCombineLatest,
34-
EMPTY,
3533
Observable,
3634
of as observableOf,
3735
Subscription,
3836
} from 'rxjs';
3937
import {
40-
expand,
4138
filter,
4239
map,
43-
reduce,
4440
switchMap,
41+
take,
4542
tap,
4643
} from 'rxjs/operators';
4744

4845
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
46+
import { FindAllDataImpl } from '../../core/data/base/find-all-data';
4947
import { BitstreamDataService } from '../../core/data/bitstream-data.service';
5048
import { BitstreamFormatDataService } from '../../core/data/bitstream-format-data.service';
51-
import { PaginatedList } from '../../core/data/paginated-list.model';
5249
import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service';
5350
import { RemoteData } from '../../core/data/remote-data';
5451
import { Bitstream } from '../../core/shared/bitstream.model';
@@ -58,7 +55,6 @@ import { Bundle } from '../../core/shared/bundle.model';
5855
import { Item } from '../../core/shared/item.model';
5956
import { Metadata } from '../../core/shared/metadata.utils';
6057
import {
61-
getAllSucceededRemoteDataPayload,
6258
getFirstCompletedRemoteData,
6359
getFirstSucceededRemoteData,
6460
getFirstSucceededRemoteDataPayload,
@@ -75,6 +71,7 @@ import { ErrorComponent } from '../../shared/error/error.component';
7571
import { DynamicCustomSwitchModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/custom-switch/custom-switch.model';
7672
import { DsDynamicInputModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model';
7773
import { 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';
7875
import { FormComponent } from '../../shared/form/form.component';
7976
import { ThemedLoadingComponent } from '../../shared/loading/themed-loading.component';
8077
import { 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
}

src/app/shared/form/builder/ds-dynamic-form-ui/models/scrollable-dropdown/dynamic-scrollable-dropdown.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
<button class="dropdown-item collection-item text-truncate" *ngFor="let listEntry of optionsList; let i = index"
5151
[class.active]="i === selectedIndex"
5252
(keydown.enter)="onSelect(listEntry); sdRef.close()" (mousedown)="onSelect(listEntry); sdRef.close()"
53-
title="{{ listEntry.display }}" role="option"
54-
[attr.id]="listEntry.display === (currentValue|async) ? ('combobox_' + id + '_selected') : null">
53+
title="{{ inputFormatter(listEntry) }}" role="option"
54+
[attr.id]="inputFormatter(listEntry) === (currentValue|async) ? ('combobox_' + id + '_selected') : null">
5555
{{inputFormatter(listEntry)}}
5656
</button>
5757
<div class="scrollable-dropdown-loading text-center" *ngIf="loading"><p>{{'form.loading' | translate}}</p></div>

0 commit comments

Comments
 (0)