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
40 private playlistElementId: number
43 protected formValidatorService: FormValidatorService,
44 private authService: AuthService,
45 private notifier: Notifier,
47 private videoPlaylistService: VideoPlaylistService,
48 private videoPlaylistValidatorsService: VideoPlaylistValidatorsService,
49 private cd: ChangeDetectorRef
55 return this.authService.getUser()
60 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
102 this.playlistElementId = existingPlaylist ? existingPlaylist.playlistElementId : undefined
105 this.cd.markForCheck()
110 openChange (opened: boolean) {
111 if (opened === false) {
112 this.isNewPlaylistBlockOpened = false
113 this.displayOptions = false
117 openCreateBlock (event: Event) {
118 event.preventDefault()
120 this.isNewPlaylistBlockOpened = true
123 togglePlaylist (event: Event, playlist: PlaylistSummary) {
124 event.preventDefault()
126 if (playlist.inPlaylist === true) {
127 this.removeVideoFromPlaylist(playlist)
129 this.addVideoInPlaylist(playlist)
132 playlist.inPlaylist = !playlist.inPlaylist
135 this.cd.markForCheck()
139 const displayName = this.form.value[ 'displayName' ]
141 const videoPlaylistCreate: VideoPlaylistCreate = {
143 privacy: VideoPlaylistPrivacy.PRIVATE
146 this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe(
148 this.videoPlaylists.push({
149 id: res.videoPlaylist.id,
154 this.isNewPlaylistBlockOpened = false
156 this.cd.markForCheck()
159 err => this.notifier.error(err.message)
163 resetOptions (resetTimestamp = false) {
164 this.displayOptions = false
166 this.timestampOptions = {} as any
167 this.timestampOptions.startTimestampEnabled = false
168 this.timestampOptions.stopTimestampEnabled = false
170 if (resetTimestamp) {
171 this.timestampOptions.startTimestamp = 0
172 this.timestampOptions.stopTimestamp = this.video.duration
176 formatTimestamp (playlist: PlaylistSummary) {
177 const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : ''
178 const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : ''
180 return `(${start}-${stop})`
183 private removeVideoFromPlaylist (playlist: PlaylistSummary) {
184 if (!this.playlistElementId) return
186 this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, this.playlistElementId)
189 this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName }))
191 playlist.inPlaylist = false
195 this.notifier.error(err.message)
197 playlist.inPlaylist = true
200 () => this.cd.markForCheck()
204 private addVideoInPlaylist (playlist: PlaylistSummary) {
205 const body: VideoPlaylistElementCreate = { videoId: this.video.id }
207 if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp
208 if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp
210 this.videoPlaylistService.addVideoInPlaylist(playlist.id, body)
213 playlist.inPlaylist = true
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()