1 import { Component, Input } from '@angular/core'
2 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
3 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
4 import { Video } from '@app/shared/video/video.model'
5 import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
6 import { VideoService } from '@app/shared/video/video.service'
7 import { Router } from '@angular/router'
8 import { AuthService } from '@app/core'
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 @Input() video: VideoDetails
17 @Input() playlist: VideoPlaylist
19 playlistVideos: Video[] = []
20 playlistPagination: ComponentPagination = {
26 noPlaylistVideos = false
27 currentPlaylistPosition = 1
30 private auth: AuthService,
31 private videoService: VideoService,
32 private router: Router
35 onPlaylistVideosNearOfBottom () {
37 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
39 this.playlistPagination.currentPage += 1
40 this.loadPlaylistElements(this.playlist,false)
43 onElementRemoved (video: Video) {
44 this.playlistVideos = this.playlistVideos.filter(v => v.id !== video.id)
46 this.playlistPagination.totalItems--
50 return this.playlist.isLocal === true &&
51 this.auth.isLoggedIn() &&
52 this.playlist.ownerAccount.name === this.auth.getUser().username
55 isUnlistedPlaylist () {
56 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
59 isPrivatePlaylist () {
60 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
64 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
67 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
68 this.videoService.getPlaylistVideos(playlist.uuid, this.playlistPagination)
69 .subscribe(({ totalVideos, videos }) => {
70 this.playlistVideos = this.playlistVideos.concat(videos)
71 this.playlistPagination.totalItems = totalVideos
73 if (totalVideos === 0) {
74 this.noPlaylistVideos = true
78 this.updatePlaylistIndex(this.video)
80 if (redirectToFirst) {
82 queryParams: { videoId: this.playlistVideos[ 0 ].uuid },
85 this.router.navigate([], extras)
90 updatePlaylistIndex (video: VideoDetails) {
91 if (this.playlistVideos.length === 0 || !video) return
93 for (const playlistVideo of this.playlistVideos) {
94 if (playlistVideo.id === video.id) {
95 this.currentPlaylistPosition = playlistVideo.playlistElement.position
100 // Load more videos to find our video
101 this.onPlaylistVideosNearOfBottom()
104 navigateToNextPlaylistVideo () {
105 if (this.currentPlaylistPosition < this.playlistPagination.totalItems) {
106 const next = this.playlistVideos.find(v => v.playlistElement.position === this.currentPlaylistPosition + 1)
108 const start = next.playlistElement.startTimestamp
109 const stop = next.playlistElement.stopTimestamp
110 this.router.navigate([],{ queryParams: { videoId: next.uuid, start, stop } })