Skip to content

Commit 54ed550

Browse files
committed
Merge branch 'w2p-116728_removed-unnecessary-ngvars_contribute-7.6' into fix-embargoed-thumbnails_contribute-8.1
2 parents b9ff51d + 11f2517 commit 54ed550

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

src/app/thumbnail/thumbnail.component.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<div class="thumbnail" [class.limit-width]="limitWidth">
2-
<div *ngIf="isLoading" class="thumbnail-content outer">
2+
<div *ngIf="(isLoading$ | async)" class="thumbnail-content outer">
33
<div class="inner">
44
<div class="centered">
55
<ds-loading [spinner]="true"></ds-loading>
66
</div>
77
</div>
88
</div>
9-
<!-- don't use *ngIf="!isLoading" so the thumbnail can load in while the animation is playing -->
10-
<img *ngIf="src !== null" class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading}"
11-
[src]="src | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
12-
<div *ngIf="src === null && !isLoading" class="thumbnail-content outer">
9+
<!-- don't use *ngIf="!(isLoading$ | async)" so the thumbnail can load in while the animation is playing -->
10+
<img *ngIf="(src$ | async) !== null" class="thumbnail-content img-fluid" [ngClass]="{'d-none': (isLoading$ | async)}"
11+
[src]="(src$ | async) | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
12+
<div *ngIf="(src$ | async) === null && (isLoading$ | async) === false" class="thumbnail-content outer">
1313
<div class="inner">
1414
<div class="thumbnail-placeholder centered lead">
1515
{{ placeholder | translate }}

