1 import { ChangeDetectionStrategy, ChangeDetectorRef, 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',
23 changeDetection: ChangeDetectionStrategy.OnPush
25 export class VideoAddToPlaylistComponent extends FormReactive implements OnInit {
27 @Input() currentVideoTimestamp: number
28 @Input() lazyLoad = false
30 isNewPlaylistBlockOpened = false
31 videoPlaylists: PlaylistSummary[] = []
33 startTimestampEnabled: boolean
34 startTimestamp: number
35 stopTimestampEnabled: boolean
38 displayOptions = false
41 protected formValidatorService: FormValidatorService,
42 private authService: AuthService,
43 private notifier: Notifier,
45 private videoPlaylistService: VideoPlaylistService,
46 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
47 private cd: ChangeDetectorRef
53 return this.authService.getUser()
57 this.resetOptions(true)
60 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
63 if (this.lazyLoad !== true) this.load()
68 this.videoPlaylistService.listAccountPlaylists(this.user.account, '-updatedAt'),
69 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
72 ([ playlistsResult, existResult ]) => {
73 for (const playlist of playlistsResult.data) {
74 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
76 this.videoPlaylists.push({
78 displayName: playlist.displayName,
79 inPlaylist: !!existingPlaylist,
80 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
81 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
85 this.cd.markForCheck()
90 openChange (opened: boolean) {
91 if (opened === false) {
92 this.isNewPlaylistBlockOpened = false
93 this.displayOptions = false
97 openCreateBlock (event: Event) {
98 event.preventDefault()
100 this.isNewPlaylistBlockOpened = true
103 togglePlaylist (event: Event, playlist: PlaylistSummary) {
104 event.preventDefault()
106 if (playlist.inPlaylist === true) {
107 this.removeVideoFromPlaylist(playlist)
109 this.addVideoInPlaylist(playlist)
112 playlist.inPlaylist = !playlist.inPlaylist
115 this.cd.markForCheck()
119 const displayName = this.form.value[ 'displayName' ]
121 const videoPlaylistCreate: VideoPlaylistCreate = {
123 privacy: VideoPlaylistPrivacy.PRIVATE
126 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
128 this.videoPlaylists.push({
129 id: res.videoPlaylist.id,
134 this.isNewPlaylistBlockOpened = false
136 this.cd.markForCheck()
139 err => this.notifier.error(err.message)
143 resetOptions (resetTimestamp = false) {
144 this.displayOptions = false
146 this.timestampOptions = {} as any
147 this.timestampOptions.startTimestampEnabled = false
148 this.timestampOptions.stopTimestampEnabled = false
150 if (resetTimestamp) {
151 this.timestampOptions.startTimestamp = 0
152 this.timestampOptions.stopTimestamp = this.video.duration
156 formatTimestamp (playlist: PlaylistSummary) {
157 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
158 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
160 return `(${start}-${stop})`
163 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
164 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.video.id)
167 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
169 playlist.inPlaylist = false
173 this.notifier.error(err.message)
175 playlist.inPlaylist = true
178 () => this.cd.markForCheck()
182 private addVideoInPlaylist (playlist: PlaylistSummary) {
183 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
185 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
186 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
188 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
191 playlist.inPlaylist = true
193 playlist.startTimestamp = body.startTimestamp
194 playlist.stopTimestamp = body.stopTimestamp
196 const message = body.startTimestamp || body.stopTimestamp
197 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
198 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
200 this.notifier.success(message)
204 this.notifier.error(err.message)
206 playlist.inPlaylist = false
209 () => this.cd.markForCheck()