]>
Commit | Line | Data |
---|---|---|
f1a0f3b7 | 1 | import { VideoPlaylist, VideoPlaylistElement } from '../../../../../shared/models' |
42b40636 | 2 | import { logger } from '../../../root-helpers' |
f1a0f3b7 C |
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 | ||
7dcd7d81 | 35 | if (!next?.video) { |
f1a0f3b7 C |
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 | ||
7dcd7d81 | 55 | if (!prev?.video) { |
f1a0f3b7 C |
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) | |
7dcd7d81 | 71 | if (!this.currentPlaylistElement?.video) { |
42b40636 | 72 | logger.error('Current playlist element is not valid.', this.currentPlaylistElement) |
f1a0f3b7 C |
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 | } |