src/app/thumbnail/thumbnail.component.spec.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,31 +100,31 @@ describe('ThumbnailComponent', () => {
100100

101101
describe('loading', () => {
102102
it('should start out with isLoading$ true', () => {
103-
expect(comp.isLoading).toBeTrue();
103+
expect(comp.isLoading$.getValue()).toBeTrue();
104104
});
105105

106106
it('should set isLoading$ to false once an image is successfully loaded', () => {
107107
comp.setSrc('http://bit.stream');
108108
fixture.debugElement.query(By.css('img.thumbnail-content')).triggerEventHandler('load', new Event('load'));
109-
expect(comp.isLoading).toBeFalse();
109+
expect(comp.isLoading$.getValue()).toBeFalse();
110110
});
111111

112112
it('should set isLoading$ to false once the src is set to null', () => {
113113
comp.setSrc(null);
114-
expect(comp.isLoading).toBeFalse();
114+
expect(comp.isLoading$.getValue()).toBeFalse();
115115
});
116116

117117
it('should show a loading animation while isLoading$ is true', () => {
118118
expect(de.query(By.css('ds-loading'))).toBeTruthy();
119119

120-
comp.isLoading = false;
120+
comp.isLoading$.next(false);
121121
fixture.detectChanges();
122122
expect(fixture.debugElement.query(By.css('ds-loading'))).toBeFalsy();
123123
});
124124

125125
describe('with a thumbnail image', () => {
126126
beforeEach(() => {
127-
comp.src = 'https://bit.stream';
127+
comp.src$.next('https://bit.stream');
128128
fixture.detectChanges();
129129
});
130130

@@ -133,7 +133,7 @@ describe('ThumbnailComponent', () => {
133133
expect(img).toBeTruthy();
134134
expect(img.classes['d-none']).toBeTrue();
135135

136-
comp.isLoading = false;
136+
comp.isLoading$.next(false);
137137
fixture.detectChanges();
138138
img = fixture.debugElement.query(By.css('img.thumbnail-content'));
139139
expect(img).toBeTruthy();
@@ -144,14 +144,14 @@ describe('ThumbnailComponent', () => {
144144

145145
describe('without a thumbnail image', () => {
146146
beforeEach(() => {
147-
comp.src = null;
147+
comp.src$.next(null);
148148
fixture.detectChanges();
149149
});
150150

151151
it('should only show the HTML placeholder once done loading', () => {
152152
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeFalsy();
153153

154-
comp.isLoading = false;
154+
comp.isLoading$.next(false);
155155
fixture.detectChanges();
156156
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeTruthy();
157157
});
@@ -247,14 +247,14 @@ describe('ThumbnailComponent', () => {
247247
describe('fallback', () => {
248248
describe('if there is a default image', () => {
249249
it('should display the default image', () => {
250-
comp.src = 'http://bit.stream';
250+
comp.src$.next('http://bit.stream');
251251
comp.defaultImage = 'http://default.img';
252252
comp.errorHandler();
253-
expect(comp.src).toBe(comp.defaultImage);
253+
expect(comp.src$.getValue()).toBe(comp.defaultImage);
254254
});
255255

256256
it('should include the alt text', () => {
257-
comp.src = 'http://bit.stream';
257+
comp.src$.next('http://bit.stream');
258258
comp.defaultImage = 'http://default.img';
259259
comp.errorHandler();
260260

@@ -266,10 +266,10 @@ describe('ThumbnailComponent', () => {
266266

267267
describe('if there is no default image', () => {
268268
it('should display the HTML placeholder', () => {
269-
comp.src = 'http://default.img';
269+
comp.src$.next('http://default.img');
270270
comp.defaultImage = null;
271271
comp.errorHandler();
272-
expect(comp.src).toBe(null);
272+
expect(comp.src$.getValue()).toBe(null);
273273

274274
fixture.detectChanges();
275275
const placeholder = fixture.debugElement.query(By.css('div.thumbnail-placeholder')).nativeElement;
@@ -361,7 +361,7 @@ describe('ThumbnailComponent', () => {
361361
it('should show the default image', () => {
362362
comp.defaultImage = 'default/image.jpg';
363363
comp.ngOnChanges({});
364-
expect(comp.src).toBe('default/image.jpg');
364+
expect(comp.src$.getValue()).toBe('default/image.jpg');
365365
});
366366
});
367367
});
@@ -417,7 +417,7 @@ describe('ThumbnailComponent', () => {
417417
});
418418

419419
it('should start out with isLoading$ true', () => {
420-
expect(comp.isLoading).toBeTrue();
420+
expect(comp.isLoading$.getValue()).toBeTrue();
421421
expect(de.query(By.css('ds-loading'))).toBeTruthy();
422422
});
423423

src/app/thumbnail/thumbnail.component.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {
1111
SimpleChanges,
1212
} from '@angular/core';
1313
import { TranslateModule } from '@ngx-translate/core';
14-
import { of as observableOf } from 'rxjs';
14+
import {
15+
BehaviorSubject,
16+
of as observableOf,
17+
} from 'rxjs';
1518
import { switchMap } from 'rxjs/operators';
1619

1720
import { AuthService } from '../core/auth/auth.service';
@@ -55,7 +58,7 @@ export class ThumbnailComponent implements OnChanges {
5558
/**
5659
* The src attribute used in the template to render the image.
5760
*/
58-
src: string = undefined;
61+
src$: BehaviorSubject<string> = new BehaviorSubject<string>(undefined);
5962

6063
retriedWithToken = false;
6164

@@ -78,7 +81,7 @@ export class ThumbnailComponent implements OnChanges {
7881
* Whether the thumbnail is currently loading
7982
* Start out as true to avoid flashing the alt text while a thumbnail is being loaded.
8083
*/
81-
isLoading = true;
84+
isLoading$: BehaviorSubject<boolean> = new BehaviorSubject(true);
8285

8386
constructor(
8487
@Inject(PLATFORM_ID) private platformID: any,
@@ -94,6 +97,13 @@ export class ThumbnailComponent implements OnChanges {
9497
*/
9598
ngOnChanges(changes: SimpleChanges): void {
9699
if (isPlatformBrowser(this.platformID)) {
100+
// every time the inputs change we need to start the loading animation again, as it's possible
101+
// that thumbnail is first set to null when the parent component initializes and then set to
102+
// the actual value
103+
if (this.isLoading$.getValue() === false) {
104+
this.isLoading$.next(true);
105+
}
106+
97107
if (hasNoValue(this.thumbnail)) {
98108
this.setSrc(this.defaultImage);
99109
return;
@@ -134,7 +144,7 @@ export class ThumbnailComponent implements OnChanges {
134144
* Otherwise, fall back to the default image or a HTML placeholder
135145
*/
136146
errorHandler() {
137-
const src = this.src;
147+
const src = this.src$.getValue();
138148
const thumbnail = this.bitstream;
139149
const thumbnailSrc = thumbnail?._links?.content?.href;
140150

@@ -186,16 +196,16 @@ export class ThumbnailComponent implements OnChanges {
186196
* @param src
187197
*/
188198
setSrc(src: string): void {
189-
this.src = src;
199+
this.src$.next(src);
190200
if (src === null) {
191-
this.isLoading = false;
201+
this.isLoading$.next(false);
192202
}
193203
}
194204

195205
/**
196206
* Stop the loading animation once the thumbnail is successfully loaded
197207
*/
198208
successHandler() {
199-
this.isLoading = false;
209+
this.isLoading$.next(false);
200210
}
201211
}

0 commit comments

Comments
 (0)