1 import { Component, EventEmitter, Input, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { AuthService, ComponentPagination, HooksService, Notifier, SessionStorageService, UserService } from '@app/core'
4 import { isInViewport } from '@app/helpers'
5 import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist'
6 import { getBoolOrDefault } from '@root-helpers/local-storage-utils'
7 import { peertubeSessionStorage } from '@root-helpers/peertube-web-storage'
8 import { VideoPlaylistPrivacy } from '@shared/models'
11 selector: 'my-video-watch-playlist',
12 templateUrl: './video-watch-playlist.component.html',
13 styleUrls: [ './video-watch-playlist.component.scss' ]
15 export class VideoWatchPlaylistComponent {
16 static SESSION_STORAGE_LOOP_PLAYLIST = 'loop_playlist'
18 @Input() playlist: VideoPlaylist
20 @Output() videoFound = new EventEmitter<string>()
22 playlistElements: VideoPlaylistElement[] = []
23 playlistPagination: ComponentPagination = {
29 autoPlayNextVideoPlaylist: boolean
30 autoPlayNextVideoPlaylistSwitchText = ''
32 loopPlaylistSwitchText = ''
33 noPlaylistVideos = false
35 currentPlaylistPosition: number
38 private hooks: HooksService,
39 private userService: UserService,
40 private auth: AuthService,
41 private notifier: Notifier,
42 private videoPlaylist: VideoPlaylistService,
43 private sessionStorage: SessionStorageService,
44 private router: Router
46 this.userService.getAnonymousOrLoggedUser()
47 .subscribe(user => this.autoPlayNextVideoPlaylist = user.autoPlayNextVideoPlaylist)
49 this.setAutoPlayNextVideoPlaylistSwitchText()
51 this.loopPlaylist = getBoolOrDefault(this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST), false)
52 this.setLoopPlaylistSwitchText()
55 onPlaylistVideosNearOfBottom (position?: number) {
57 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
59 this.playlistPagination.currentPage += 1
60 this.loadPlaylistElements(this.playlist, false, position)
63 onElementRemoved (playlistElement: VideoPlaylistElement) {
64 this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
66 this.playlistPagination.totalItems--
70 return this.playlist.isLocal === true &&
71 this.auth.isLoggedIn() &&
72 this.playlist.ownerAccount.name === this.auth.getUser().username
75 isUnlistedPlaylist () {
76 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
79 isPrivatePlaylist () {
80 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
84 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
87 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false, position?: number) {
88 const obs = this.hooks.wrapObsFun(
89 this.videoPlaylist.getPlaylistVideos.bind(this.videoPlaylist),
90 { videoPlaylistId: playlist.uuid, componentPagination: this.playlistPagination },
92 'filter:api.video-watch.video-playlist-elements.get.params',
93 'filter:api.video-watch.video-playlist-elements.get.result'
96 obs.subscribe(({ total, data: playlistElements }) => {
97 this.playlistElements = this.playlistElements.concat(playlistElements)
98 this.playlistPagination.totalItems = total
100 const firstAvailableVideo = this.playlistElements.find(e => !!e.video)
101 if (!firstAvailableVideo) {
102 this.noPlaylistVideos = true
106 if (position) this.updatePlaylistIndex(position)
108 if (redirectToFirst) {
111 start: firstAvailableVideo.startTimestamp,
112 stop: firstAvailableVideo.stopTimestamp,
113 playlistPosition: firstAvailableVideo.position
117 this.router.navigate([], extras)
122 updatePlaylistIndex (position: number) {
123 if (this.playlistElements.length === 0 || !position) return
125 // Handle the reverse index
126 if (position < 0) position = this.playlist.videosLength + position + 1
128 for (const playlistElement of this.playlistElements) {
129 // >= if the previous videos were not valid
130 if (playlistElement.video && playlistElement.position >= position) {
131 this.currentPlaylistPosition = playlistElement.position
133 this.videoFound.emit(playlistElement.video.uuid)
136 const element = document.querySelector<HTMLElement>('.element-' + this.currentPlaylistPosition)
137 const container = document.querySelector<HTMLElement>('.playlist')
139 if (isInViewport(element, container)) return
141 container.scrollTop = element.offsetTop
148 // Load more videos to find our video
149 this.onPlaylistVideosNearOfBottom(position)
152 hasPreviousVideo () {
153 return !!this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous')
157 return !!this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next')
160 navigateToPreviousPlaylistVideo () {
161 const previous = this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous')
162 if (!previous) return
164 const start = previous.startTimestamp
165 const stop = previous.stopTimestamp
166 this.router.navigate([], { queryParams: { playlistPosition: previous.position, start, stop } })
169 findPlaylistVideo (position: number, type: 'previous' | 'next'): VideoPlaylistElement {
171 (type === 'next' && position > this.playlistPagination.totalItems) ||
172 (type === 'previous' && position < 1)
174 // End of the playlist: end the recursion if we're not in the loop mode
175 if (!this.loopPlaylist) return
178 position = type === 'previous'
179 ? this.playlistPagination.totalItems
183 const found = this.playlistElements.find(e => e.position === position)
184 if (found?.video) return found
186 const newPosition = type === 'previous'
190 return this.findPlaylistVideo(newPosition, type)
193 navigateToNextPlaylistVideo () {
194 const next = this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next')
197 const start = next.startTimestamp
198 const stop = next.stopTimestamp
199 this.router.navigate([], { queryParams: { playlistPosition: next.position, start, stop } })
202 switchAutoPlayNextVideoPlaylist () {
203 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist
204 this.setAutoPlayNextVideoPlaylistSwitchText()
206 const details = { autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist }
208 if (this.auth.isLoggedIn()) {
209 this.userService.updateMyProfile(details)
212 this.auth.refreshUserInformation()
215 error: err => this.notifier.error(err.message)
218 this.userService.updateMyAnonymousProfile(details)
222 switchLoopPlaylist () {
223 this.loopPlaylist = !this.loopPlaylist
224 this.setLoopPlaylistSwitchText()
226 peertubeSessionStorage.setItem(
227 VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST,
228 this.loopPlaylist.toString()
232 private setAutoPlayNextVideoPlaylistSwitchText () {
233 this.autoPlayNextVideoPlaylistSwitchText = this.autoPlayNextVideoPlaylist
234 ? $localize`Stop autoplaying next video`
235 : $localize`Autoplay next video`
238 private setLoopPlaylistSwitchText () {
239 this.loopPlaylistSwitchText = this.loopPlaylist
240 ? $localize`Stop looping playlist videos`
241 : $localize`Loop playlist videos`