1 import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnChanges, 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 { 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, OnChanges {
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()
58 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
64 ngOnChanges (simpleChanges: SimpleChanges) {
65 if (simpleChanges['video']) {
71 this.resetOptions(true)
73 if (this.lazyLoad !== true) this.load()
77 this.videoPlaylists = []
81 this.cd.markForCheck()
86 this.videoPlaylistService.listAccountPlaylists(this.user.account, '-updatedAt'),
87 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
90 ([ playlistsResult, existResult ]) => {
91 for (const playlist of playlistsResult.data) {
92 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
94 this.videoPlaylists.push({
96 displayName: playlist.displayName,
97 inPlaylist: !!existingPlaylist,
98 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
99 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
103 this.cd.markForCheck()
108 openChange (opened: boolean) {
109 if (opened === false) {
110 this.isNewPlaylistBlockOpened = false
111 this.displayOptions = false
115 openCreateBlock (event: Event) {
116 event.preventDefault()
118 this.isNewPlaylistBlockOpened = true
121 togglePlaylist (event: Event, playlist: PlaylistSummary) {
122 event.preventDefault()
124 if (playlist.inPlaylist === true) {
125 this.removeVideoFromPlaylist(playlist)
127 this.addVideoInPlaylist(playlist)
130 playlist.inPlaylist = !playlist.inPlaylist
133 this.cd.markForCheck()
137 const displayName = this.form.value[ 'displayName' ]
139 const videoPlaylistCreate: VideoPlaylistCreate = {
141 privacy: VideoPlaylistPrivacy.PRIVATE
144 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
146 this.videoPlaylists.push({
147 id: res.videoPlaylist.id,
152 this.isNewPlaylistBlockOpened = false
154 this.cd.markForCheck()
157 err => this.notifier.error(err.message)
161 resetOptions (resetTimestamp = false) {
162 this.displayOptions = false
164 this.timestampOptions = {} as any
165 this.timestampOptions.startTimestampEnabled = false
166 this.timestampOptions.stopTimestampEnabled = false
168 if (resetTimestamp) {
169 this.timestampOptions.startTimestamp = 0
170 this.timestampOptions.stopTimestamp = this.video.duration
174 formatTimestamp (playlist: PlaylistSummary) {
175 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
176 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
178 return `(${start}-${stop})`
181 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
182 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.video.id)
185 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
187 playlist.inPlaylist = false
191 this.notifier.error(err.message)
193 playlist.inPlaylist = true
196 () => this.cd.markForCheck()
200 private addVideoInPlaylist (playlist: PlaylistSummary) {
201 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
203 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
204 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
206 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
209 playlist.inPlaylist = true
211 playlist.startTimestamp = body.startTimestamp
212 playlist.stopTimestamp = body.stopTimestamp
214 const message = body.startTimestamp || body.stopTimestamp
215 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
216 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
218 this.notifier.success(message)
222 this.notifier.error(err.message)
224 playlist.inPlaylist = false
227 () => this.cd.markForCheck()