Skip to content

Commit 72a2a37

Browse files
committed
feat(): extend global config and add fallback image
1 parent 6a1f818 commit 72a2a37

File tree

2 files changed

+130
-21
lines changed

2 files changed

+130
-21
lines changed

src/components/img-loader/img-loader.ts

Lines changed: 88 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@ export class ImgLoader implements OnInit {
1313
*/
1414
@Input('src') imageUrl: string;
1515

16+
@Input('fallback') fallbackUrl: string;
17+
1618
@Input() spinner: boolean;
1719

18-
@Input() useImg: boolean = false;
20+
@Input() useImg: boolean;
21+
22+
@Input() width: string;
23+
24+
@Input() height: string;
25+
26+
@Input() display: string;
1927

20-
@Input() width: string = '100%';
28+
@Input() backgroundSize: string;
2129

22-
@Input() height: string = '100%';
30+
@Input() backgroundRepeat: string;
2331

2432
isLoading: boolean = true;
2533

@@ -29,35 +37,94 @@ export class ImgLoader implements OnInit {
2937
, private imageLoader: ImageLoader
3038
, private config: ImageLoaderConfig
3139
) {
32-
if (typeof this.spinner === 'undefined' && config.spinnerEnabled) {
40+
if (!this.spinner && config.spinnerEnabled) {
3341
this.spinner = true;
3442
}
43+
44+
if (!this.fallbackUrl) {
45+
this.fallbackUrl = config.fallbackUrl;
46+
}
47+
48+
if (!this.useImg) {
49+
this.useImg = config.useImg;
50+
}
51+
52+
if (!this.width) {
53+
this.width = config.width;
54+
}
55+
56+
if (!this.height) {
57+
this.height = config.height;
58+
}
59+
60+
if (!this.display) {
61+
this.display = config.display;
62+
}
63+
64+
if (!this.backgroundSize) {
65+
this.backgroundSize = config.backgroundSize;
66+
}
67+
68+
if (!this.backgroundRepeat) {
69+
this.backgroundRepeat = config.backgroundRepeat;
70+
}
71+
3572
}
3673

3774
ngOnInit(): void {
75+
if (!this.imageUrl) {
76+
// image url was not passed
77+
// this can happen when [src] is set to a variable that turned out to be undefined
78+
// one example could be a list of users with their profile pictures
79+
// in this case, it would be useful to use the fallback image instead
80+
if (this.fallbackUrl) {
81+
// we're not going to cache the fallback image since it should be locally saved
82+
this.setImage(this.fallbackUrl);
83+
}
84+
// remove the spinner just in case there is no fallback image
85+
this.isLoading = false;
86+
return;
87+
}
88+
3889
this.imageLoader.getImagePath(this.imageUrl)
39-
.then((imageUrl: string) => {
90+
.then((imageUrl: string) => this.setImage(imageUrl));
91+
}
92+
93+
private setImage(imageUrl: string): void {
94+
let element;
95+
96+
this.isLoading = false;
97+
98+
if (this.useImg) {
99+
this.renderer.createElement(this.element.nativeElement, 'img');
100+
element = <HTMLImageElement>this.element.nativeElement.getElementsByTagName('IMG')[0];
101+
this.renderer.setElementAttribute(element, 'src', imageUrl);
102+
} else {
40103

41-
let element;
104+
element = this.element.nativeElement;
42105

43-
this.isLoading = false;
106+
if (this.display) {
107+
this.renderer.setElementStyle(element, 'display', this.display);
108+
}
44109

45-
if (this.useImg) {
46-
this.renderer.createElement(this.element.nativeElement, 'img');
47-
element = <HTMLImageElement>this.element.nativeElement.getElementsByTagName('IMG')[0];
48-
this.renderer.setElementAttribute(element, 'src', imageUrl);
49-
} else {
110+
if (this.height) {
111+
this.renderer.setElementStyle(element, 'height', this.height);
112+
}
50113

51-
element = this.element.nativeElement;
114+
if (this.width) {
115+
this.renderer.setElementStyle(element, 'width', this.width);
116+
}
52117

53-
this.renderer.setElementStyle(element, 'width', this.width);
54-
this.renderer.setElementStyle(element, 'height', this.height);
55-
this.renderer.setElementStyle(element, 'display', 'block');
56-
this.renderer.setElementStyle(element, 'background-size', 'contain');
57-
this.renderer.setElementStyle(element, 'background-repeat', 'no-repeat');
58-
this.renderer.setElementStyle(element, 'background-image', 'url(\'' + imageUrl +'\')');
59-
}
60-
});
118+
if (this.backgroundSize) {
119+
this.renderer.setElementStyle(element, 'background-size', this.backgroundSize);
120+
}
121+
122+
if (this.backgroundRepeat) {
123+
this.renderer.setElementStyle(element, 'background-repeat', this.backgroundRepeat);
124+
}
125+
126+
this.renderer.setElementStyle(element, 'background-image', 'url(\'' + imageUrl +'\')');
127+
}
61128
}
62129

63130
}

src/providers/image-loader-config.ts

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

88
spinnerEnabled: boolean = true;
99

10+
backgroundSize: string = 'contain';
11+
12+
backgroundRepeat: string = 'no-repeat';
13+
14+
display: string = 'block';
15+
16+
width: string = '100%';
17+
18+
height: string = '100%';
19+
20+
useImg: boolean = false;
21+
22+
fallbackUrl: string;
23+
1024
private _cacheDirectoryName: string = 'image-loader-cache';
1125

1226
set cacheDirectoryName(name: string) {
@@ -30,4 +44,32 @@ export class ImageLoaderConfig {
3044
this.cacheDirectoryName = name;
3145
}
3246

47+
setHeight(height: string): void {
48+
this.height = height;
49+
}
50+
51+
setWidth(width: string): void {
52+
this.width = width;
53+
}
54+
55+
enableBlock(enable: string): void {
56+
this.display = enable;
57+
}
58+
59+
useImageTag(use: boolean): void {
60+
this.useImg = use;
61+
}
62+
63+
setBackgroundSize(backgroundSize: string): void {
64+
this.backgroundSize = backgroundSize;
65+
}
66+
67+
setBackgroundRepeat(backgroundRepeat: string): void {
68+
this.backgroundRepeat = backgroundRepeat;
69+
}
70+
71+
setFallbackUrl(fallbackUrl: string): void {
72+
this.fallbackUrl = fallbackUrl;
73+
}
74+
3375
}

0 commit comments

Comments
 (0)