Merge branch 'fix-embargoed-thumbnails_contribute-8.1'

This commit is contained in:
Art Lowel
2025-04-25 17:58:06 +02:00
3 changed files with 35 additions and 29 deletions

View File

@@ -1,5 +1,5 @@
<div class="thumbnail" [class.limit-width]="limitWidth"> <div class="thumbnail" [class.limit-width]="limitWidth">
@if (isLoading) { @if (isLoading$ | async) {
<div class="thumbnail-content outer"> <div class="thumbnail-content outer">
<div class="inner"> <div class="inner">
<div class="centered"> <div class="centered">
@@ -9,11 +9,11 @@
</div> </div>
} }
<!-- 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 -->
@if (src !== null) { @if ((src$ | async) !== null) {
<img class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading}" <img class="thumbnail-content img-fluid" [ngClass]="{'d-none': (isLoading$ | async)}"
[src]="src | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()"> [src]="(src$ | async) | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
} }
@if (src === null && !isLoading) { @if ((src$ | async) === null && (isLoading$ | async) === false) {
<div class="thumbnail-content outer"> <div class="thumbnail-content outer">
<div class="inner"> <div class="inner">
<div class="thumbnail-placeholder centered lead"> <div class="thumbnail-placeholder centered lead">

View File

@@ -100,31 +100,31 @@ describe('ThumbnailComponent', () => {
describe('loading', () => { describe('loading', () => {
it('should start out with isLoading$ true', () => { it('should start out with isLoading$ true', () => {
expect(comp.isLoading).toBeTrue(); expect(comp.isLoading$.getValue()).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).toBeFalse(); expect(comp.isLoading$.getValue()).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).toBeFalse(); expect(comp.isLoading$.getValue()).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 = false; comp.isLoading$.next(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 = 'https://bit.stream'; comp.src$.next('https://bit.stream');
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -133,7 +133,7 @@ describe('ThumbnailComponent', () => {
expect(img).toBeTruthy(); expect(img).toBeTruthy();
expect(img.classes['d-none']).toBeTrue(); expect(img.classes['d-none']).toBeTrue();
comp.isLoading = false; comp.isLoading$.next(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();
@@ -144,14 +144,14 @@ describe('ThumbnailComponent', () => {
describe('without a thumbnail image', () => { describe('without a thumbnail image', () => {
beforeEach(() => { beforeEach(() => {
comp.src = null; comp.src$.next(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 = false; comp.isLoading$.next(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();
}); });
@@ -247,14 +247,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 = 'http://bit.stream'; comp.src$.next('http://bit.stream');
comp.defaultImage = 'http://default.img'; comp.defaultImage = 'http://default.img';
comp.errorHandler(); comp.errorHandler();
expect(comp.src).toBe(comp.defaultImage); expect(comp.src$.getValue()).toBe(comp.defaultImage);
}); });
it('should include the alt text', () => { it('should include the alt text', () => {
comp.src = 'http://bit.stream'; comp.src$.next('http://bit.stream');
comp.defaultImage = 'http://default.img'; comp.defaultImage = 'http://default.img';
comp.errorHandler(); comp.errorHandler();
@@ -266,10 +266,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 = 'http://default.img'; comp.src$.next('http://default.img');
comp.defaultImage = null; comp.defaultImage = null;
comp.errorHandler(); comp.errorHandler();
expect(comp.src).toBe(null); expect(comp.src$.getValue()).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;
@@ -363,7 +363,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).toBe('default/image.jpg'); expect(comp.src$.getValue()).toBe('default/image.jpg');
}); });
}); });
}); });
@@ -419,7 +419,7 @@ describe('ThumbnailComponent', () => {
}); });
it('should start out with isLoading$ true', () => { it('should start out with isLoading$ true', () => {
expect(comp.isLoading).toBeTrue(); expect(comp.isLoading$.getValue()).toBeTrue();
expect(de.query(By.css('ds-loading'))).toBeTruthy(); expect(de.query(By.css('ds-loading'))).toBeTruthy();
}); });

View File

@@ -11,7 +11,7 @@ import {
SimpleChanges, SimpleChanges,
} from '@angular/core'; } from '@angular/core';
import { TranslateModule } from '@ngx-translate/core'; import { TranslateModule } from '@ngx-translate/core';
import { of as observableOf } from 'rxjs'; import { of as observableOf, BehaviorSubject } 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';
@@ -26,7 +26,6 @@ import {
} from '../shared/empty.util'; } from '../shared/empty.util';
import { ThemedLoadingComponent } from '../shared/loading/themed-loading.component'; import { ThemedLoadingComponent } from '../shared/loading/themed-loading.component';
import { SafeUrlPipe } from '../shared/utils/safe-url-pipe'; import { SafeUrlPipe } from '../shared/utils/safe-url-pipe';
import { VarDirective } from '../shared/utils/var.directive';
/** /**
* This component renders a given Bitstream as a thumbnail. * This component renders a given Bitstream as a thumbnail.
@@ -38,7 +37,7 @@ import { VarDirective } from '../shared/utils/var.directive';
styleUrls: ['./thumbnail.component.scss'], styleUrls: ['./thumbnail.component.scss'],
templateUrl: './thumbnail.component.html', templateUrl: './thumbnail.component.html',
standalone: true, standalone: true,
imports: [VarDirective, CommonModule, ThemedLoadingComponent, TranslateModule, SafeUrlPipe], imports: [CommonModule, ThemedLoadingComponent, TranslateModule, SafeUrlPipe],
}) })
export class ThumbnailComponent implements OnChanges { export class ThumbnailComponent implements OnChanges {
/** /**
@@ -55,7 +54,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: string = undefined; src$: BehaviorSubject<string> = new BehaviorSubject<string>(undefined);
retriedWithToken = false; retriedWithToken = false;
@@ -78,7 +77,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 = true; isLoading$: BehaviorSubject<boolean> = new BehaviorSubject(true);
constructor( constructor(
@Inject(PLATFORM_ID) private platformID: any, @Inject(PLATFORM_ID) private platformID: any,
@@ -94,6 +93,13 @@ export class ThumbnailComponent implements OnChanges {
*/ */
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (isPlatformBrowser(this.platformID)) { if (isPlatformBrowser(this.platformID)) {
// every time the inputs change we need to start the loading animation again, as it's possible
// that thumbnail is first set to null when the parent component initializes and then set to
// the actual value
if (this.isLoading$.getValue() === false) {
this.isLoading$.next(true);
}
if (hasNoValue(this.thumbnail)) { if (hasNoValue(this.thumbnail)) {
this.setSrc(this.defaultImage); this.setSrc(this.defaultImage);
return; return;
@@ -134,7 +140,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; const src = this.src$.getValue();
const thumbnail = this.bitstream; const thumbnail = this.bitstream;
const thumbnailSrc = thumbnail?._links?.content?.href; const thumbnailSrc = thumbnail?._links?.content?.href;
@@ -186,9 +192,9 @@ export class ThumbnailComponent implements OnChanges {
* @param src * @param src
*/ */
setSrc(src: string): void { setSrc(src: string): void {
this.src = src; this.src$.next(src);
if (src === null) { if (src === null) {
this.isLoading = false; this.isLoading$.next(false);
} }
} }
@@ -196,6 +202,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 = false; this.isLoading$.next(false);
} }
} }