1 import { Component, Input, OnInit } from '@angular/core'
2 import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
3 import { AuthService, Notifier } from '@app/core'
4 import { forkJoin } from 'rxjs'
5 import { Video, VideoPlaylistCreate, VideoPlaylistElementCreate, VideoPlaylistPrivacy } from '@shared/models'
6 import { FormReactive, FormValidatorService, VideoPlaylistValidatorsService } from '@app/shared/forms'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { secondsToTime } from '../../../assets/player/utils'
10 type PlaylistSummary = {
15 startTimestamp?: number
16 stopTimestamp?: number
20 selector: 'my-video-add-to-playlist',
21 styleUrls: [ './video-add-to-playlist.component.scss' ],
22 templateUrl: './video-add-to-playlist.component.html'
24 export class VideoAddToPlaylistComponent extends FormReactive implements OnInit {
26 @Input() currentVideoTimestamp: number
27 @Input() lazyLoad = false
29 isNewPlaylistBlockOpened = false
30 videoPlaylists: PlaylistSummary[] = []
32 startTimestampEnabled: boolean
33 startTimestamp: number
34 stopTimestampEnabled: boolean
37 displayOptions = false
40 protected formValidatorService: FormValidatorService,
41 private authService: AuthService,
42 private notifier: Notifier,
44 private videoPlaylistService: VideoPlaylistService,
45 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService
51 return this.authService.getUser()
55 this.resetOptions(true)
58 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
61 if (this.lazyLoad !== true) this.load()
66 this.videoPlaylistService.listAccountPlaylists(this.user.account, '-updatedAt'),
67 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
70 ([ playlistsResult, existResult ]) => {
71 for (const playlist of playlistsResult.data) {
72 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
74 this.videoPlaylists.push({
76 displayName: playlist.displayName,
77 inPlaylist: !!existingPlaylist,
78 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
79 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
86 openChange (opened: boolean) {
87 if (opened === false) {
88 this.isNewPlaylistBlockOpened = false
89 this.displayOptions = false
93 openCreateBlock (event: Event) {
94 event.preventDefault()
96 this.isNewPlaylistBlockOpened = true
99 togglePlaylist (event: Event, playlist: PlaylistSummary) {
100 event.preventDefault()
102 if (playlist.inPlaylist === true) {
103 this.removeVideoFromPlaylist(playlist)
105 this.addVideoInPlaylist(playlist)
108 playlist.inPlaylist = !playlist.inPlaylist
113 const displayName = this.form.value[ 'displayName' ]
115 const videoPlaylistCreate: VideoPlaylistCreate = {
117 privacy: VideoPlaylistPrivacy.PRIVATE
120 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
122 this.videoPlaylists.push({
123 id: res.videoPlaylist.id,
128 this.isNewPlaylistBlockOpened = false
131 err => this.notifier.error(err.message)
135 resetOptions (resetTimestamp = false) {
136 this.displayOptions = false
138 this.timestampOptions = {} as any
139 this.timestampOptions.startTimestampEnabled = false
140 this.timestampOptions.stopTimestampEnabled = false
142 if (resetTimestamp) {
143 this.timestampOptions.startTimestamp = 0
144 this.timestampOptions.stopTimestamp = this.video.duration
148 formatTimestamp (playlist: PlaylistSummary) {
149 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
150 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
152 return `(${start}-${stop})`
155 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
156 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.video.id)
159 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
161 playlist.inPlaylist = false
165 this.notifier.error(err.message)
167 playlist.inPlaylist = true
172 private addVideoInPlaylist (playlist: PlaylistSummary) {
173 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
175 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
176 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
178 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
181 playlist.inPlaylist = true
183 playlist.startTimestamp = body.startTimestamp
184 playlist.stopTimestamp = body.stopTimestamp
186 const message = body.startTimestamp || body.stopTimestamp
187 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
188 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
190 this.notifier.success(message)
194 this.notifier.error(err.message)
196 playlist.inPlaylist = false