]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch-playlist.component.ts
autoplay next video switch for both user and visitors
[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'
72675ebe 4import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models'
72675ebe
C
5import { Router } from '@angular/router'
6import { AuthService } from '@app/core'
bfbd9128
C
7import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
8import { VideoPlaylistElement } from '@app/shared/video-playlist/video-playlist-element.model'
72675ebe
C
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
bfbd9128 19 playlistElements: VideoPlaylistElement[] = []
72675ebe
C
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,
bfbd9128 31 private videoPlaylist: VideoPlaylistService,
72675ebe
C
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
bfbd9128
C
43 onElementRemoved (playlistElement: VideoPlaylistElement) {
44 this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id)
72675ebe
C
45
46 this.playlistPagination.totalItems--
47 }
48
49 isPlaylistOwned () {
1acd784c
C
50 return this.playlist.isLocal === true &&
51 this.auth.isLoggedIn() &&
52 this.playlist.ownerAccount.name === this.auth.getUser().username
72675ebe
C
53 }
54
55 isUnlistedPlaylist () {
56 return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED
57 }
58
59 isPrivatePlaylist () {
60 return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE
61 }
62
63 isPublicPlaylist () {
64 return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC
65 }
66
67 loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) {
bfbd9128 68 this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination)
93cae479 69 .subscribe(({ total, data }) => {
bfbd9128 70 this.playlistElements = this.playlistElements.concat(data)
93cae479 71 this.playlistPagination.totalItems = total
72675ebe 72
bfbd9128
C
73 const firstAvailableVideos = this.playlistElements.find(e => !!e.video)
74 if (!firstAvailableVideos) {
72675ebe
C
75 this.noPlaylistVideos = true
76 return
77 }
78
79 this.updatePlaylistIndex(this.video)
80
81 if (redirectToFirst) {
82 const extras = {
f1d9b2d6
C
83 queryParams: {
84 start: firstAvailableVideos.startTimestamp,
85 stop: firstAvailableVideos.stopTimestamp,
86 videoId: firstAvailableVideos.video.uuid
87 },
72675ebe
C
88 replaceUrl: true
89 }
90 this.router.navigate([], extras)
91 }
92 })
93 }
94
95 updatePlaylistIndex (video: VideoDetails) {
bfbd9128 96 if (this.playlistElements.length === 0 || !video) return
72675ebe 97
bfbd9128
C
98 for (const playlistElement of this.playlistElements) {
99 if (playlistElement.video && playlistElement.video.id === video.id) {
100 this.currentPlaylistPosition = playlistElement.position
72675ebe
C
101 return
102 }
103 }
104
105 // Load more videos to find our video
106 this.onPlaylistVideosNearOfBottom()
107 }
108
109 navigateToNextPlaylistVideo () {
110 if (this.currentPlaylistPosition < this.playlistPagination.totalItems) {
bfbd9128 111 const next = this.playlistElements.find(e => e.position === this.currentPlaylistPosition + 1)
72675ebe 112
bfbd9128
C
113 if (!next || !next.video) {
114 this.currentPlaylistPosition++
115 this.navigateToNextPlaylistVideo()
116 return
117 }
118
119 const start = next.startTimestamp
120 const stop = next.stopTimestamp
121 this.router.navigate([],{ queryParams: { videoId: next.video.uuid, start, stop } })
72675ebe
C
122 }
123 }
124}