-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathcloudinary-placeholder.component.ts
61 lines (52 loc) · 1.71 KB
/
cloudinary-placeholder.component.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
AfterContentChecked,
Component,
HostBinding,
Input,
ElementRef,
Renderer2,
} from '@angular/core';
import {Cloudinary} from './cloudinary.service';
import { SDKAnalyticsConstants } from './SDKAnalyticsConstants';
@Component({
selector: 'cl-placeholder',
template: `<img [src]="this.placeholderImg">`
,
})
export class CloudinaryPlaceHolder implements AfterContentChecked {
@Input('type') type: string;
@HostBinding('style.width') itemWidth;
@HostBinding('style.height') itemHeight;
@HostBinding('attr.public-id') publicId;
options: object = {};
placeholderImg: string;
constructor(private cloudinary: Cloudinary, private renderer: Renderer2, private el: ElementRef) {}
setWidth(width) {
this.itemWidth = width;
}
setHeight(height) {
this.itemHeight = height;
}
setPublicId(id) {
this.publicId = id;
}
ngAfterContentChecked() {
const imageTag = this.cloudinary.imageTag(this.publicId, this.options);
this.setElementAttributes(this.el.nativeElement.children[0], imageTag.attributes());
this.placeholderImg = this.getPlaceholderImage();
}
getPlaceholderImage() {
if (this.type === 'predominant-color' && this.itemHeight && this.itemWidth) {
return this.cloudinary.url(this.publicId, {placeholder: 'predominant-color-pixel' || true, ...this.options});
} else {
return this.cloudinary.url(this.publicId, {placeholder: this.type || true, ...this.options});
}
}
setElementAttributes(element, attributesLiteral) {
Object.keys(attributesLiteral).forEach(attrName => {
if (attrName !== 'src' && attrName !== 'style') {
this.renderer.setAttribute(element, attrName, attributesLiteral[attrName]);
}
});
}
}