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, timeToInt } 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
28 isNewPlaylistBlockOpened = false
29 videoPlaylists: PlaylistSummary[] = []
31 startTimestampEnabled: boolean
32 startTimestamp: number
33 stopTimestampEnabled: boolean
36 displayOptions = false
39 protected formValidatorService: FormValidatorService,
40 private authService: AuthService,
41 private notifier: Notifier,
43 private videoPlaylistService: VideoPlaylistService,
44 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService
50 return this.authService.getUser()
54 this.resetOptions(true)
57 'display-name': this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
61 this.videoPlaylistService.listAccountPlaylists(this.user.account, '-updatedAt'),
62 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
65 ([ playlistsResult, existResult ]) => {
66 for (const playlist of playlistsResult.data) {
67 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
69 this.videoPlaylists.push({
71 displayName: playlist.displayName,
72 inPlaylist: !!existingPlaylist,
73 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
74 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
81 openChange (opened: boolean) {
82 if (opened === false) {
83 this.isNewPlaylistBlockOpened = false
84 this.displayOptions = false
88 openCreateBlock (event: Event) {
89 event.preventDefault()
91 this.isNewPlaylistBlockOpened = true
94 togglePlaylist (event: Event, playlist: PlaylistSummary) {
95 event.preventDefault()
97 if (playlist.inPlaylist === true) {
98 this.removeVideoFromPlaylist(playlist)
100 this.addVideoInPlaylist(playlist)
103 playlist.inPlaylist = !playlist.inPlaylist
108 const displayName = this.form.value[ 'display-name' ]
110 const videoPlaylistCreate: VideoPlaylistCreate = {
112 privacy: VideoPlaylistPrivacy.PRIVATE
115 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
117 this.videoPlaylists.push({
118 id: res.videoPlaylist.id,
123 this.isNewPlaylistBlockOpened = false
126 err => this.notifier.error(err.message)
130 resetOptions (resetTimestamp = false) {
131 this.displayOptions = false
133 this.timestampOptions = {} as any
134 this.timestampOptions.startTimestampEnabled = false
135 this.timestampOptions.stopTimestampEnabled = false
137 if (resetTimestamp) {
138 this.timestampOptions.startTimestamp = 0
139 this.timestampOptions.stopTimestamp = this.video.duration
143 formatTimestamp (playlist: PlaylistSummary) {
144 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
145 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
147 return `(${start}-${stop})`
150 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
151 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.video.id)
154 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
156 playlist.inPlaylist = false
160 this.notifier.error(err.message)
162 playlist.inPlaylist = true
167 private addVideoInPlaylist (playlist: PlaylistSummary) {
168 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
170 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
171 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
173 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
176 playlist.inPlaylist = true
178 playlist.startTimestamp = body.startTimestamp
179 playlist.stopTimestamp = body.stopTimestamp
181 const message = body.startTimestamp || body.stopTimestamp
182 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
183 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
185 this.notifier.success(message)
189 this.notifier.error(err.message)
191 playlist.inPlaylist = false