@@ -13,13 +13,21 @@ export class ImgLoader implements OnInit {
13
13
*/
14
14
@Input ( 'src' ) imageUrl : string ;
15
15
16
+ @Input ( 'fallback' ) fallbackUrl : string ;
17
+
16
18
@Input ( ) spinner : boolean ;
17
19
18
- @Input ( ) useImg : boolean = false ;
20
+ @Input ( ) useImg : boolean ;
21
+
22
+ @Input ( ) width : string ;
23
+
24
+ @Input ( ) height : string ;
25
+
26
+ @Input ( ) display : string ;
19
27
20
- @Input ( ) width : string = '100%' ;
28
+ @Input ( ) backgroundSize : string ;
21
29
22
- @Input ( ) height : string = '100%' ;
30
+ @Input ( ) backgroundRepeat : string ;
23
31
24
32
isLoading : boolean = true ;
25
33
@@ -29,35 +37,94 @@ export class ImgLoader implements OnInit {
29
37
, private imageLoader : ImageLoader
30
38
, private config : ImageLoaderConfig
31
39
) {
32
- if ( typeof this . spinner === 'undefined' && config . spinnerEnabled ) {
40
+ if ( ! this . spinner && config . spinnerEnabled ) {
33
41
this . spinner = true ;
34
42
}
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
+
35
72
}
36
73
37
74
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
+
38
89
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 {
40
103
41
- let element ;
104
+ element = this . element . nativeElement ;
42
105
43
- this . isLoading = false ;
106
+ if ( this . display ) {
107
+ this . renderer . setElementStyle ( element , 'display' , this . display ) ;
108
+ }
44
109
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
+ }
50
113
51
- element = this . element . nativeElement ;
114
+ if ( this . width ) {
115
+ this . renderer . setElementStyle ( element , 'width' , this . width ) ;
116
+ }
52
117
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
+ }
61
128
}
62
129
63
130
}
0 commit comments