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