import { Component, Input } from '@angular/core'
import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
-import { Video } from '@app/shared/video/video.model'
import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
-import { VideoService } from '@app/shared/video/video.service'
import { Router } from '@angular/router'
import { AuthService } from '@app/core'
+import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
+import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
@Component({
selector: 'my-video-watch-playlist',
@Input() video: VideoDetails
@Input() playlist: VideoPlaylist
- playlistVideos: Video[] = []
+ playlistElements: VideoPlaylistElement[] = []
playlistPagination: ComponentPagination = {
currentPage: 1,
itemsPerPage: 30,
constructor (
private auth: AuthService,
- private videoService: VideoService,
+ private videoPlaylist: VideoPlaylistService,
private router: Router
) {}
this.loadPlaylistElements(this.playlist,false)
}
- onElementRemoved (video: Video) {
- this.playlistVideos = this.playlistVideos.filter(v => v.id !== video.id)
+ onElementRemoved (playlistElement: VideoPlaylistElement) {
+ this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
this.playlistPagination.totalItems--
}
}
loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
- this.videoService.getPlaylistVideos(playlist.uuid, this.playlistPagination)
- .subscribe(({ totalVideos, videos }) => {
- this.playlistVideos = this.playlistVideos.concat(videos)
- this.playlistPagination.totalItems = totalVideos
+ this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination)
+ .subscribe(({ total, data }) => {
+ this.playlistElements = this.playlistElements.concat(data)
+ this.playlistPagination.totalItems = total
- if (totalVideos === 0) {
+ const firstAvailableVideos = this.playlistElements.find(e => !!e.video)
+ if (!firstAvailableVideos) {
this.noPlaylistVideos = true
return
}
if (redirectToFirst) {
const extras = {
- queryParams: { videoId: this.playlistVideos[ 0 ].uuid },
+ queryParams: {
+ start: firstAvailableVideos.startTimestamp,
+ stop: firstAvailableVideos.stopTimestamp,
+ videoId: firstAvailableVideos.video.uuid
+ },
replaceUrl: true
}
this.router.navigate([], extras)
}
updatePlaylistIndex (video: VideoDetails) {
- if (this.playlistVideos.length === 0 || !video) return
+ if (this.playlistElements.length === 0 || !video) return
- for (const playlistVideo of this.playlistVideos) {
- if (playlistVideo.id === video.id) {
- this.currentPlaylistPosition = playlistVideo.playlistElement.position
+ for (const playlistElement of this.playlistElements) {
+ if (playlistElement.video && playlistElement.video.id === video.id) {
+ this.currentPlaylistPosition = playlistElement.position
return
}
}
navigateToNextPlaylistVideo () {
if (this.currentPlaylistPosition < this.playlistPagination.totalItems) {
- const next = this.playlistVideos.find(v => v.playlistElement.position === this.currentPlaylistPosition + 1)
+ const next = this.playlistElements.find(e => e.position === this.currentPlaylistPosition + 1)
- const start = next.playlistElement.startTimestamp
- const stop = next.playlistElement.stopTimestamp
- this.router.navigate([],{ queryParams: { videoId: next.uuid, start, stop } })
+ if (!next || !next.video) {
+ this.currentPlaylistPosition++
+ this.navigateToNextPlaylistVideo()
+ return
+ }
+
+ const start = next.startTimestamp
+ const stop = next.stopTimestamp
+ this.router.navigate([],{ queryParams: { videoId: next.video.uuid, start, stop } })
}
}
}