From ac3bb472d06dc4b1eae4c0713646546425eb4dd4 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 8 Sep 2018 19:23:45 -0400 Subject: [PATCH] Add support for more events * `seeking` * `ended` --- src/youtube-video.ts | 11 +++++++++++ tsconfig.json | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/youtube-video.ts b/src/youtube-video.ts index 42c072fa..bee8e0ad 100644 --- a/src/youtube-video.ts +++ b/src/youtube-video.ts @@ -14,6 +14,7 @@ export class YoutubeVideoElement extends HTMLElement { ytPlayer: YT.Player; paused: boolean = true; + ended: boolean = false; ytPlayerContainer: HTMLElement = undefined; private resolveBuildPlayerPromise: () => void = null; @@ -128,6 +129,7 @@ export class YoutubeVideoElement extends HTMLElement { private onPlay() { this.paused = false; + this.ended = false; // pause all other youtube videos from playing! videos.forEach((id, video) => { if (video !== this && !video.paused) { @@ -143,10 +145,17 @@ export class YoutubeVideoElement extends HTMLElement { private onEnd() { this.paused = true; + this.ended = true; + } + + private onCued() { + this.ended = false; + this.dispatchEvent(new CustomEvent('seeking')); } set error(error) { const { message } = error; + // TODO: dispatch a HTMLMediaElement.error here (https://html.spec.whatwg.org/multipage/media.html#dom-media-error) this.dispatchEvent(new ErrorEvent(message)); this.mediaError = error; throw error; @@ -169,6 +178,7 @@ export class YoutubeVideoElement extends HTMLElement { // trigger our internal event handling method // whenever the youtube api player triggers an event const eventMethodMap = { + cued: this.onCued, ended: this.onEnd, pause: this.onPause, playing: this.onPlay, @@ -217,6 +227,7 @@ export class YoutubeVideoElement extends HTMLElement { const playerOptions = { events: { onError: (e: YT.OnErrorEvent) => { + // TODO: update this to be a MediaError https://html.spec.whatwg.org/multipage/media.html#mediaerror) to adhere to spec this.error = new Error('player could not be built'); }, onReady: (e: YT.PlayerEvent) => { diff --git a/tsconfig.json b/tsconfig.json index ca40f61f..b189d113 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,5 +13,7 @@ "es2015", "dom" ], - "typeRoots": ["types"] + "typeRoots": [ + "types" + ] }