Skip to content

Commit 4aceb73

Browse files
eitanp461tocker
authored andcommitted
Add background image directive
1 parent 8d4eef6 commit 4aceb73

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Component, DebugElement } from '@angular/core';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { By } from '@angular/platform-browser';
4+
import { Cloudinary } from './cloudinary.service';
5+
import CloudinaryConfiguration from './cloudinary-configuration.class';
6+
import { CloudinaryBackgroundImageDirective } from './cloudinary-background-image.directive'
7+
import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive';
8+
9+
@Component({
10+
template: `
11+
<button [clBackgroundImage]="image.public_id" fetch_format="auto" target="_blank">
12+
<cl-transformation effect="sepia"></cl-transformation>
13+
click me
14+
</button>
15+
<img [clBackgroundImage]="image.public_id" width="100" crop="scale"/>
16+
`
17+
})
18+
class TestComponent { }
19+
20+
describe('CloudinaryBackgroundImageDirective', () => {
21+
22+
let fixture: ComponentFixture<TestComponent>;
23+
let des: DebugElement[]; // the three elements w/ the directive
24+
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
25+
{ cloud_name: '@@fake_angular2_sdk@@' } as CloudinaryConfiguration);
26+
27+
beforeEach(() => {
28+
fixture = TestBed.configureTestingModule({
29+
declarations: [CloudinaryBackgroundImageDirective, CloudinaryTransformationDirective, TestComponent],
30+
providers: [{ provide: Cloudinary, useValue: localCloudinary }]
31+
}).createComponent(TestComponent);
32+
const comp = fixture.componentInstance;
33+
34+
// pretend that it was wired to something that supplied an image
35+
comp['image'] = {
36+
public_id: 'some_image_id'
37+
};
38+
39+
fixture.detectChanges(); // initial binding
40+
41+
// all elements with an attached CloudinaryImageSourceDirective
42+
des = fixture.debugElement.queryAll(By.directive(CloudinaryBackgroundImageDirective));
43+
});
44+
45+
it('should have 1 element', () => {
46+
expect(des.length).toBe(2);
47+
});
48+
49+
it('updates the background image with transformations', () => {
50+
const button = des[0].nativeElement as HTMLButtonElement;
51+
expect(button.style.backgroundImage).toEqual(jasmine.stringMatching(/image\/upload\/e_sepia\/f_auto\/some_image_id/));
52+
expect(button.textContent).toEqual(jasmine.stringMatching(/click me/));
53+
});
54+
55+
it('updates the background image without transformations', () => {
56+
const div = des[1].nativeElement as HTMLDivElement;
57+
expect(div.style.backgroundImage).toEqual(jasmine.stringMatching(/c_scale,w_100\/some_image_id/));
58+
});
59+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Directive, ElementRef, Renderer2, AfterViewInit, Input, QueryList, ContentChildren} from '@angular/core';
2+
import {Cloudinary} from './cloudinary.service';
3+
import {CloudinaryTransformationDirective} from './cloudinary-transformation.directive';
4+
5+
@Directive({
6+
selector: '[clBackgroundImage]'
7+
})
8+
export class CloudinaryBackgroundImageDirective implements AfterViewInit {
9+
10+
@Input() clBackgroundImage: string;
11+
12+
@ContentChildren(CloudinaryTransformationDirective)
13+
transformations: QueryList<CloudinaryTransformationDirective>;
14+
15+
constructor(private renderer: Renderer2, private el: ElementRef, private cloudinary: Cloudinary) {
16+
}
17+
18+
ngAfterViewInit() {
19+
const nativeElement = this.el.nativeElement;
20+
const options = this.cloudinary.toCloudinaryAttributes(nativeElement.attributes, this.transformations);
21+
const imageUrl = this.cloudinary.url(this.clBackgroundImage, options);
22+
this.renderer.setStyle(this.el.nativeElement, 'background-image', `url('${imageUrl}')`);
23+
};
24+
}

0 commit comments

Comments
 (0)