Skip to content

Commit c2686c7

Browse files
lucho2d7ihadeed
authored andcommitted
feat(): ability to use fallbackUrl as placeholder instead of spinner (#36)
* added ability to use fallbackUrl as placeholder instead of spinner * added ability to use fallbackUrl as placeholder instead of spinner, fix * code style
1 parent f29d630 commit c2686c7

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/components/img-loader.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ImageLoaderConfig } from '../providers/image-loader-config';
44

55
@Component({
66
selector: 'img-loader',
7-
template: '<ion-spinner *ngIf="spinner && isLoading"></ion-spinner>',
7+
template: '<ion-spinner *ngIf="spinner && isLoading && !fallbackAsPlaceholder"></ion-spinner>',
88
styles: ['ion-spinner { float: none; margin-left: auto; margin-right: auto; display: block; }']
99
})
1010
export class ImgLoader implements OnInit {
@@ -34,6 +34,11 @@ export class ImgLoader implements OnInit {
3434
*/
3535
@Input() spinner: boolean = this._config.spinnerEnabled;
3636

37+
/**
38+
* Whether to show the fallback image instead of a spinner while the image loads
39+
*/
40+
@Input() fallbackAsPlaceholder: boolean = this._config.fallbackAsPlaceholder;
41+
3742
/**
3843
* Use <img> tag
3944
*/
@@ -106,12 +111,17 @@ export class ImgLoader implements OnInit {
106111
) { }
107112

108113
ngOnInit(): void {
114+
if(this.fallbackAsPlaceholder && this.fallbackUrl) {
115+
this.setImage(this.fallbackUrl, false);
116+
}
117+
109118
if (!this.src) {
110119
// image url was not passed
111120
// this can happen when [src] is set to a variable that turned out to be undefined
112121
// one example could be a list of users with their profile pictures
113122
// in this case, it would be useful to use the fallback image instead
114-
if (this.fallbackUrl) {
123+
// if fallbackUrl was used as placeholder we do not need to set it again
124+
if (!this.fallbackAsPlaceholder && this.fallbackUrl) {
115125
// we're not going to cache the fallback image since it should be locally saved
116126
this.setImage(this.fallbackUrl);
117127
} else {
@@ -153,8 +163,8 @@ export class ImgLoader implements OnInit {
153163
* Set the image to be displayed
154164
* @param imageUrl
155165
*/
156-
private setImage(imageUrl: string): void {
157-
this.isLoading = false;
166+
private setImage(imageUrl: string, stopLoading: boolean = true): void {
167+
this.isLoading = !stopLoading;
158168

159169
if (this._useImg) {
160170

src/providers/image-loader-config.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export class ImageLoaderConfig {
77

88
spinnerEnabled: boolean = true;
99

10+
fallbackAsPlaceholder: boolean = false;
11+
1012
backgroundSize: string = 'contain';
1113

1214
backgroundRepeat: string = 'no-repeat';
@@ -55,6 +57,14 @@ export class ImageLoaderConfig {
5557
this.spinnerEnabled = enable;
5658
}
5759

60+
/**
61+
* Enable/Disable the fallback image as placeholder instead of the spinner. Defaults to false.
62+
* @param enable {boolean} set to true to enable
63+
*/
64+
enableFallbackAsPlaceholder(enable: boolean): void {
65+
this.fallbackAsPlaceholder = enable;
66+
}
67+
5868
/**
5969
* Sets the cache directory name. Defaults to 'image-loader-cache'
6070
* @param name {string} name of directory

0 commit comments

Comments
 (0)