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
62 ngOnChanges (simpleChanges: SimpleChanges) {
63 if (simpleChanges['video']) {
69 this.resetOptions(true)
71 if (this.lazyLoad !== true) this.load()
75 this.videoPlaylists = []
79 this.cd.markForCheck()
84 this.videoPlaylistService.listAccountPlaylists(this.user.account, '-updatedAt'),
85 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
88 ([ playlistsResult, existResult ]) => {
89 for (const playlist of playlistsResult.data) {
90 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
92 this.videoPlaylists.push({
94 displayName: playlist.displayName,
95 inPlaylist: !!existingPlaylist,
96 startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined,
97 stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined
101 this.cd.markForCheck()
106 openChange (opened: boolean) {
107 if (opened === false) {
108 this.isNewPlaylistBlockOpened = false
109 this.displayOptions = false
113 openCreateBlock (event: Event) {
114 event.preventDefault()
116 this.isNewPlaylistBlockOpened = true
119 togglePlaylist (event: Event, playlist: PlaylistSummary) {
120 event.preventDefault()
122 if (playlist.inPlaylist === true) {
123 this.removeVideoFromPlaylist(playlist)
125 this.addVideoInPlaylist(playlist)
128 playlist.inPlaylist = !playlist.inPlaylist
131 this.cd.markForCheck()
135 const displayName = this.form.value[ 'displayName' ]
137 const videoPlaylistCreate: VideoPlaylistCreate = {
139 privacy: VideoPlaylistPrivacy.PRIVATE
142 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
144 this.videoPlaylists.push({
145 id: res.videoPlaylist.id,
150 this.isNewPlaylistBlockOpened = false
152 this.cd.markForCheck()
155 err => this.notifier.error(err.message)
159 resetOptions (resetTimestamp = false) {
160 this.displayOptions = false
162 this.timestampOptions = {} as any
163 this.timestampOptions.startTimestampEnabled = false
164 this.timestampOptions.stopTimestampEnabled = false
166 if (resetTimestamp) {
167 this.timestampOptions.startTimestamp = 0
168 this.timestampOptions.stopTimestamp = this.video.duration
172 formatTimestamp (playlist: PlaylistSummary) {
173 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
174 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
176 return `(${start}-${stop})`
179 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
180 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.video.id)
183 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
185 playlist.inPlaylist = false
189 this.notifier.error(err.message)
191 playlist.inPlaylist = true
194 () => this.cd.markForCheck()
198 private addVideoInPlaylist (playlist: PlaylistSummary) {
199 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
201 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
202 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
204 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
207 playlist.inPlaylist = true
209 playlist.startTimestamp = body.startTimestamp
210 playlist.stopTimestamp = body.stopTimestamp
212 const message = body.startTimestamp || body.stopTimestamp
213 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
214 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
216 this.notifier.success(message)
220 this.notifier.error(err.message)
222 playlist.inPlaylist = false
225 () => this.cd.markForCheck()