116728: Removed unnecessary *ngVars from the ThumbnailComponent

This commit is contained in:
Alexandre Vryghem
2024-07-26 13:57:47 +02:00
committed by Tim Donohue
parent 7f1ddcc41c
commit 64596c306e
3 changed files with 30 additions and 35 deletions

View File

@@ -1,4 +1,4 @@
<div class="thumbnail" [class.limit-width]="limitWidth" *ngVar="(isLoading$ | async) as isLoading"> <div class="thumbnail" [class.limit-width]="limitWidth">
<div *ngIf="isLoading" class="thumbnail-content outer"> <div *ngIf="isLoading" class="thumbnail-content outer">
<div class="inner"> <div class="inner">
<div class="centered"> <div class="centered">
@@ -6,7 +6,6 @@
</div> </div>
</div> </div>
</div> </div>
<ng-container *ngVar="(src$ | async) as src">
<!-- don't use *ngIf="!isLoading" so the thumbnail can load in while the animation is playing --> <!-- don't use *ngIf="!isLoading" so the thumbnail can load in while the animation is playing -->
<img *ngIf="src !== null" class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading}" <img *ngIf="src !== null" class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading}"
[src]="src | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()"> [src]="src | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
@@ -17,5 +16,4 @@
</div> </div>
</div> </div>
</div> </div>
</ng-container>
</div> </div>

View File

