Skip to content

Add accessibility mode attribute to cl-image #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/cloudinary-image.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,67 @@ describe('CloudinaryImage', () => {
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_sepia/c_fit,w_30/e_blur:2000,f_auto,q_1/bear'));
}));
});
describe('cl-image with acessibility modes', () => {
@Component({
template: `<cl-image public-id="bear" accessibility="darkmode"></cl-image>
<cl-image public-id="bear" accessibility="monochrome"></cl-image>
<cl-image public-id="bear" accessibility="brightmode"></cl-image>
<cl-image public-id="bear" accessibility="colorblind"></cl-image>`
})
class TestComponent {}

let fixture: ComponentFixture<TestComponent>;
let des: DebugElement[]; // the elements w/ the directive
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular2_sdk@@', client_hints: true } as CloudinaryConfiguration);
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
});
it('creates an img element with accessibility darkmode', fakeAsync(() => {
const img = des[0].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_tint:75:black/bear'));
}));
it('creates an img element with accessibility monochrome', fakeAsync(() => {
const img = des[1].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_grayscale/bear'));
}));
it('creates an img element with accessibility brightmode', fakeAsync(() => {
const img = des[2].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_tint:50:white/bear'));
}));
it('creates an img element with accessibility colorblind', fakeAsync(() => {
const img = des[3].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_assist_colorblind/bear'));
}));
});
describe('cl-image with acessibility modes and transformation', () => {
@Component({
template: `<cl-image public-id="bear" accessibility="darkmode" effect="grayscale" overlay="sample">
<cl-transformation effect="sepia"></cl-transformation>
</cl-image>`
})
class TestComponent {}

let fixture: ComponentFixture<TestComponent>;
let des: DebugElement[]; // the elements w/ the directive
let testLocalCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
{ cloud_name: '@@fake_angular2_sdk@@', client_hints: true } as CloudinaryConfiguration);
beforeEach(() => {
fixture = TestBed.configureTestingModule({
declarations: [CloudinaryTransformationDirective, CloudinaryImage, TestComponent],
providers: [{ provide: Cloudinary, useValue: testLocalCloudinary }]
}).createComponent(TestComponent);
fixture.detectChanges(); // initial binding
des = fixture.debugElement.queryAll(By.directive(CloudinaryImage));
});
it('creates an img element with accessibility darkmode without overwriting effect', fakeAsync(() => {
const img = des[0].children[0].nativeElement as HTMLImageElement;
expect(img.attributes.getNamedItem('src').value).toEqual(jasmine.stringMatching('e_sepia/e_grayscale,l_sample/e_tint:75:black/bear'));
}));
});
});
15 changes: 14 additions & 1 deletion src/cloudinary-image.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import { Cloudinary } from './cloudinary.service';
import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive';
import { CloudinaryPlaceHolder } from './cloudinary-placeholder.component';
import { isBrowser } from './cloudinary.service';
import { accessibilityEffect } from './constants';

@Component({
selector: 'cl-image',
template: `<img [style.display]="shouldShowPlaceHolder ? 'none' : 'inline'" (load)="hasLoaded()">
<div [style.display]="shouldShowPlaceHolder ? 'inline' : 'none'">
<ng-content></ng-content>
<ng-content></ng-content>
</div>
`,
})
Expand All @@ -33,6 +34,7 @@ export class CloudinaryImage
@Input('loading') loading: string;
@Input('width') width?: string;
@Input('height') height?: string;
@Input('accessibility') accessibility?: string;

@ContentChildren(CloudinaryTransformationDirective)
transformations: QueryList<CloudinaryTransformationDirective>;
Expand All @@ -43,6 +45,7 @@ export class CloudinaryImage

observer: MutationObserver;
shouldShowPlaceHolder: boolean = true;
options: object = {};

constructor(private el: ElementRef, private cloudinary: Cloudinary) {}

Expand Down Expand Up @@ -109,6 +112,7 @@ export class CloudinaryImage
image.onerror = e => {
this.onError.emit(e);
}

const options = this.cloudinary.toCloudinaryAttributes(
nativeElement.attributes,
this.transformations
Expand All @@ -121,6 +125,10 @@ export class CloudinaryImage
if (this.placeholderComponent) {
this.placeholderHandler(options);
}
if (this.accessibility) {
this.options = options;
options.src = this.accessibilityModeHandler();
}
const imageTag = this.cloudinary.imageTag(this.publicId, options);

this.setElementAttributes(image, imageTag.attributes());
Expand All @@ -145,4 +153,9 @@ export class CloudinaryImage
})
this.placeholderComponent.options = placeholderOptions;
}

accessibilityModeHandler() {
return this.cloudinary.url(this.publicId, {transformation: [this.options, accessibilityEffect[this.accessibility]]});
}
}

6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const accessibilityEffect = {
'darkmode': {effect: 'tint:75:black'},
'brightmode': {effect: 'tint:50:white'},
'monochrome': {effect: 'grayscale'},
'colorblind': {effect: 'assist_colorblind'}
}