Skip to content

Commit 64685ff

Browse files
authored
added viideo ref (#127)
1 parent e315189 commit 64685ff

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

packages/angular/projects/cloudinary-library/src/lib/cloudinary-video.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class CloudinaryVideoComponent implements OnInit, OnChanges, OnDestroy {
5353
@Input('sources') sources: VideoSources;
5454
@Input('plugins') plugins: Plugins;
5555
@Input('poster') poster: string;
56+
@Input('innerRef') innerRef: ElementRef;
5657

5758
// Event emitters
5859
@Output() play: EventEmitter<any> = new EventEmitter();
@@ -89,6 +90,9 @@ export class CloudinaryVideoComponent implements OnInit, OnChanges, OnDestroy {
8990
if (this.muted) {
9091
this.el.nativeElement.children[0].muted = true;
9192
}
93+
94+
// attach ref to innerRef input
95+
this.attachRef();
9296
}
9397

9498
/**
@@ -144,4 +148,13 @@ export class CloudinaryVideoComponent implements OnInit, OnChanges, OnDestroy {
144148
emitEndedEvent() {
145149
this.ended.emit();
146150
}
151+
152+
/**
153+
* Attach both this.videoRef and props.innerRef as ref to the given element.
154+
*/
155+
attachRef() {
156+
if (this.innerRef) {
157+
this.innerRef.nativeElement = this.el.nativeElement.children[0];
158+
}
159+
}
147160
}

packages/angular/projects/cloudinary-library/src/tests/cloudinary-video.component.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CloudinaryVideoComponent } from '../lib/cloudinary-video.component';
33
import {CloudinaryVideo} from '@cloudinary/url-gen';
44
import { auto, vp9 } from '@cloudinary/url-gen/qualifiers/videoCodec';
55
import { videoCodec } from '@cloudinary/url-gen/actions/transcode';
6+
import {ElementRef} from "@angular/core";
67

78
const cloudinaryVideo = new CloudinaryVideo('sample', { cloudName: 'demo'}, { analytics: false });
89

@@ -151,6 +152,23 @@ describe('CloudinaryVideoComponent render', () => {
151152

152153
component.ngOnDestroy();
153154
}));
155+
156+
it('Should support forwarding innerRef to underlying video element', fakeAsync(() => {
157+
component.cldVid = cloudinaryVideo;
158+
const ref = new ElementRef<HTMLVideoElement>(null);
159+
component.innerRef = ref;
160+
161+
fixture.detectChanges();
162+
tick(0);
163+
spyOn(component, 'emitPlayEvent');
164+
const videoElement: HTMLVideoElement = fixture.nativeElement;
165+
const vid = videoElement.querySelector('video');
166+
167+
ref.nativeElement.dispatchEvent(new Event('play'));
168+
fixture.detectChanges();
169+
170+
expect(component.emitPlayEvent).toHaveBeenCalled();
171+
}));
154172
});
155173

156174

0 commit comments

Comments
 (0)