-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathcloudinary-background-image.directive.ts
33 lines (27 loc) · 1.31 KB
/
cloudinary-background-image.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import {Directive, ElementRef, Renderer2, AfterViewInit, Input, QueryList, ContentChildren} from '@angular/core';
import {Cloudinary} from './cloudinary.service';
import {CloudinaryTransformationDirective} from './cloudinary-transformation.directive';
@Directive({
selector: '[clBackgroundImage]'
})
export class CloudinaryBackgroundImageDirective implements AfterViewInit {
@Input() clBackgroundImage: string;
@Input() position: string = 'center';
@ContentChildren(CloudinaryTransformationDirective)
transformations: QueryList<CloudinaryTransformationDirective>;
constructor(private renderer: Renderer2, private el: ElementRef, private cloudinary: Cloudinary) {
}
isBrowser(): boolean {
return typeof window !== 'undefined';
}
ngAfterViewInit() {
if (this.isBrowser()) {
const nativeElement = this.el.nativeElement;
const options = this.cloudinary.toCloudinaryAttributes(nativeElement.attributes, this.transformations);
const imageUrl = this.cloudinary.url(this.clBackgroundImage, options);
this.renderer.setStyle(nativeElement, 'background-image', `url('${imageUrl}')`);
this.renderer.setStyle(nativeElement, 'background-repeat', 'no-repeat');
this.renderer.setStyle(nativeElement, 'background-position', this.position);
}
}
}