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 playlistElementId?: number
16 startTimestamp?: number
17 stopTimestamp?: number
21 selector: 'my-video-add-to-playlist',
22 styleUrls: [ './video-add-to-playlist.component.scss' ],
23 templateUrl: './video-add-to-playlist.component.html',
24 changeDetection: ChangeDetectionStrategy.OnPush
26 export class VideoAddToPlaylistComponent extends FormReactive implements OnInit, OnChanges {
28 @Input() currentVideoTimestamp: number
29 @Input() lazyLoad = false
31 isNewPlaylistBlockOpened = false
32 videoPlaylists: PlaylistSummary[] = []
34 startTimestampEnabled: boolean
35 startTimestamp: number
36 stopTimestampEnabled: boolean
39 displayOptions = false
42 protected formValidatorService: FormValidatorService,
43 private authService: AuthService,
44 private notifier: Notifier,
46 private videoPlaylistService: VideoPlaylistService,
47 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
48 private cd: ChangeDetectorRef
54 return this.authService.getUser()
59 displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME
63 ngOnChanges (simpleChanges: SimpleChanges) {
64 if (simpleChanges['video']) {
70 this.resetOptions(true)
72 if (this.lazyLoad !== true) this.load()
76 this.videoPlaylists = []
80 this.cd.markForCheck()
85 this.videoPlaylistService.listAccountPlaylists(this.user.account, undefined,'-updatedAt'),
86 this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id)
89 ([ playlistsResult, existResult ]) => {
90 for (const playlist of playlistsResult.data) {
91 const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id)
93 this.videoPlaylists.push({
95 displayName: playlist.displayName,
96 inPlaylist: !!existingPlaylist,
97 playlistElementId: existingPlaylist ? existingPlaylist.playlistElementId : undefined,
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 if (!playlist.playlistElementId) return
184 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, playlist.playlistElementId)
187 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
189 playlist.inPlaylist = false
190 playlist.playlistElementId = undefined
194 this.notifier.error(err.message)
196 playlist.inPlaylist = true
199 () => this.cd.markForCheck()
203 private addVideoInPlaylist (playlist: PlaylistSummary) {
204 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
206 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
207 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
209 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
212 playlist.inPlaylist = true
213 playlist.playlistElementId = res.videoPlaylistElement.id
215 playlist.startTimestamp = body.startTimestamp
216 playlist.stopTimestamp = body.stopTimestamp
218 const message = body.startTimestamp || body.stopTimestamp
219 ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) })
220 : this.i18n('Video added in {{n}}', { n: playlist.displayName })
222 this.notifier.success(message)
226 this.notifier.error(err.message)
228 playlist.inPlaylist = false
231 () => this.cd.markForCheck()