]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/standalone/videos/embed.ts
Add previous button
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / embed.ts
index 17b0ee9ef03053c2da4bd6cd839a519745da097f..786d749a498903ca9181029fdb03dde864b1e111 100644 (file)
@@ -309,13 +309,13 @@ export class PeerTubeEmbed {
       cancelText: peertubeTranslate('Cancel', translations),
       suspendedText: peertubeTranslate('Autoplay is suspended', translations),
       getTitle: () => this.nextVideoTitle(),
-      next: () => this.autoplayNext(),
+      next: () => this.playNextVideo(),
       condition: () => !!this.getNextPlaylistElement(),
       suspended: () => false
     })
   }
 
-  private async autoplayNext () {
+  private async playNextVideo () {
     const next = this.getNextPlaylistElement()
     if (!next) {
       console.log('Next element not found in playlist.')
@@ -327,6 +327,18 @@ export class PeerTubeEmbed {
     return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
   }
 
+  private async playPreviousVideo () {
+    const previous = this.getPreviousPlaylistElement()
+    if (!previous) {
+      console.log('Previous element not found in playlist.')
+      return
+    }
+
+    this.currentPlaylistElement = previous
+
+    return this.loadVideoAndBuildPlayer(this.currentPlaylistElement.video.uuid)
+  }
+
   private async loadVideoAndBuildPlayer (uuid: string) {
     const res = await this.loadVideo(uuid)
     if (res === undefined) return
@@ -357,6 +369,22 @@ export class PeerTubeEmbed {
     return next
   }
 
+  private getPreviousPlaylistElement (position?: number): VideoPlaylistElement {
+    if (!position) position = this.currentPlaylistElement.position -1
+
+    if (position < 1) {
+      return undefined
+    }
+
+    const prev = this.playlistElements.find(e => e.position === position)
+
+    if (!prev || !prev.video) {
+      return this.getNextPlaylistElement(position - 1)
+    }
+
+    return prev
+  }
+
   private async buildVideoPlayer (videoResponse: Response, captionsPromise: Promise<Response>) {
     let alreadyHadPlayer = false
 
@@ -418,7 +446,12 @@ export class PeerTubeEmbed {
         stopTime: this.stopTime,
         subtitle: this.subtitle,
 
-        nextVideo: () => this.autoplayNext(),
+        nextVideo: this.playlist ? () => this.playNextVideo() : undefined,
+        hasNextVideo: this.playlist ? () => !!this.getNextPlaylistElement() : undefined,
+
+        previousVideo: this.playlist ? () => this.playPreviousVideo() : undefined,
+        hasPreviousVideo: this.playlist ? () => !!this.getPreviousPlaylistElement() : undefined,
+
         playlist: playlistPlugin,
 
         videoCaptions,