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>()
21 @Output() noVideoFound = new EventEmitter<void>()
23 playlistElements: VideoPlaylistElement[] = []
24 playlistPagination: ComponentPagination = {
30 autoPlayNextVideoPlaylist: boolean
31 autoPlayNextVideoPlaylistSwitchText = ''
34 loopPlaylistSwitchText = ''
36 noPlaylistVideos = false
37 currentPlaylistPosition: number
40 private hooks: HooksService,
41 private userService: UserService,
42 private auth: AuthService,
43 private notifier: Notifier,
44 private videoPlaylist: VideoPlaylistService,
45 private sessionStorage: SessionStorageService,
46 private router: Router
48 this.userService.getAnonymousOrLoggedUser()
49 .subscribe(user => this.autoPlayNextVideoPlaylist = user.autoPlayNextVideoPlaylist)
51 this.setAutoPlayNextVideoPlaylistSwitchText()
53 this.loopPlaylist = getBoolOrDefault(this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST), false)
54 this.setLoopPlaylistSwitchText()
57 onPlaylistVideosNearOfBottom (position?: number) {
59 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
61 this.playlistPagination.currentPage += 1
62 this.loadPlaylistElements(this.playlist, false, position)
65 onElementRemoved (playlistElement: VideoPlaylistElement) {
66 this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
68 this.playlistPagination.totalItems--
72 return this.playlist.isLocal === true &&
73 this.auth.isLoggedIn() &&
74 this.playlist.ownerAccount.name === this.auth.getUser().username
77 isUnlistedPlaylist () {
78 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
81 isPrivatePlaylist () {
82 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
86 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
89 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false, position?: number) {
90 const obs = this.hooks.wrapObsFun(
91 this.videoPlaylist.getPlaylistVideos.bind(this.videoPlaylist),
92 { videoPlaylistId: playlist.uuid, componentPagination: this.playlistPagination },
94 'filter:api.video-watch.video-playlist-elements.get.params',
95 'filter:api.video-watch.video-playlist-elements.get.result'
98 obs.subscribe(({ total, data: playlistElements }) => {
99 this.playlistElements = this.playlistElements.concat(playlistElements)
100 this.playlistPagination.totalItems = total
102 const firstAvailableVideo = this.playlistElements.find(e => !!e.video)
103 if (!firstAvailableVideo) {
104 this.noPlaylistVideos = true
105 this.noVideoFound.emit()
109 if (position) this.updatePlaylistIndex(position)
111 if (redirectToFirst) {
114 start: firstAvailableVideo.startTimestamp,
115 stop: firstAvailableVideo.stopTimestamp,
116 playlistPosition: firstAvailableVideo.position
120 this.router.navigate([], extras)
125 updatePlaylistIndex (position: number) {
126 if (this.playlistElements.length === 0 || !position) return
128 // Handle the reverse index
129 if (position < 0) position = this.playlist.videosLength + position + 1
131 for (const playlistElement of this.playlistElements) {
132 // >= if the previous videos were not valid
133 if (playlistElement.video && playlistElement.position >= position) {
134 this.currentPlaylistPosition = playlistElement.position
136 this.videoFound.emit(playlistElement.video.uuid)
139 const element = document.querySelector<HTMLElement>('.element-' + this.currentPlaylistPosition)
140 const container = document.querySelector<HTMLElement>('.playlist')
142 if (isInViewport(element, container)) return
144 container.scrollTop = element.offsetTop
151 // Load more videos to find our video
152 this.onPlaylistVideosNearOfBottom(position)
155 hasPreviousVideo () {
156 return !!this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous')
160 return !!this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next')
163 navigateToPreviousPlaylistVideo () {
164 const previous = this.findPlaylistVideo(this.currentPlaylistPosition - 1, 'previous')
165 if (!previous) return
167 const start = previous.startTimestamp
168 const stop = previous.stopTimestamp
169 this.router.navigate([], { queryParams: { playlistPosition: previous.position, start, stop } })
172 findPlaylistVideo (position: number, type: 'previous' | 'next'): VideoPlaylistElement {
174 (type === 'next' && position > this.playlistPagination.totalItems) ||
175 (type === 'previous' && position < 1)
177 // End of the playlist: end the recursion if we're not in the loop mode
178 if (!this.loopPlaylist) return
181 position = type === 'previous'
182 ? this.playlistPagination.totalItems
186 const found = this.playlistElements.find(e => e.position === position)
187 if (found?.video) return found
189 const newPosition = type === 'previous'
193 return this.findPlaylistVideo(newPosition, type)
196 navigateToNextPlaylistVideo () {
197 const next = this.findPlaylistVideo(this.currentPlaylistPosition + 1, 'next')
200 const start = next.startTimestamp
201 const stop = next.stopTimestamp
202 this.router.navigate([], { queryParams: { playlistPosition: next.position, start, stop } })
205 switchAutoPlayNextVideoPlaylist () {
206 this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist
207 this.setAutoPlayNextVideoPlaylistSwitchText()
209 const details = { autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist }
211 if (this.auth.isLoggedIn()) {
212 this.userService.updateMyProfile(details)
215 this.auth.refreshUserInformation()
218 error: err => this.notifier.error(err.message)
221 this.userService.updateMyAnonymousProfile(details)
225 switchLoopPlaylist () {
226 this.loopPlaylist = !this.loopPlaylist
227 this.setLoopPlaylistSwitchText()
229 peertubeSessionStorage.setItem(
230 VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST,
231 this.loopPlaylist.toString()
235 private setAutoPlayNextVideoPlaylistSwitchText () {
236 this.autoPlayNextVideoPlaylistSwitchText = this.autoPlayNextVideoPlaylist
237 ? $localize`Stop autoplaying next video`
238 : $localize`Autoplay next video`
241 private setLoopPlaylistSwitchText () {
242 this.loopPlaylistSwitchText = this.loopPlaylist
243 ? $localize`Stop looping playlist videos`
244 : $localize`Loop playlist videos`