Skip to content

Commit 2840047

Browse files
authored
added video event support (#300)
1 parent 22e62f3 commit 2840047

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

projects/angular-cld/src/lib/cloudinary-video.component.spec.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, DebugElement } from '@angular/core';
2-
import { ComponentFixture, TestBed } from '@angular/core/testing';
2+
import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing';
33
import { By } from '@angular/platform-browser';
44
import { Cloudinary } from './cloudinary.service';
55
import CloudinaryConfiguration from './cloudinary-configuration.class';
@@ -401,3 +401,54 @@ describe('CloudinaryVideo', () => {
401401
});
402402
});
403403

404+
405+
describe('Video event handler', () => {
406+
let localCloudinary: Cloudinary = new Cloudinary(require('cloudinary-core'),
407+
{ cloud_name: '@@fake_angular2_sdk@@' } as CloudinaryConfiguration);
408+
let component: CloudinaryVideo;
409+
let fixture: ComponentFixture<CloudinaryVideo>;
410+
411+
beforeEach(() => {
412+
TestBed.configureTestingModule({
413+
declarations: [CloudinaryVideo],
414+
providers: [{ provide: Cloudinary , useValue: localCloudinary}]
415+
});
416+
fixture = TestBed.createComponent(CloudinaryVideo);
417+
component = fixture.componentInstance;
418+
component.publicId = 'demo';
419+
});
420+
421+
it('should emit play event', fakeAsync(() => {
422+
spyOn(component, 'emitPlayEvent');
423+
const videoElement: HTMLVideoElement = fixture.nativeElement;
424+
const vid = videoElement.querySelector('video');
425+
426+
vid.dispatchEvent(new Event('play'));
427+
fixture.detectChanges();
428+
429+
expect(component.emitPlayEvent).toHaveBeenCalled();
430+
}));
431+
432+
it('should emit playing event', fakeAsync(() => {
433+
spyOn(component, 'emitPlayingEvent');
434+
const videoElement: HTMLVideoElement = fixture.nativeElement;
435+
const vid = videoElement.querySelector('video');
436+
437+
vid.dispatchEvent(new Event('playing'));
438+
fixture.detectChanges();
439+
440+
expect(component.emitPlayingEvent).toHaveBeenCalled();
441+
}));
442+
443+
it('should emit loadstart event', fakeAsync(() => {
444+
spyOn(component, 'emitLoadstartEvent');
445+
const videoElement: HTMLVideoElement = fixture.nativeElement;
446+
const vid = videoElement.querySelector('video');
447+
448+
vid.dispatchEvent(new Event('loadstart'));
449+
fixture.detectChanges();
450+
451+
expect(component.emitLoadstartEvent).toHaveBeenCalled();
452+
}));
453+
});
454+

projects/angular-cld/src/lib/cloudinary-video.component.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,28 @@ import {
1111
OnDestroy,
1212
PLATFORM_ID,
1313
Inject,
14+
EventEmitter,
15+
Output
1416
} from '@angular/core';
1517
import { isPlatformBrowser } from '@angular/common';
1618
import { Cloudinary } from './cloudinary.service';
1719
import { CloudinaryTransformationDirective } from './cloudinary-transformation.directive';
1820

1921
@Component({
2022
selector: 'cl-video',
21-
template: '<video></video>'
23+
template: '<video (play)="emitPlayEvent()" (loadstart)="emitLoadstartEvent()" (playing)="emitPlayingEvent()" (error)="emitErrorEvent" (ended)="emitEndedEvent"></video>'
2224
})
2325
// See also video reference - http://cloudinary.com/documentation/video_manipulation_and_delivery#video_transformations_reference
2426
export class CloudinaryVideo
2527
implements AfterViewInit, OnInit, OnChanges, OnDestroy {
2628
@Input('public-id') publicId: string;
2729

30+
@Output() play: EventEmitter<any> = new EventEmitter();
31+
@Output() loadstart: EventEmitter<any> = new EventEmitter();
32+
@Output() playing: EventEmitter<any> = new EventEmitter();
33+
@Output() error: EventEmitter<any> = new EventEmitter();
34+
@Output() ended: EventEmitter<any> = new EventEmitter();
35+
2836
@ContentChildren(CloudinaryTransformationDirective)
2937
transformations: QueryList<CloudinaryTransformationDirective>;
3038

@@ -103,4 +111,24 @@ export class CloudinaryVideo
103111
}
104112
element.appendChild(fragment);
105113
}
114+
115+
emitPlayEvent() {
116+
this.play.emit();
117+
}
118+
119+
emitLoadstartEvent() {
120+
this.loadstart.emit();
121+
}
122+
123+
emitPlayingEvent() {
124+
this.playing.emit();
125+
}
126+
127+
emitErrorEvent() {
128+
this.error.emit();
129+
}
130+
131+
emitEndedEvent() {
132+
this.ended.emit();
133+
}
106134
}

0 commit comments

Comments
 (0)