Skip to content

Commit b9c712c

Browse files
authored
Merge pull request #3221 from alexandrevryghem/w2p-116728_removed-unnecessary-ngvars_contribute-main
Removed unnecessary *ngVars from ThumbnailComponent
2 parents 035ae80 + 7d5ae8e commit b9c712c

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
<div class="thumbnail" [class.limit-width]="limitWidth" *ngVar="(isLoading$ | async) as isLoading">
1+
<div class="thumbnail" [class.limit-width]="limitWidth">
22
<div *ngIf="isLoading" 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-
<ng-container *ngVar="(src$ | async) as src">
10-
<!-- don't use *ngIf="!isLoading" so the thumbnail can load in while the animation is playing -->
11-
<img *ngIf="src !== null" class="thumbnail-content img-fluid" [ngClass]="{'d-none': isLoading}"
12-
[src]="src | dsSafeUrl" [alt]="alt | translate" (error)="errorHandler()" (load)="successHandler()">
13-
<div *ngIf="src === null && !isLoading" class="thumbnail-content outer">
14-
<div class="inner">
15-
<div class="thumbnail-placeholder centered lead">
16-
{{ placeholder | translate }}
17-
</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">
13+
<div class="inner">
14+
<div class="thumbnail-placeholder centered lead">
15+
{{ placeholder | translate }}
1816
</div>
1917
</div>
20-
</ng-container>
18+
</div>
2119
</div>

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

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

9797
describe('loading', () => {
9898
it('should start out with isLoading$ true', () => {
99-
expect(comp.isLoading$.getValue()).toBeTrue();
99+
expect(comp.isLoading).toBeTrue();
100100
});
101101

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

108108
it('should set isLoading$ to false once the src is set to null', () => {
109109
comp.setSrc(null);
110-
expect(comp.isLoading$.getValue()).toBeFalse();
110+
expect(comp.isLoading).toBeFalse();
111111
});
112112

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

116-
comp.isLoading$.next(false);
116+
comp.isLoading = false;
117117
fixture.detectChanges();
118118
expect(fixture.debugElement.query(By.css('ds-loading'))).toBeFalsy();
119119
});
120120

121121
describe('with a thumbnail image', () => {
122122
beforeEach(() => {
123-
comp.src$.next('https://bit.stream');
123+
comp.src = 'https://bit.stream';
124124
fixture.detectChanges();
125125
});
126126

@@ -129,7 +129,7 @@ describe('ThumbnailComponent', () => {
129129
expect(img).toBeTruthy();
130130
expect(img.classes['d-none']).toBeTrue();
131131

132-
comp.isLoading$.next(false);
132+
comp.isLoading = false;
133133
fixture.detectChanges();
134134
img = fixture.debugElement.query(By.css('img.thumbnail-content'));
135135
expect(img).toBeTruthy();
@@ -140,14 +140,14 @@ describe('ThumbnailComponent', () => {
140140

141141
describe('without a thumbnail image', () => {
142142
beforeEach(() => {
143-
comp.src$.next(null);
143+
comp.src = null;
144144
fixture.detectChanges();
145145
});
146146

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

150-
comp.isLoading$.next(false);
150+
comp.isLoading = false;
151151
fixture.detectChanges();
152152
expect(fixture.debugElement.query(By.css('div.thumbnail-placeholder'))).toBeTruthy();
153153
});
@@ -243,14 +243,14 @@ describe('ThumbnailComponent', () => {
243243
describe('fallback', () => {
244244
describe('if there is a default image', () => {
245245
it('should display the default image', () => {
246-
comp.src$.next('http://bit.stream');
246+
comp.src = 'http://bit.stream';
247247
comp.defaultImage = 'http://default.img';
248248
comp.errorHandler();
249-
expect(comp.src$.getValue()).toBe(comp.defaultImage);
249+
expect(comp.src).toBe(comp.defaultImage);
250250
});
251251

252252
it('should include the alt text', () => {
253-
comp.src$.next('http://bit.stream');
253+
comp.src = 'http://bit.stream';
254254
comp.defaultImage = 'http://default.img';
255255
comp.errorHandler();
256256

@@ -262,10 +262,10 @@ describe('ThumbnailComponent', () => {
262262

263263
describe('if there is no default image', () => {
264264
it('should display the HTML placeholder', () => {
265-
comp.src$.next('http://default.img');
265+
comp.src = 'http://default.img';
266266
comp.defaultImage = null;
267267
comp.errorHandler();
268-
expect(comp.src$.getValue()).toBe(null);
268+
expect(comp.src).toBe(null);
269269

270270
fixture.detectChanges();
271271
const placeholder = fixture.debugElement.query(By.css('div.thumbnail-placeholder')).nativeElement;
@@ -357,7 +357,7 @@ describe('ThumbnailComponent', () => {
357357
it('should show the default image', () => {
358358
comp.defaultImage = 'default/image.jpg';
359359
comp.ngOnChanges({});
360-
expect(comp.src$.getValue()).toBe('default/image.jpg');
360+
expect(comp.src).toBe('default/image.jpg');
361361
});
362362
});
363363
});

src/app/thumbnail/thumbnail.component.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
SimpleChanges,
77
} from '@angular/core';
88
import { TranslateModule } from '@ngx-translate/core';
9-
import {
10-
BehaviorSubject,
11-
of as observableOf,
12-
} from 'rxjs';
9+
import { of as observableOf } from 'rxjs';
1310
import { switchMap } from 'rxjs/operators';
1411

1512
import { AuthService } from '../core/auth/auth.service';
@@ -53,7 +50,7 @@ export class ThumbnailComponent implements OnChanges {
5350
/**
5451
* The src attribute used in the template to render the image.
5552
*/
56-
src$ = new BehaviorSubject<string>(undefined);
53+
src: string = undefined;
5754

5855
retriedWithToken = false;
5956

@@ -76,7 +73,7 @@ export class ThumbnailComponent implements OnChanges {
7673
* Whether the thumbnail is currently loading
7774
* Start out as true to avoid flashing the alt text while a thumbnail is being loaded.
7875
*/
79-
isLoading$ = new BehaviorSubject(true);
76+
isLoading = true;
8077

8178
constructor(
8279
protected auth: AuthService,
@@ -129,7 +126,7 @@ export class ThumbnailComponent implements OnChanges {
129126
* Otherwise, fall back to the default image or a HTML placeholder
130127
*/
131128
errorHandler() {
132-
const src = this.src$.getValue();
129+
const src = this.src;
133130
const thumbnail = this.bitstream;
134131
const thumbnailSrc = thumbnail?._links?.content?.href;
135132

@@ -181,16 +178,16 @@ export class ThumbnailComponent implements OnChanges {
181178
* @param src
182179
*/
183180
setSrc(src: string): void {
184-
this.src$.next(src);
181+
this.src = src;
185182
if (src === null) {
186-
this.isLoading$.next(false);
183+
this.isLoading = false;
187184
}
188185
}
189186

190187
/**
191188
* Stop the loading animation once the thumbnail is successfully loaded
192189
*/
193190
successHandler() {
194-
this.isLoading$.next(false);
191+
this.isLoading = false;
195192
}
196193
}

0 commit comments

Comments
 (0)