]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch-playlist.component.ts
Move video watch playlist in its own component
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch-playlist.component.ts
CommitLineData
72675ebe
C
1import { Component, Input } from '@angular/core'
2import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
3import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
4import { Video } from '@app/shared/video/video.model'
5import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
6import { VideoService } from '@app/shared/video/video.service'
7import { Router } from '@angular/router'
8import { AuthService } from '@app/core'
9
10@Component({
11 selector: 'my-video-watch-playlist',
12 templateUrl: './video-watch-playlist.component.html',
13 styleUrls: [ './video-watch-playlist.component.scss' ]
14})
15export class VideoWatchPlaylistComponent {
16 @Input() video: VideoDetails
17 @Input() playlist: VideoPlaylist
18
19 playlistVideos: Video[] = []
20 playlistPagination: ComponentPagination = {
21 currentPage: 1,
22 itemsPerPage: 30,
23 totalItems: null
24 }
25
26 noPlaylistVideos = false
27 currentPlaylistPosition = 1
28
29 constructor (
30 private auth: AuthService,
31 private videoService: VideoService,
32 private router: Router
33 ) {}
34
35 onPlaylistVideosNearOfBottom () {
36 // Last page
37 if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return
38
39 this.playlistPagination.currentPage += 1
40 this.loadPlaylistElements(this.playlist,false)
41 }
42
43 onElementRemoved (video: Video) {
44 this.playlistVideos = this.playlistVideos.filter(v => v.id !== video.id)
45
46 this.playlistPagination.totalItems--
47 }
48
49 isPlaylistOwned () {
50 return this.playlist.isLocal === true && this.playlist.ownerAccount.name === this.auth.getUser().username
51 }
52
53 isUnlistedPlaylist () {
54 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
55 }
56
57 isPrivatePlaylist () {
58 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
59 }
60
61 isPublicPlaylist () {
62 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
63 }
64
65 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
66 this.videoService.getPlaylistVideos(playlist.uuid, this.playlistPagination)
67 .subscribe(({ totalVideos, videos }) => {
68 this.playlistVideos = this.playlistVideos.concat(videos)
69 this.playlistPagination.totalItems = totalVideos
70
71 if (totalVideos === 0) {
72 this.noPlaylistVideos = true
73 return
74 }
75
76 this.updatePlaylistIndex(this.video)
77
78 if (redirectToFirst) {
79 const extras = {
80 queryParams: { videoId: this.playlistVideos[ 0 ].uuid },
81 replaceUrl: true
82 }
83 this.router.navigate([], extras)
84 }
85 })
86 }
87
88 updatePlaylistIndex (video: VideoDetails) {
89 if (this.playlistVideos.length === 0 || !video) return
90
91 for (const playlistVideo of this.playlistVideos) {
92 if (playlistVideo.id === video.id) {
93 this.currentPlaylistPosition = playlistVideo.playlistElement.position
94 return
95 }
96 }
97
98 // Load more videos to find our video
99 this.onPlaylistVideosNearOfBottom()
100 }
101
102 navigateToNextPlaylistVideo () {
103 if (this.currentPlaylistPosition < this.playlistPagination.totalItems) {
104 const next = this.playlistVideos.find(v => v.playlistElement.position === this.currentPlaylistPosition + 1)
105
106 const start = next.playlistElement.startTimestamp
107 const stop = next.playlistElement.stopTimestamp
108 this.router.navigate([],{ queryParams: { videoId: next.uuid, start, stop } })
109 }
110 }
111}