1 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core'
2 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
3 import { AuthService, Notifier } from '@app/core'
4 import { Subject, Subscription } from 'rxjs'
5 import { debounceTime, filter } from 'rxjs/operators'
6 import { Video, VideoPlaylistCreate, VideoPlaylistElementCreate, VideoPlaylistPrivacy } from '@shared/models'
7 import { FormReactive, FormValidatorService, VideoPlaylistValidatorsService } from '@app/shared/forms'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { secondsToTime } from '../../../assets/player/utils'
10 import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model'
11 import * as debug from 'debug'
12 import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
13 import { VideoExistInPlaylist } from '@shared/models/videos/playlist/video-exist-in-playlist.model'
15 const logger = debug('peertube:playlists:VideoAddToPlaylistComponent')
17 type PlaylistSummary = {
22 playlistElementId?: number
23 startTimestamp?: number
24 stopTimestamp?: number
28 selector: 'my-video-add-to-playlist',
29 styleUrls: [ './video-add-to-playlist.component.scss' ],
30 templateUrl: './video-add-to-playlist.component.html',
31 changeDetection: ChangeDetectionStrategy.OnPush
33 export class VideoAddToPlaylistComponent extends FormReactive implements OnInit, OnChanges, OnDestroy, DisableForReuseHook {
35 @Input() currentVideoTimestamp: number
36 @Input() lazyLoad = false
38 isNewPlaylistBlockOpened = false
39 videoPlaylistSearch: string
40 videoPlaylistSearchChanged = new Subject<string>()
41 videoPlaylists: PlaylistSummary[] = []
43 startTimestampEnabled: boolean
44 startTimestamp: number
45 stopTimestampEnabled: boolean
48 displayOptions = false
50 private disabled = false
52 private listenToPlaylistChangeSub: Subscription
53 private playlistsData: VideoPlaylist[] = []
56 protected formValidatorService: FormValidatorService,
57 private authService: AuthService,
58 private notifier: Notifier,
60 private videoPlaylistService: VideoPlaylistService,
61 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
62 private cd: ChangeDetectorRef
68 return this.authService.getUser()
73 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
76 this.videoPlaylistService.listenToMyAccountPlaylistsChange()
77 .subscribe(result => {
78 this.playlistsData = result.data
80 this.videoPlaylistService.runPlaylistCheck(this.video.id)
83 this.videoPlaylistSearchChanged
84 .pipe(debounceTime(500))
85 .subscribe(() => this.load())
87 if (this.lazyLoad === false) this.load()
90 ngOnChanges (simpleChanges: SimpleChanges) {
91 if (simpleChanges['video']) {
97 this.unsubscribePlaylistChanges()
105 this.disabled = false
109 logger('Reloading component')
111 this.videoPlaylists = []
112 this.videoPlaylistSearch = undefined
114 this.resetOptions(true)
117 this.cd.markForCheck()
121 logger('Loading component')
123 this.listenToPlaylistChanges()
125 this.videoPlaylistService.listMyPlaylistWithCache(this.user, this.videoPlaylistSearch)
126 .subscribe(playlistsResult => {
127 this.playlistsData = playlistsResult.data
129 this.videoPlaylistService.runPlaylistCheck(this.video.id)
133 openChange (opened: boolean) {
134 if (opened === false) {
135 this.isNewPlaylistBlockOpened = false
136 this.displayOptions = false
140 openCreateBlock (event: Event) {
141 event.preventDefault()
143 this.isNewPlaylistBlockOpened = true
146 togglePlaylist (event: Event, playlist: PlaylistSummary) {
147 event.preventDefault()
149 if (playlist.inPlaylist === true) {
150 this.removeVideoFromPlaylist(playlist)
152 this.addVideoInPlaylist(playlist)
155 playlist.inPlaylist = !playlist.inPlaylist
158 this.cd.markForCheck()
162 const displayName = this.form.value[ 'displayName' ]
164 const videoPlaylistCreate: VideoPlaylistCreate = {
166 privacy: VideoPlaylistPrivacy.PRIVATE
169 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
171 this.isNewPlaylistBlockOpened = false
173 this.cd.markForCheck()
176 err => this.notifier.error(err.message)
180 resetOptions (resetTimestamp = false) {
181 this.displayOptions = false
183 this.timestampOptions = {} as any
184 this.timestampOptions.startTimestampEnabled = false
185 this.timestampOptions.stopTimestampEnabled = false
187 if (resetTimestamp) {
188 this.timestampOptions.startTimestamp = 0
189 this.timestampOptions.stopTimestamp = this.video.duration
193 formatTimestamp (playlist: PlaylistSummary) {
194 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
195 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
197 return `(${start}-${stop})`
200 onVideoPlaylistSearchChanged () {
201 this.videoPlaylistSearchChanged.next()
204 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
205 if (!playlist.playlistElementId) return
207 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, playlist.playlistElementId, this.video.id)
210 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
214 this.notifier.error(err.message)
217 () => this.cd.markForCheck()
221 private listenToPlaylistChanges () {
222 this.unsubscribePlaylistChanges()
224 this.listenToPlaylistChangeSub = this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)
225 .pipe(filter(() => this.disabled === false))
226 .subscribe(existResult => this.rebuildPlaylists(existResult))
229 private unsubscribePlaylistChanges () {
230 if (this.listenToPlaylistChangeSub) {
231 this.listenToPlaylistChangeSub.unsubscribe()
232 this.listenToPlaylistChangeSub = undefined
236 private rebuildPlaylists (existResult: VideoExistInPlaylist[]) {
237 logger('Got existing results for %d.', this.video.id, existResult)
239 this.videoPlaylists = []
240 for (const playlist of this.playlistsData) {
241 const existingPlaylist = existResult.find(p => p.playlistId === playlist.id)
243 this.videoPlaylists.push({
245 displayName: playlist.displayName,
246 inPlaylist: !!existingPlaylist,
247 playlistElementId: existingPlaylist ? existingPlaylist.playlistElementId : undefined,
248 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
249 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
253 logger('Rebuilt playlist state for video %d.', this.video.id, this.videoPlaylists)
255 this.cd.markForCheck()
258 private addVideoInPlaylist (playlist: PlaylistSummary) {
259 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
261 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
262 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
264 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
267 const message = body.startTimestamp || body.stopTimestamp
268 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
269 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
271 this.notifier.success(message)
275 this.notifier.error(err.message)
278 () => this.cd.markForCheck()