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