From 4f8a1ac08bbb77e2730494dcfecf240463e33c20 Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Tue, 26 Nov 2024 19:34:53 +0100 Subject: [PATCH] [DSC-2052] Refactoring bitstream-attachment.component in order to fix rendering issue with functions called inside the template --- .../attachment-render.component.ts | 34 +++---- .../bitstream-attachment.component.html | 92 ++++++++++--------- .../bitstream-attachment.component.spec.ts | 24 ++++- .../bitstream-attachment.component.ts | 37 ++++++-- .../bitstream-rendering-model.ts | 6 +- 5 files changed, 117 insertions(+), 76 deletions(-) diff --git a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/attachment-render/attachment-render.component.ts b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/attachment-render/attachment-render.component.ts index cc57b8910dc..a7d99d3ba8c 100644 --- a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/attachment-render/attachment-render.component.ts +++ b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/attachment-render/attachment-render.component.ts @@ -1,8 +1,7 @@ import { Component, - ComponentFactory, - ComponentFactoryResolver, ComponentRef, + inject, Injector, Input, OnInit, @@ -53,12 +52,7 @@ export class AttachmentRenderComponent implements OnInit { read: ViewContainerRef, }) attachmentValueViewRef: ViewContainerRef; - constructor( - protected componentFactoryResolver: ComponentFactoryResolver, - private injector: Injector, - ) { - - } + private injector: Injector = inject(Injector); ngOnInit(): void { this.attachmentValueViewRef.clear(); @@ -68,26 +62,28 @@ export class AttachmentRenderComponent implements OnInit { /** * Generate ComponentFactory for attachment rendering */ - computeComponentFactory(): ComponentFactory { + computeComponentFactory(): GenericConstructor { const rendering = this.computeRendering(); const attachmentTypeRenderOptions = getAttachmentTypeRendering(rendering); - const constructor: GenericConstructor = attachmentTypeRenderOptions?.componentRef; - return constructor ? this.componentFactoryResolver.resolveComponentFactory(constructor) : null; + return attachmentTypeRenderOptions?.componentRef || null; } /** * Generate ComponentRef for attachment rendering */ generateComponentRef(): ComponentRef { - let attachentComponentRef: ComponentRef; - const factory: ComponentFactory = this.computeComponentFactory(); - if (factory) { - attachentComponentRef = this.attachmentValueViewRef.createComponent(factory, 0, this.getComponentInjector()); - (attachentComponentRef.instance as any).item = this.item; - (attachentComponentRef.instance as any).bitstream = this.bitstream; - (attachentComponentRef.instance as any).tabName = this.tabName; + let attachmentComponentRef: ComponentRef; + const component: GenericConstructor = this.computeComponentFactory(); + if (component) { + attachmentComponentRef = this.attachmentValueViewRef.createComponent(component, { + index: 0, + injector: this.getComponentInjector(), + }); + (attachmentComponentRef.instance as any).item = this.item; + (attachmentComponentRef.instance as any).bitstream = this.bitstream; + (attachmentComponentRef.instance as any).tabName = this.tabName; } - return attachentComponentRef; + return attachmentComponentRef; } /** diff --git a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.html b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.html index 4516c99d1f8..ea79887de4a 100644 --- a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.html +++ b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.html @@ -1,4 +1,4 @@ - +@if (attachment) {
@@ -7,11 +7,11 @@
- + @for (renderingType of allAttachmentProviders; track $index) {
-
+ }
@@ -20,56 +20,60 @@
- + @for (attachmentConf of envMetadata; track $index) { + @if (attachment.firstMetadataValue(attachmentConf.name) || attachmentConf.type === AdvancedAttachmentElementType.Attribute) { +
+ {{ 'cris-layout.advanced-attachment.' + attachmentConf.name | translate }} -
- {{ 'cris-layout.advanced-attachment.'+attachmentConf.name | translate }} + @if (attachmentConf.type === AdvancedAttachmentElementType.Metadata) { - - -

- {{attachment.firstMetadataValue(attachmentConf.name) | titlecase}} -

- -

- {{attachment.firstMetadataValue(attachmentConf.name)}} -

+

+ {{attachment.firstMetadataValue(attachmentConf.name) | titlecase}} +

- - +

{{attachment.firstMetadataValue(attachmentConf.name)}} - - - - - - - -

- {{'cris-layout.advanced-attachment.label.not-present' | translate}}

-

{{getFormat(attachment) | async}}

-
- -

{{getSize(attachment) | dsFileSize}}

-
+ @if (attachmentConf.truncatable) { + + + {{attachment.firstMetadataValue(attachmentConf.name)}} + + + } + } + @if (attachmentConf.type === AdvancedAttachmentElementType.Attribute) { + @if (attachmentConf.name === 'format') { + @if ((bitstreamFormat$ | async) === null || (bitstreamFormat$ | async) === undefined) { +

+ {{'cris-layout.advanced-attachment.label.not-present' | translate}} +

+ } @else { +

{{(bitstreamFormat$ | async)}}

+ } + } - -

- {{'cris-layout.advanced-attachment.label.not-present' | translate}} -

-

({{getChecksum(attachment).checkSumAlgorithm}}):{{ getChecksum(attachment).value }}

-
- + @if (attachmentConf.name === 'size' && bitstreamSize) { +

{{bitstreamSize | dsFileSize}}

+ } -
- + @if (attachmentConf.name === 'checksum') { + @if (checksumInfo?.value === null || checksumInfo?.value === undefined) { +

+ {{'cris-layout.advanced-attachment.label.not-present' | translate}} +

+ } @else { +

({{checksumInfo.checkSumAlgorithm}}):{{ checksumInfo.value }}

+ } + } + } + +
+ } + }
- +} diff --git a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.spec.ts b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.spec.ts index 4585eed4f83..eb440cd8b42 100644 --- a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.spec.ts +++ b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.spec.ts @@ -3,13 +3,19 @@ import { ComponentFixture, TestBed, } from '@angular/core/testing'; -import { RouterTestingModule } from '@angular/router/testing'; -import { TranslateService } from '@ngx-translate/core'; +import { provideRouter } from '@angular/router'; +import { + TranslateLoader, + TranslateModule, +} from '@ngx-translate/core'; import { BitstreamDataService } from '../../../../../../../../core/data/bitstream-data.service'; +import { MockBitstream1 } from '../../../../../../../../shared/mocks/item.mock'; +import { TranslateLoaderMock } from '../../../../../../../../shared/mocks/translate-loader.mock'; import { TruncatableComponent } from '../../../../../../../../shared/truncatable/truncatable.component'; import { TruncatablePartComponent } from '../../../../../../../../shared/truncatable/truncatable-part/truncatable-part.component'; import { ThemedThumbnailComponent } from '../../../../../../../../thumbnail/themed-thumbnail.component'; +import { BitstreamRenderingModelComponent } from '../../bitstream-rendering-model'; import { AttachmentRenderComponent } from './attachment-render/attachment-render.component'; import { BitstreamAttachmentComponent } from './bitstream-attachment.component'; @@ -19,14 +25,23 @@ describe('BitstreamAttachmentComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [RouterTestingModule.withRoutes([]), BitstreamAttachmentComponent], + imports: [ + BitstreamAttachmentComponent, + BitstreamRenderingModelComponent, + TranslateModule.forRoot({ + loader: { + provide: TranslateLoader, + useClass: TranslateLoaderMock, + }, + }), + ], providers: [ + provideRouter([]), { provide: 'fieldProvider', useValue: {} }, { provide: 'itemProvider', useValue: {} }, { provide: 'renderingSubTypeProvider', useValue: '' }, { provide: 'tabNameProvider', useValue: '' }, { provide: BitstreamDataService, useValue: {} }, - { provide: TranslateService, useValue: {} }, ], schemas: [NO_ERRORS_SCHEMA], }) @@ -36,6 +51,7 @@ describe('BitstreamAttachmentComponent', () => { beforeEach(() => { fixture = TestBed.createComponent(BitstreamAttachmentComponent); component = fixture.componentInstance; + component.attachment = MockBitstream1; fixture.detectChanges(); }); diff --git a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.ts b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.ts index 505ec34b80c..f7ad2fa137e 100644 --- a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.ts +++ b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/advanced-attachment/bitstream-attachment/bitstream-attachment.component.ts @@ -1,7 +1,5 @@ import { AsyncPipe, - NgFor, - NgIf, TitleCasePipe, } from '@angular/common'; import { @@ -18,12 +16,16 @@ import { TranslateModule, TranslateService, } from '@ngx-translate/core'; +import { Observable } from 'rxjs'; import { AdvancedAttachmentElementType } from '../../../../../../../../../config/advanced-attachment-rendering.config'; import { environment } from '../../../../../../../../../environments/environment'; import { BitstreamDataService } from '../../../../../../../../core/data/bitstream-data.service'; import { LayoutField } from '../../../../../../../../core/layout/models/box.model'; -import { Bitstream } from '../../../../../../../../core/shared/bitstream.model'; +import { + Bitstream, + ChecksumInfo, +} from '../../../../../../../../core/shared/bitstream.model'; import { Item } from '../../../../../../../../core/shared/item.model'; import { TruncatableComponent } from '../../../../../../../../shared/truncatable/truncatable.component'; import { TruncatablePartComponent } from '../../../../../../../../shared/truncatable/truncatable-part/truncatable-part.component'; @@ -39,9 +41,7 @@ import { AttachmentRenderingType } from './attachment-type.decorator'; styleUrls: ['./bitstream-attachment.component.scss'], standalone: true, imports: [ - NgIf, ThemedThumbnailComponent, - NgFor, AttachmentRenderComponent, TruncatableComponent, TruncatablePartComponent, @@ -63,6 +63,9 @@ export class BitstreamAttachmentComponent extends BitstreamRenderingModelCompone */ AdvancedAttachmentElementType = AdvancedAttachmentElementType; + /** + * Configuration type enum + */ AttachmentRenderingType = AttachmentRenderingType; /** @@ -73,11 +76,26 @@ export class BitstreamAttachmentComponent extends BitstreamRenderingModelCompone /** * Attachment metadata to be displayed in title case */ - attachmentTypeMetadata = 'dc.type'; - @Input() - attachment: Bitstream; + /** + * Attachment to be displayed + */ + @Input() attachment: Bitstream; + + /** + * Format of the bitstream + */ + bitstreamFormat$: Observable; + + /** + * Size of the bitstream + */ + bitstreamSize: number; + /** + * Checksum info of the bitstream + */ + checksumInfo: ChecksumInfo; constructor( @Inject('fieldProvider') public fieldProvider: LayoutField, @@ -94,5 +112,8 @@ export class BitstreamAttachmentComponent extends BitstreamRenderingModelCompone ngOnInit() { this.allAttachmentProviders = this.attachment?.allMetadataValues('bitstream.viewer.provider'); + this.bitstreamFormat$ = this.getFormat(this.attachment); + this.bitstreamSize = this.getSize(this.attachment); + this.checksumInfo = this.getChecksum(this.attachment); } } diff --git a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/bitstream-rendering-model.ts b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/bitstream-rendering-model.ts index b1a5f056dfc..2ff2ed11f3e 100644 --- a/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/bitstream-rendering-model.ts +++ b/src/app/cris-layout/cris-layout-matrix/cris-layout-box-container/boxes/metadata/rendering-types/bitstream-rendering-model.ts @@ -4,7 +4,10 @@ import { } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { Observable } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { + map, + take, +} from 'rxjs/operators'; import { BitstreamDataService, @@ -123,6 +126,7 @@ export abstract class BitstreamRenderingModelComponent extends RenderingTypeStru map((rd: RemoteData) => { return rd.payload?.shortDescription; }), + take(1), ); }