]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/standalone/videos/shared/playlist-tracker.ts
Fix client lint
[github/Chocobozzz/PeerTube.git] / client / src / standalone / videos / shared / playlist-tracker.ts
1 import { VideoPlaylist, VideoPlaylistElement } from '../../../../../shared/models'
2 import { logger } from '../../../root-helpers'
3
4 export class PlaylistTracker {
5 private currentPlaylistElement: VideoPlaylistElement
6
7 constructor (
8 private readonly playlist: VideoPlaylist,
9 private readonly playlistElements: VideoPlaylistElement[]
10 ) {
11
12 }
13
14 getPlaylist () {
15 return this.playlist
16 }
17
18 getPlaylistElements () {
19 return this.playlistElements
20 }
21
22 hasNextPlaylistElement (position?: number) {
23 return !!this.getNextPlaylistElement(position)
24 }
25
26 getNextPlaylistElement (position?: number): VideoPlaylistElement {
27 if (!position) position = this.currentPlaylistElement.position + 1
28
29 if (position > this.playlist.videosLength) {
30 return undefined
31 }
32
33 const next = this.playlistElements.find(e => e.position === position)
34
35 if (!next?.video) {
36 return this.getNextPlaylistElement(position + 1)
37 }
38
39 return next
40 }
41
42 hasPreviousPlaylistElement (position?: number) {
43 return !!this.getPreviousPlaylistElement(position)
44 }
45
46 getPreviousPlaylistElement (position?: number): VideoPlaylistElement {
47 if (!position) position = this.currentPlaylistElement.position - 1
48
49 if (position < 1) {
50 return undefined
51 }
52
53 const prev = this.playlistElements.find(e => e.position === position)
54
55 if (!prev?.video) {
56 return this.getNextPlaylistElement(position - 1)
57 }
58
59 return prev
60 }
61
62 nextVideoTitle () {
63 const next = this.getNextPlaylistElement()
64 if (!next) return ''
65
66 return next.video.name
67 }
68
69 setPosition (position: number) {
70 this.currentPlaylistElement = this.playlistElements.find(e => e.position === position)
71 if (!this.currentPlaylistElement?.video) {
72 logger.error('Current playlist element is not valid.', this.currentPlaylistElement)
73 this.currentPlaylistElement = this.getNextPlaylistElement()
74 }
75
76 if (!this.currentPlaylistElement) {
77 throw new Error('This playlist does not have any valid element')
78 }
79 }
80
81 setCurrentElement (playlistElement: VideoPlaylistElement) {
82 this.currentPlaylistElement = playlistElement
83 }
84
85 getCurrentElement () {
86 return this.currentPlaylistElement
87 }
88
89 getCurrentPosition () {
90 if (!this.currentPlaylistElement) return -1
91
92 return this.currentPlaylistElement.position
93 }
94 }