]>
Commit | Line | Data |
---|---|---|
72675ebe | 1 | import { Component, Input } from '@angular/core' |
72675ebe | 2 | import { Router } from '@angular/router' |
67ed6552 | 3 | import { AuthService, ComponentPagination, LocalStorageService, Notifier, SessionStorageService, UserService } from '@app/core' |
67ed6552 | 4 | import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' |
66357162 | 5 | import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage' |
67ed6552 | 6 | import { VideoDetails, VideoPlaylistPrivacy } from '@shared/models' |
72675ebe C |
7 | |
8 | @Component({ | |
9 | selector: 'my-video-watch-playlist', | |
10 | templateUrl: './video-watch-playlist.component.html', | |
11 | styleUrls: [ './video-watch-playlist.component.scss' ] | |
12 | }) | |
13 | export class VideoWatchPlaylistComponent { | |
bee29df8 | 14 | static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist' |
88a7f93f | 15 | static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'loop_playlist' |
bee29df8 | 16 | |
72675ebe C |
17 | @Input() video: VideoDetails |
18 | @Input() playlist: VideoPlaylist | |
19 | ||
bfbd9128 | 20 | playlistElements: VideoPlaylistElement[] = [] |
72675ebe C |
21 | playlistPagination: ComponentPagination = { |
22 | currentPage: 1, | |
23 | itemsPerPage: 30, | |
24 | totalItems: null | |
25 | } | |
26 | ||
bee29df8 RK |
27 | autoPlayNextVideoPlaylist: boolean |
28 | autoPlayNextVideoPlaylistSwitchText = '' | |
88a7f93f RK |
29 | loopPlaylist: boolean |
30 | loopPlaylistSwitchText = '' | |
72675ebe C |
31 | noPlaylistVideos = false |
32 | currentPlaylistPosition = 1 | |
33 | ||
34 | constructor ( | |
bee29df8 | 35 | private userService: UserService, |
72675ebe | 36 | private auth: AuthService, |
bee29df8 | 37 | private notifier: Notifier, |
bfbd9128 | 38 | private videoPlaylist: VideoPlaylistService, |
d3217560 RK |
39 | private localStorageService: LocalStorageService, |
40 | private sessionStorageService: SessionStorageService, | |
72675ebe | 41 | private router: Router |
bee29df8 | 42 | ) { |
706c5a47 | 43 | // defaults to true |
bee29df8 RK |
44 | this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn() |
45 | ? this.auth.getUser().autoPlayNextVideoPlaylist | |
d3217560 | 46 | : this.localStorageService.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false' |
bee29df8 | 47 | this.setAutoPlayNextVideoPlaylistSwitchText() |
88a7f93f | 48 | |
706c5a47 | 49 | // defaults to false |
d3217560 | 50 | this.loopPlaylist = this.sessionStorageService.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true' |
88a7f93f | 51 | this.setLoopPlaylistSwitchText() |
bee29df8 | 52 | } |
72675ebe C |
53 | |
54 | onPlaylistVideosNearOfBottom () { | |
55 | // Last page | |
56 | if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return | |
57 | ||
58 | this.playlistPagination.currentPage += 1 | |
59 | this.loadPlaylistElements(this.playlist,false) | |
60 | } | |
61 | ||
bfbd9128 C |
62 | onElementRemoved (playlistElement: VideoPlaylistElement) { |
63 | this.playlistElements = this.playlistElements.filter(e => e.id !== playlistElement.id) | |
72675ebe C |
64 | |
65 | this.playlistPagination.totalItems-- | |
66 | } | |
67 | ||
68 | isPlaylistOwned () { | |
1acd784c C |
69 | return this.playlist.isLocal === true && |
70 | this.auth.isLoggedIn() && | |
71 | this.playlist.ownerAccount.name === this.auth.getUser().username | |
72675ebe C |
72 | } |
73 | ||
74 | isUnlistedPlaylist () { | |
75 | return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED | |
76 | } | |
77 | ||
78 | isPrivatePlaylist () { | |
79 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE | |
80 | } | |
81 | ||
82 | isPublicPlaylist () { | |
83 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC | |
84 | } | |
85 | ||
86 | loadPlaylistElements (playlist: VideoPlaylist, redirectToFirst = false) { | |
bfbd9128 | 87 | this.videoPlaylist.getPlaylistVideos(playlist.uuid, this.playlistPagination) |
93cae479 | 88 | .subscribe(({ total, data }) => { |
bfbd9128 | 89 | this.playlistElements = this.playlistElements.concat(data) |
93cae479 | 90 | this.playlistPagination.totalItems = total |
72675ebe | 91 | |
bfbd9128 C |
92 | const firstAvailableVideos = this.playlistElements.find(e => !!e.video) |
93 | if (!firstAvailableVideos) { | |
72675ebe C |
94 | this.noPlaylistVideos = true |
95 | return | |
96 | } | |
97 | ||
98 | this.updatePlaylistIndex(this.video) | |
99 | ||
100 | if (redirectToFirst) { | |
101 | const extras = { | |
f1d9b2d6 C |
102 | queryParams: { |
103 | start: firstAvailableVideos.startTimestamp, | |
104 | stop: firstAvailableVideos.stopTimestamp, | |
105 | videoId: firstAvailableVideos.video.uuid | |
106 | }, | |
72675ebe C |
107 | replaceUrl: true |
108 | } | |
109 | this.router.navigate([], extras) | |
110 | } | |
111 | }) | |
112 | } | |
113 | ||
114 | updatePlaylistIndex (video: VideoDetails) { | |
bfbd9128 | 115 | if (this.playlistElements.length === 0 || !video) return |
72675ebe | 116 | |
bfbd9128 C |
117 | for (const playlistElement of this.playlistElements) { |
118 | if (playlistElement.video && playlistElement.video.id === video.id) { | |
119 | this.currentPlaylistPosition = playlistElement.position | |
72675ebe C |
120 | return |
121 | } | |
122 | } | |
123 | ||
124 | // Load more videos to find our video | |
125 | this.onPlaylistVideosNearOfBottom() | |
126 | } | |
127 | ||
706c5a47 RK |
128 | findNextPlaylistVideo (position = this.currentPlaylistPosition): VideoPlaylistElement { |
129 | if (this.currentPlaylistPosition >= this.playlistPagination.totalItems) { | |
130 | // we have reached the end of the playlist: either loop or stop | |
131 | if (this.loopPlaylist) { | |
132 | this.currentPlaylistPosition = position = 0 | |
133 | } else { | |
bfbd9128 C |
134 | return |
135 | } | |
706c5a47 RK |
136 | } |
137 | ||
138 | const next = this.playlistElements.find(e => e.position === position) | |
bfbd9128 | 139 | |
706c5a47 RK |
140 | if (!next || !next.video) { |
141 | return this.findNextPlaylistVideo(position + 1) | |
72675ebe | 142 | } |
706c5a47 RK |
143 | |
144 | return next | |
145 | } | |
146 | ||
147 | navigateToNextPlaylistVideo () { | |
148 | const next = this.findNextPlaylistVideo(this.currentPlaylistPosition + 1) | |
149 | if (!next) return | |
150 | const start = next.startTimestamp | |
151 | const stop = next.stopTimestamp | |
152 | this.router.navigate([],{ queryParams: { videoId: next.video.uuid, start, stop } }) | |
72675ebe | 153 | } |
bee29df8 RK |
154 | |
155 | switchAutoPlayNextVideoPlaylist () { | |
156 | this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist | |
157 | this.setAutoPlayNextVideoPlaylistSwitchText() | |
158 | ||
159 | peertubeLocalStorage.setItem( | |
160 | VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, | |
161 | this.autoPlayNextVideoPlaylist.toString() | |
162 | ) | |
163 | ||
164 | if (this.auth.isLoggedIn()) { | |
165 | const details = { | |
166 | autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist | |
167 | } | |
168 | ||
169 | this.userService.updateMyProfile(details).subscribe( | |
170 | () => { | |
171 | this.auth.refreshUserInformation() | |
172 | }, | |
173 | err => this.notifier.error(err.message) | |
174 | ) | |
175 | } | |
176 | } | |
177 | ||
88a7f93f RK |
178 | switchLoopPlaylist () { |
179 | this.loopPlaylist = !this.loopPlaylist | |
180 | this.setLoopPlaylistSwitchText() | |
181 | ||
182 | peertubeSessionStorage.setItem( | |
183 | VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, | |
184 | this.loopPlaylist.toString() | |
185 | ) | |
186 | } | |
187 | ||
bee29df8 | 188 | private setAutoPlayNextVideoPlaylistSwitchText () { |
88a7f93f | 189 | this.autoPlayNextVideoPlaylistSwitchText = this.autoPlayNextVideoPlaylist |
66357162 C |
190 | ? $localize`Stop autoplaying next video` |
191 | : $localize`Autoplay next video` | |
88a7f93f RK |
192 | } |
193 | ||
194 | private setLoopPlaylistSwitchText () { | |
195 | this.loopPlaylistSwitchText = this.loopPlaylist | |
66357162 C |
196 | ? $localize`Stop looping playlist videos` |
197 | : $localize`Loop playlist videos` | |
bee29df8 | 198 | } |
72675ebe | 199 | } |