]>
Commit | Line | Data |
---|---|---|
1 | import { Component, EventEmitter, Input, Output } from '@angular/core' | |
2 | import { Router } from '@angular/router' | |
3 | import { AuthService, ComponentPagination, LocalStorageService, Notifier, SessionStorageService, UserService } from '@app/core' | |
4 | import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' | |
5 | import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage' | |
6 | import { VideoPlaylistPrivacy } from '@shared/models' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-video-watch-playlist', | |
10 | templateUrl: './video-watch-playlist.component.html', | |
11 | styleUrls: [ './video-watch-playlist.component.scss' ] | |
12 | }) | |
13 | export class VideoWatchPlaylistComponent { | |
14 | static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist' | |
15 | static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'loop_playlist' | |
16 | ||
17 | @Input() playlist: VideoPlaylist | |
18 | ||
19 | @Output() videoFound = new EventEmitter<string>() | |
20 | ||
21 | playlistElements: VideoPlaylistElement[] = [] | |
22 | playlistPagination: ComponentPagination = { | |
23 | currentPage: 1, | |
24 | itemsPerPage: 30, | |
25 | totalItems: null | |
26 | } | |
27 | ||
28 | autoPlayNextVideoPlaylist: boolean | |
29 | autoPlayNextVideoPlaylistSwitchText = '' | |
30 | loopPlaylist: boolean | |
31 | loopPlaylistSwitchText = '' | |
32 | noPlaylistVideos = false | |
33 | ||
34 | currentPlaylistPosition: number | |
35 | ||
36 | constructor ( | |
37 | private userService: UserService, | |
38 | private auth: AuthService, | |
39 | private notifier: Notifier, | |
40 | private videoPlaylist: VideoPlaylistService, | |
41 | private localStorageService: LocalStorageService, | |
42 | private sessionStorage: SessionStorageService, | |
43 | private router: Router | |
44 | ) { | |
45 | // defaults to true | |
46 | this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn() | |
47 | ? this.auth.getUser().autoPlayNextVideoPlaylist | |
48 | : this.localStorageService.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false' | |
49 | ||
50 | this.setAutoPlayNextVideoPlaylistSwitchText() | |
51 | ||
52 | // defaults to false | |
53 | this.loopPlaylist = this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true' | |
54 | this.setLoopPlaylistSwitchText() | |
55 | } | |
56 | ||
57 | onPlaylistVideosNearOfBottom (position?: number) { | |
58 | // Last page | |
59 | if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return | |
60 | ||
61 | this.playlistPagination.currentPage += 1 | |
62 | this.loadPlaylistElements(this.playlist, false, position) | |
63 | } | |
64 | ||
65 | onElementRemoved (playlistElement: VideoPlaylistElement) { | |
66 | this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id) | |
67 | ||
68 | this.playlistPagination.totalItems-- | |
69 | } | |
70 | ||
71 | isPlaylistOwned () { | |
72 | return this.playlist.isLocal === true && | |
73 | this.auth.isLoggedIn() && | |
74 | this.playlist.ownerAccount.name === this.auth.getUser().username | |
75 | } | |
76 | ||
77 | isUnlistedPlaylist () { | |
78 | return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED | |
79 | } | |
80 | ||
81 | isPrivatePlaylist () { | |
82 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE | |
83 | } | |
84 | ||
85 | isPublicPlaylist () { | |
86 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC | |
87 | } | |
88 | ||
89 | loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false, position?: number) { | |
90 | this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination) | |
91 | .subscribe(({ total, data }) => { | |
92 | this.playlistElements = this.playlistElements.concat(data) | |
93 | this.playlistPagination.totalItems = total | |
94 | ||
95 | const firstAvailableVideo = this.playlistElements.find(e => !!e.video) | |
96 | if (!firstAvailableVideo) { | |
97 | this.noPlaylistVideos = true | |
98 | return | |
99 | } | |
100 | ||
101 | if (position) this.updatePlaylistIndex(position) | |
102 | ||
103 | if (redirectToFirst) { | |
104 | const extras = { | |
105 | queryParams: { | |
106 | start: firstAvailableVideo.startTimestamp, | |
107 | stop: firstAvailableVideo.stopTimestamp, | |
108 | playlistPosition: firstAvailableVideo.position | |
109 | }, | |
110 | replaceUrl: true | |
111 | } | |
112 | this.router.navigate([], extras) | |
113 | } | |
114 | }) | |
115 | } | |
116 | ||
117 | updatePlaylistIndex (position: number) { | |
118 | if (this.playlistElements.length === 0 || !position) return | |
119 | ||
120 | // Handle the reverse index | |
121 | if (position < 0) position = this.playlist.videosLength + position + 1 | |
122 | ||
123 | for (const playlistElement of this.playlistElements) { | |
124 | // >= if the previous videos were not valid | |
125 | if (playlistElement.video && playlistElement.position >= position) { | |
126 | this.currentPlaylistPosition = playlistElement.position | |
127 | ||
128 | this.videoFound.emit(playlistElement.video.uuid) | |
129 | ||
130 | setTimeout(() => { | |
131 | document.querySelector('.element-' + this.currentPlaylistPosition).scrollIntoView(false) | |
132 | }) | |
133 | ||
134 | return | |
135 | } | |
136 | } | |
137 | ||
138 | // Load more videos to find our video | |
139 | this.onPlaylistVideosNearOfBottom(position) | |
140 | } | |
141 | ||
142 | navigateToPreviousPlaylistVideo () { | |
143 | const previous = this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous') | |
144 | if (!previous) return | |
145 | ||
146 | const start = previous.startTimestamp | |
147 | const stop = previous.stopTimestamp | |
148 | this.router.navigate([], { queryParams: { playlistPosition: previous.position, start, stop } }) | |
149 | } | |
150 | ||
151 | findPlaylistVideo (position: number, type: 'previous' | 'next'): VideoPlaylistElement { | |
152 | if ( | |
153 | (type === 'next' && position > this.playlistPagination.totalItems) || | |
154 | (type === 'previous' && position < 1) | |
155 | ) { | |
156 | // End of the playlist: end the recursion if we're not in the loop mode | |
157 | if (!this.loopPlaylist) return | |
158 | ||
159 | // Loop mode | |
160 | position = type === 'previous' | |
161 | ? this.playlistPagination.totalItems | |
162 | : 1 | |
163 | } | |
164 | ||
165 | const found = this.playlistElements.find(e => e.position === position) | |
166 | if (found?.video) return found | |
167 | ||
168 | const newPosition = type === 'previous' | |
169 | ? position - 1 | |
170 | : position + 1 | |
171 | ||
172 | return this.findPlaylistVideo(newPosition, type) | |
173 | } | |
174 | ||
175 | navigateToNextPlaylistVideo () { | |
176 | const next = this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next') | |
177 | if (!next) return | |
178 | ||
179 | const start = next.startTimestamp | |
180 | const stop = next.stopTimestamp | |
181 | this.router.navigate([], { queryParams: { playlistPosition: next.position, start, stop } }) | |
182 | } | |
183 | ||
184 | switchAutoPlayNextVideoPlaylist () { | |
185 | this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist | |
186 | this.setAutoPlayNextVideoPlaylistSwitchText() | |
187 | ||
188 | peertubeLocalStorage.setItem( | |
189 | VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, | |
190 | this.autoPlayNextVideoPlaylist.toString() | |
191 | ) | |
192 | ||
193 | if (this.auth.isLoggedIn()) { | |
194 | const details = { | |
195 | autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist | |
196 | } | |
197 | ||
198 | this.userService.updateMyProfile(details) | |
199 | .subscribe({ | |
200 | next: () => { | |
201 | this.auth.refreshUserInformation() | |
202 | }, | |
203 | ||
204 | error: err => this.notifier.error(err.message) | |
205 | }) | |
206 | } | |
207 | } | |
208 | ||
209 | switchLoopPlaylist () { | |
210 | this.loopPlaylist = !this.loopPlaylist | |
211 | this.setLoopPlaylistSwitchText() | |
212 | ||
213 | peertubeSessionStorage.setItem( | |
214 | VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, | |
215 | this.loopPlaylist.toString() | |
216 | ) | |
217 | } | |
218 | ||
219 | private setAutoPlayNextVideoPlaylistSwitchText () { | |
220 | this.autoPlayNextVideoPlaylistSwitchText = this.autoPlayNextVideoPlaylist | |
221 | ? $localize`Stop autoplaying next video` | |
222 | : $localize`Autoplay next video` | |
223 | } | |
224 | ||
225 | private setLoopPlaylistSwitchText () { | |
226 | this.loopPlaylistSwitchText = this.loopPlaylist | |
227 | ? $localize`Stop looping playlist videos` | |
228 | : $localize`Loop playlist videos` | |
229 | } | |
230 | } |