]>
Commit | Line | Data |
---|---|---|
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, Subject } from 'rxjs' | |
5 | import { debounceTime } from 'rxjs/operators' | |
6 | import { Video, VideoPlaylistCreate, VideoPlaylistElementCreate, VideoPlaylistPrivacy } from '@shared/models' | |
7 | import { FormReactive, FormValidatorService, VideoPlaylistValidatorsService } from '@app/shared/forms' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { secondsToTime } from '../../../assets/player/utils' | |
10 | ||
11 | type PlaylistSummary = { | |
12 | id: number | |
13 | inPlaylist: boolean | |
14 | displayName: string | |
15 | ||
16 | playlistElementId?: number | |
17 | startTimestamp?: number | |
18 | stopTimestamp?: number | |
19 | } | |
20 | ||
21 | @Component({ | |
22 | selector: 'my-video-add-to-playlist', | |
23 | styleUrls: [ './video-add-to-playlist.component.scss' ], | |
24 | templateUrl: './video-add-to-playlist.component.html', | |
25 | changeDetection: ChangeDetectionStrategy.OnPush | |
26 | }) | |
27 | export class VideoAddToPlaylistComponent extends FormReactive implements OnInit, OnChanges { | |
28 | @Input() video: Video | |
29 | @Input() currentVideoTimestamp: number | |
30 | @Input() lazyLoad = false | |
31 | ||
32 | isNewPlaylistBlockOpened = false | |
33 | videoPlaylistSearch: string | |
34 | videoPlaylistSearchChanged = new Subject<string>() | |
35 | videoPlaylists: PlaylistSummary[] = [] | |
36 | timestampOptions: { | |
37 | startTimestampEnabled: boolean | |
38 | startTimestamp: number | |
39 | stopTimestampEnabled: boolean | |
40 | stopTimestamp: number | |
41 | } | |
42 | displayOptions = false | |
43 | ||
44 | constructor ( | |
45 | protected formValidatorService: FormValidatorService, | |
46 | private authService: AuthService, | |
47 | private notifier: Notifier, | |
48 | private i18n: I18n, | |
49 | private videoPlaylistService: VideoPlaylistService, | |
50 | private videoPlaylistValidatorsService: VideoPlaylistValidatorsService, | |
51 | private cd: ChangeDetectorRef | |
52 | ) { | |
53 | super() | |
54 | } | |
55 | ||
56 | get user () { | |
57 | return this.authService.getUser() | |
58 | } | |
59 | ||
60 | ngOnInit () { | |
61 | this.buildForm({ | |
62 | displayName: this.videoPlaylistValidatorsService.VIDEO_PLAYLIST_DISPLAY_NAME | |
63 | }) | |
64 | ||
65 | this.videoPlaylistSearchChanged | |
66 | .pipe( | |
67 | debounceTime(500)) | |
68 | .subscribe(() => { | |
69 | this.load() | |
70 | }) | |
71 | } | |
72 | ||
73 | ngOnChanges (simpleChanges: SimpleChanges) { | |
74 | if (simpleChanges['video']) { | |
75 | this.reload() | |
76 | } | |
77 | } | |
78 | ||
79 | init () { | |
80 | this.resetOptions(true) | |
81 | ||
82 | if (this.lazyLoad !== true) this.load() | |
83 | } | |
84 | ||
85 | reload () { | |
86 | this.videoPlaylists = [] | |
87 | this.videoPlaylistSearch = undefined | |
88 | ||
89 | this.init() | |
90 | ||
91 | this.cd.markForCheck() | |
92 | } | |
93 | ||
94 | load () { | |
95 | forkJoin([ | |
96 | this.videoPlaylistService.listAccountPlaylists(this.user.account, undefined, '-updatedAt', this.videoPlaylistSearch), | |
97 | this.videoPlaylistService.doesVideoExistInPlaylist(this.video.id) | |
98 | ]) | |
99 | .subscribe( | |
100 | ([ playlistsResult, existResult ]) => { | |
101 | this.videoPlaylists = [] | |
102 | for (const playlist of playlistsResult.data) { | |
103 | const existingPlaylist = existResult[ this.video.id ].find(p => p.playlistId === playlist.id) | |
104 | ||
105 | this.videoPlaylists.push({ | |
106 | id: playlist.id, | |
107 | displayName: playlist.displayName, | |
108 | inPlaylist: !!existingPlaylist, | |
109 | playlistElementId: existingPlaylist ? existingPlaylist.playlistElementId : undefined, | |
110 | startTimestamp: existingPlaylist ? existingPlaylist.startTimestamp : undefined, | |
111 | stopTimestamp: existingPlaylist ? existingPlaylist.stopTimestamp : undefined | |
112 | }) | |
113 | } | |
114 | ||
115 | this.cd.markForCheck() | |
116 | } | |
117 | ) | |
118 | } | |
119 | ||
120 | openChange (opened: boolean) { | |
121 | if (opened === false) { | |
122 | this.isNewPlaylistBlockOpened = false | |
123 | this.displayOptions = false | |
124 | } | |
125 | } | |
126 | ||
127 | openCreateBlock (event: Event) { | |
128 | event.preventDefault() | |
129 | ||
130 | this.isNewPlaylistBlockOpened = true | |
131 | } | |
132 | ||
133 | togglePlaylist (event: Event, playlist: PlaylistSummary) { | |
134 | event.preventDefault() | |
135 | ||
136 | if (playlist.inPlaylist === true) { | |
137 | this.removeVideoFromPlaylist(playlist) | |
138 | } else { | |
139 | this.addVideoInPlaylist(playlist) | |
140 | } | |
141 | ||
142 | playlist.inPlaylist = !playlist.inPlaylist | |
143 | this.resetOptions() | |
144 | ||
145 | this.cd.markForCheck() | |
146 | } | |
147 | ||
148 | createPlaylist () { | |
149 | const displayName = this.form.value[ 'displayName' ] | |
150 | ||
151 | const videoPlaylistCreate: VideoPlaylistCreate = { | |
152 | displayName, | |
153 | privacy: VideoPlaylistPrivacy.PRIVATE | |
154 | } | |
155 | ||
156 | this.videoPlaylistService.createVideoPlaylist(videoPlaylistCreate).subscribe( | |
157 | res => { | |
158 | this.videoPlaylists.push({ | |
159 | id: res.videoPlaylist.id, | |
160 | displayName, | |
161 | inPlaylist: false | |
162 | }) | |
163 | ||
164 | this.isNewPlaylistBlockOpened = false | |
165 | ||
166 | this.cd.markForCheck() | |
167 | }, | |
168 | ||
169 | err => this.notifier.error(err.message) | |
170 | ) | |
171 | } | |
172 | ||
173 | resetOptions (resetTimestamp = false) { | |
174 | this.displayOptions = false | |
175 | ||
176 | this.timestampOptions = {} as any | |
177 | this.timestampOptions.startTimestampEnabled = false | |
178 | this.timestampOptions.stopTimestampEnabled = false | |
179 | ||
180 | if (resetTimestamp) { | |
181 | this.timestampOptions.startTimestamp = 0 | |
182 | this.timestampOptions.stopTimestamp = this.video.duration | |
183 | } | |
184 | } | |
185 | ||
186 | formatTimestamp (playlist: PlaylistSummary) { | |
187 | const start = playlist.startTimestamp ? secondsToTime(playlist.startTimestamp) : '' | |
188 | const stop = playlist.stopTimestamp ? secondsToTime(playlist.stopTimestamp) : '' | |
189 | ||
190 | return `(${start}-${stop})` | |
191 | } | |
192 | ||
193 | onVideoPlaylistSearchChanged () { | |
194 | this.videoPlaylistSearchChanged.next() | |
195 | } | |
196 | ||
197 | private removeVideoFromPlaylist (playlist: PlaylistSummary) { | |
198 | if (!playlist.playlistElementId) return | |
199 | ||
200 | this.videoPlaylistService.removeVideoFromPlaylist(playlist.id, playlist.playlistElementId) | |
201 | .subscribe( | |
202 | () => { | |
203 | this.notifier.success(this.i18n('Video removed from {{name}}', { name: playlist.displayName })) | |
204 | ||
205 | playlist.inPlaylist = false | |
206 | playlist.playlistElementId = undefined | |
207 | }, | |
208 | ||
209 | err => { | |
210 | this.notifier.error(err.message) | |
211 | ||
212 | playlist.inPlaylist = true | |
213 | }, | |
214 | ||
215 | () => this.cd.markForCheck() | |
216 | ) | |
217 | } | |
218 | ||
219 | private addVideoInPlaylist (playlist: PlaylistSummary) { | |
220 | const body: VideoPlaylistElementCreate = { videoId: this.video.id } | |
221 | ||
222 | if (this.timestampOptions.startTimestampEnabled) body.startTimestamp = this.timestampOptions.startTimestamp | |
223 | if (this.timestampOptions.stopTimestampEnabled) body.stopTimestamp = this.timestampOptions.stopTimestamp | |
224 | ||
225 | this.videoPlaylistService.addVideoInPlaylist(playlist.id, body) | |
226 | .subscribe( | |
227 | res => { | |
228 | playlist.inPlaylist = true | |
229 | playlist.playlistElementId = res.videoPlaylistElement.id | |
230 | ||
231 | playlist.startTimestamp = body.startTimestamp | |
232 | playlist.stopTimestamp = body.stopTimestamp | |
233 | ||
234 | const message = body.startTimestamp || body.stopTimestamp | |
235 | ? this.i18n('Video added in {{n}} at timestamps {{t}}', { n: playlist.displayName, t: this.formatTimestamp(playlist) }) | |
236 | : this.i18n('Video added in {{n}}', { n: playlist.displayName }) | |
237 | ||
238 | this.notifier.success(message) | |
239 | }, | |
240 | ||
241 | err => { | |
242 | this.notifier.error(err.message) | |
243 | ||
244 | playlist.inPlaylist = false | |
245 | }, | |
246 | ||
247 | () => this.cd.markForCheck() | |
248 | ) | |
249 | } | |
250 | } |