@@ -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}
0 commit comments