Skip to content

Commit

Permalink
Add support for more events
Browse files Browse the repository at this point in the history
* `seeking`
* `ended`
  • Loading branch information
Mark committed Sep 12, 2018
1 parent 746664a commit ac3bb47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/youtube-video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@
"es2015",
"dom"
],
"typeRoots": ["types"]
"typeRoots": [
"types"
]
}

0 comments on commit ac3bb47

Please sign in to comment.