@@ -96,31 +96,31 @@ describe('ThumbnailComponent', () => {
describe('loading', () => { describe('loading', () => {
it('should start out with isLoading$ true', () => { it('should start out with isLoading$ true', () => {
expect(comp.isLoading$.getValue()).toBeTrue(); expect(comp.isLoading).toBeTrue();
}); });
it('should set isLoading$ to false once an image is successfully loaded', () => { it('should set isLoading$ to false once an image is successfully loaded', () => {
comp.setSrc('http://bit.stream'); comp.setSrc('http://bit.stream');
fixture.debugElement.query(By.css('img.thumbnail-content')).triggerEventHandler('load', new Event('load')); fixture.debugElement.query(By.css('img.thumbnail-content')).triggerEventHandler('load', new Event('load'));
expect(comp.isLoading$.getValue()).toBeFalse(); expect(comp.isLoading).toBeFalse();
}); });
it('should set isLoading$ to false once the src is set to null', () => { it('should set isLoading$ to false once the src is set to null', () => {
comp.setSrc(null); comp.setSrc(null);
expect(comp.isLoading$.getValue()).toBeFalse(); expect(comp.isLoading).toBeFalse();
}); });
it('should show a loading animation while isLoading$ is true', () => { it('should show a loading animation while isLoading$ is true', () => {
expect(de.query(By.css('ds-loading'))).toBeTruthy(); expect(de.query(By.css('ds-loading'))).toBeTruthy();
comp.isLoading$.next(false); comp.isLoading = false;
fixture.detectChanges(); fixture.detectChanges();
expect(fixture.debugElement.query(By.css('ds-loading'))).toBeFalsy(); expect(fixture.debugElement.query(By.css('ds-loading'))).toBeFalsy();
}); });
describe('with a thumbnail image', () => { describe('with a thumbnail image', () => {
beforeEach(() => { beforeEach(() => {
comp.src$.next('https://bit.stream'); comp.src = 'https://bit.stream';
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -129,7 +129,7 @@ describe('ThumbnailComponent', () => {
expect(img).toBeTruthy(); expect(img).toBeTruthy();
expect(img.classes['d-none']).toBeTrue(); expect(img.classes['d-none']).toBeTrue();
comp.isLoading$.next(false); comp.isLoading = false;
fixture.detectChanges(); fixture.detectChanges();
img = fixture.debugElement.query(By.css('img.thumbnail-content')); img = fixture.debugElement.query(By.css('img.thumbnail-content'));
expect(img).toBeTruthy(); expect(img).toBeTruthy();
@@ -140,14 +140,14 @@ describe('ThumbnailComponent', () => {
describe('without a thumbnail image', () => { describe('without a thumbnail image', () => {
beforeEach(() => { beforeEach(() => {
comp.src$.next(null); comp.src = null;
fixture.detectChanges(); fixture.detectChanges();
}); });
it('should only show the HTML placeholder once done loading', () => { it('should only show the HTML placeholder once done loading', () => {
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeFalsy(); expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeFalsy();
comp.isLoading$.next(false); comp.isLoading = false;
fixture.detectChanges(); fixture.detectChanges();
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeTruthy(); expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeTruthy();
}); });
@@ -243,14 +243,14 @@ describe('ThumbnailComponent', () => {
describe('fallback', () => { describe('fallback', () => {
describe('if there is a default image', () => { describe('if there is a default image', () => {
it('should display the default image', () => { it('should display the default image', () => {
comp.src$.next('http://bit.stream'); comp.src = 'http://bit.stream';
comp.defaultImage = 'http://default.img'; comp.defaultImage = 'http://default.img';
comp.errorHandler(); comp.errorHandler();
expect(comp.src$.getValue()).toBe(comp.defaultImage); expect(comp.src).toBe(comp.defaultImage);
}); });
it('should include the alt text', () => { it('should include the alt text', () => {
comp.src$.next('http://bit.stream'); comp.src = 'http://bit.stream';
comp.defaultImage = 'http://default.img'; comp.defaultImage = 'http://default.img';
comp.errorHandler(); comp.errorHandler();
@@ -262,10 +262,10 @@ describe('ThumbnailComponent', () => {
describe('if there is no default image', () => { describe('if there is no default image', () => {
it('should display the HTML placeholder', () => { it('should display the HTML placeholder', () => {
comp.src$.next('http://default.img'); comp.src = 'http://default.img';
comp.defaultImage = null; comp.defaultImage = null;
comp.errorHandler(); comp.errorHandler();
expect(comp.src$.getValue()).toBe(null); expect(comp.src).toBe(null);
fixture.detectChanges(); fixture.detectChanges();
const placeholder = fixture.debugElement.query(By.css('div.thumbnail-placeholder')).nativeElement; const placeholder = fixture.debugElement.query(By.css('div.thumbnail-placeholder')).nativeElement;
@@ -357,7 +357,7 @@ describe('ThumbnailComponent', () => {
it('should show the default image', () => { it('should show the default image', () => {
comp.defaultImage = 'default/image.jpg'; comp.defaultImage = 'default/image.jpg';
comp.ngOnChanges({}); comp.ngOnChanges({});
expect(comp.src$.getValue()).toBe('default/image.jpg'); expect(comp.src).toBe('default/image.jpg');
}); });
}); });
}); });

View File

@@ -6,10 +6,7 @@ import {
SimpleChanges, SimpleChanges,
} from '@angular/core'; } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { import { of as observableOf } from 'rxjs';
BehaviorSubject,
of as observableOf,
} from 'rxjs';
import { switchMap } from 'rxjs/operators'; import { switchMap } from 'rxjs/operators';
import { AuthService } from '../core/auth/auth.service'; import { AuthService } from '../core/auth/auth.service';
@@ -53,7 +50,7 @@ export class ThumbnailComponent implements OnChanges {
/** /**
* The src attribute used in the template to render the image. * The src attribute used in the template to render the image.
*/ */
src$ = new BehaviorSubject<string>(undefined); src: string = undefined;
retriedWithToken = false; retriedWithToken = false;
@@ -76,7 +73,7 @@ export class ThumbnailComponent implements OnChanges {
* Whether the thumbnail is currently loading * Whether the thumbnail is currently loading
* Start out as true to avoid flashing the alt text while a thumbnail is being loaded. * Start out as true to avoid flashing the alt text while a thumbnail is being loaded.
*/ */
isLoading$ = new BehaviorSubject(true); isLoading = true;
constructor( constructor(
protected auth: AuthService, protected auth: AuthService,
@@ -129,7 +126,7 @@ export class ThumbnailComponent implements OnChanges {
* Otherwise, fall back to the default image or a HTML placeholder * Otherwise, fall back to the default image or a HTML placeholder
*/ */
errorHandler() { errorHandler() {
const src = this.src$.getValue(); const src = this.src;
const thumbnail = this.bitstream; const thumbnail = this.bitstream;
const thumbnailSrc = thumbnail?._links?.content?.href; const thumbnailSrc = thumbnail?._links?.content?.href;
@@ -181,9 +178,9 @@ export class ThumbnailComponent implements OnChanges {
* @param src * @param src
*/ */
setSrc(src: string): void { setSrc(src: string): void {
this.src$.next(src); this.src = src;
if (src === null) { if (src === null) {
this.isLoading$.next(false); this.isLoading = false;
} }
} }
@@ -191,6 +188,6 @@ export class ThumbnailComponent implements OnChanges {
* Stop the loading animation once the thumbnail is successfully loaded * Stop the loading animation once the thumbnail is successfully loaded
*/ */
successHandler() { successHandler() {
this.isLoading$.next(false); this.isLoading = false;
} }
} }