Skip to content

Commit

Permalink
feature: add type setting support to lazy-video
Browse files Browse the repository at this point in the history
  • Loading branch information
amacdonald-google committed Aug 5, 2021
1 parent 3167e30 commit 6b10227
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/dom/lazy-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export const LazyVideoEvents = {
*
*/
export class LazyVideo {
private static readonly VALID_TYPES = new Set(['mp4', 'webm']);

/**
* The root video element.
*/
Expand All @@ -111,6 +113,7 @@ export class LazyVideo {
* The url of the video to load.
*/
private url: string;
private type: string | null;
private setComplete: boolean;
// Ev used to trigger the load of the video.
private ev: ElementVisibilityObject;
Expand Down Expand Up @@ -160,6 +163,9 @@ export class LazyVideo {
*/
this.url = this.el.getAttribute('lazy-video') || '';

const rawType = this.url.split('.').slice(-1)[0]; // Grab extension
this.type = LazyVideo.VALID_TYPES.has(rawType) ? rawType : null;

/**
* Whether this directive has finished setting the video.
*/
Expand Down Expand Up @@ -206,6 +212,9 @@ export class LazyVideo {
public paint(force = false): void {
if ((this.isPainted() && !this.setComplete) || force) {
this.setComplete = true;
if (this.type !== null) {
this.el.setAttribute('type', `video/${this.type}`);
}
this.el.setAttribute('src', this.url);

dom.whenVideosLoaded([this.video]).then(() => {
Expand Down

0 comments on commit 6b10227

Please sign in to comment.