]>
Commit | Line | Data |
---|---|---|
9abd170d | 1 | import { forkJoin } from 'rxjs' |
67ed6552 | 2 | import { map } from 'rxjs/operators' |
7294aab0 | 3 | import { Component, EventEmitter, Input, NgZone, OnDestroy, OnInit, Output, ViewChild } from '@angular/core' |
40e87e9e | 4 | import { FormArray, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms' |
7294aab0 | 5 | import { HooksService, PluginService, ServerService } from '@app/core' |
67ed6552 | 6 | import { removeElementFromArray } from '@app/helpers' |
7ed1edbb C |
7 | import { |
8 | VIDEO_CATEGORY_VALIDATOR, | |
9 | VIDEO_CHANNEL_VALIDATOR, | |
10 | VIDEO_DESCRIPTION_VALIDATOR, | |
11 | VIDEO_LANGUAGE_VALIDATOR, | |
12 | VIDEO_LICENCE_VALIDATOR, | |
13 | VIDEO_NAME_VALIDATOR, | |
14 | VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR, | |
15 | VIDEO_PRIVACY_VALIDATOR, | |
16 | VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR, | |
17 | VIDEO_SUPPORT_VALIDATOR, | |
18 | VIDEO_TAGS_ARRAY_VALIDATOR | |
19 | } from '@app/shared/form-validators/video-validators' | |
20 | import { FormReactiveValidationMessages, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms' | |
9abd170d | 21 | import { InstanceService } from '@app/shared/shared-instance' |
67ed6552 | 22 | import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main' |
a5cf76af | 23 | import { ServerConfig, VideoConstant, LiveVideo, VideoPrivacy } from '@shared/models' |
7294aab0 | 24 | import { RegisterClientFormFieldOptions, RegisterClientVideoFieldOptions } from '@shared/models/plugins/register-client-form-field.model' |
67ed6552 C |
25 | import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service' |
26 | import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component' | |
c6c0fa6c | 27 | import { VideoEditType } from './video-edit.type' |
02c01341 RK |
28 | |
29 | type VideoLanguages = VideoConstant<string> & { group?: string } | |
ff249f49 C |
30 | |
31 | @Component({ | |
32 | selector: 'my-video-edit', | |
33 | styleUrls: [ './video-edit.component.scss' ], | |
34 | templateUrl: './video-edit.component.html' | |
35 | }) | |
40e87e9e | 36 | export class VideoEditComponent implements OnInit, OnDestroy { |
ff249f49 C |
37 | @Input() form: FormGroup |
38 | @Input() formErrors: { [ id: string ]: string } = {} | |
e309822b | 39 | @Input() validationMessages: FormReactiveValidationMessages = {} |
9abd170d | 40 | @Input() userVideoChannels: SelectChannelItem[] = [] |
bbe0f064 | 41 | @Input() schedulePublicationPossible = true |
6913f691 | 42 | @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = [] |
14e2014a | 43 | @Input() waitTranscodingEnabled = true |
c6c0fa6c | 44 | @Input() type: VideoEditType |
a5cf76af | 45 | @Input() liveVideo: LiveVideo |
40e87e9e | 46 | |
f36da21e | 47 | @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent |
bbe0f064 | 48 | |
7294aab0 C |
49 | @Output() pluginFieldsAdded = new EventEmitter<void>() |
50 | ||
bbe0f064 C |
51 | // So that it can be accessed in the template |
52 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
ff249f49 | 53 | |
851f5daa | 54 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] |
8cd7faaa C |
55 | videoCategories: VideoConstant<number>[] = [] |
56 | videoLicences: VideoConstant<number>[] = [] | |
02c01341 | 57 | videoLanguages: VideoLanguages[] = [] |
ff249f49 | 58 | |
e309822b C |
59 | tagValidators: ValidatorFn[] |
60 | tagValidatorsMessages: { [ name: string ]: string } | |
ff249f49 | 61 | |
7294aab0 C |
62 | pluginDataFormGroup: FormGroup |
63 | ||
bbe0f064 C |
64 | schedulePublicationEnabled = false |
65 | ||
bbe0f064 C |
66 | calendarLocale: any = {} |
67 | minScheduledDate = new Date() | |
1e74f19a | 68 | myYearRange = '1880:' + (new Date()).getFullYear() |
bbe0f064 C |
69 | |
70 | calendarTimezone: string | |
71 | calendarDateFormat: string | |
ff249f49 | 72 | |
ba430d75 C |
73 | serverConfig: ServerConfig |
74 | ||
7294aab0 C |
75 | pluginFields: { |
76 | commonOptions: RegisterClientFormFieldOptions | |
77 | videoFormOptions: RegisterClientVideoFieldOptions | |
78 | }[] = [] | |
79 | ||
244b4ae3 | 80 | private schedulerInterval: any |
0f7fedc3 | 81 | private firstPatchDone = false |
772d5642 | 82 | private initialVideoCaptions: string[] = [] |
40e87e9e | 83 | |
ff249f49 | 84 | constructor ( |
d18d6478 | 85 | private formValidatorService: FormValidatorService, |
851f5daa | 86 | private videoService: VideoService, |
bbe0f064 | 87 | private serverService: ServerService, |
7294aab0 | 88 | private pluginService: PluginService, |
02c01341 | 89 | private instanceService: InstanceService, |
84c7cde6 | 90 | private i18nPrimengCalendarService: I18nPrimengCalendarService, |
7294aab0 C |
91 | private ngZone: NgZone, |
92 | private hooks: HooksService | |
e309822b | 93 | ) { |
bbe0f064 C |
94 | this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale() |
95 | this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone() | |
96 | this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat() | |
e309822b | 97 | } |
ff249f49 C |
98 | |
99 | updateForm () { | |
244b4ae3 | 100 | const defaultValues: any = { |
d18d6478 C |
101 | nsfw: 'false', |
102 | commentsEnabled: 'true', | |
7f2cfe3a | 103 | downloadEnabled: 'true', |
2186386c | 104 | waitTranscoding: 'true', |
d18d6478 C |
105 | tags: [] |
106 | } | |
244b4ae3 | 107 | const obj: any = { |
7ed1edbb C |
108 | name: VIDEO_NAME_VALIDATOR, |
109 | privacy: VIDEO_PRIVACY_VALIDATOR, | |
110 | channelId: VIDEO_CHANNEL_VALIDATOR, | |
d18d6478 C |
111 | nsfw: null, |
112 | commentsEnabled: null, | |
7f2cfe3a | 113 | downloadEnabled: null, |
2186386c | 114 | waitTranscoding: null, |
7ed1edbb C |
115 | category: VIDEO_CATEGORY_VALIDATOR, |
116 | licence: VIDEO_LICENCE_VALIDATOR, | |
117 | language: VIDEO_LANGUAGE_VALIDATOR, | |
118 | description: VIDEO_DESCRIPTION_VALIDATOR, | |
119 | tags: VIDEO_TAGS_ARRAY_VALIDATOR, | |
d18d6478 | 120 | previewfile: null, |
7ed1edbb C |
121 | support: VIDEO_SUPPORT_VALIDATOR, |
122 | schedulePublicationAt: VIDEO_SCHEDULE_PUBLICATION_AT_VALIDATOR, | |
c6c0fa6c | 123 | originallyPublishedAt: VIDEO_ORIGINALLY_PUBLISHED_AT_VALIDATOR, |
b5b68755 | 124 | liveStreamKey: null, |
bb4ba6d9 | 125 | permanentLive: null, |
b5b68755 | 126 | saveReplay: null |
d18d6478 | 127 | } |
ff249f49 | 128 | |
d18d6478 C |
129 | this.formValidatorService.updateForm( |
130 | this.form, | |
131 | this.formErrors, | |
132 | this.validationMessages, | |
133 | obj, | |
134 | defaultValues | |
135 | ) | |
74af5145 | 136 | |
40e87e9e C |
137 | this.form.addControl('captions', new FormArray([ |
138 | new FormGroup({ | |
139 | language: new FormControl(), | |
140 | captionfile: new FormControl() | |
141 | }) | |
142 | ])) | |
143 | ||
bbe0f064 C |
144 | this.trackChannelChange() |
145 | this.trackPrivacyChange() | |
bb4ba6d9 | 146 | this.trackLivePermanentFieldChange() |
bbe0f064 C |
147 | } |
148 | ||
149 | ngOnInit () { | |
150 | this.updateForm() | |
151 | ||
7294aab0 C |
152 | this.pluginService.ensurePluginsAreLoaded('video-edit') |
153 | .then(() => this.updatePluginFields()) | |
154 | ||
ba430d75 C |
155 | this.serverService.getVideoCategories() |
156 | .subscribe(res => this.videoCategories = res) | |
7294aab0 | 157 | |
ba430d75 C |
158 | this.serverService.getVideoLicences() |
159 | .subscribe(res => this.videoLicences = res) | |
7294aab0 | 160 | |
02c01341 RK |
161 | forkJoin([ |
162 | this.instanceService.getAbout(), | |
163 | this.serverService.getVideoLanguages() | |
164 | ]).pipe(map(([ about, languages ]) => ({ about, languages }))) | |
165 | .subscribe(res => { | |
166 | this.videoLanguages = res.languages | |
7294aab0 C |
167 | .map(l => { |
168 | return res.about.instance.languages.includes(l.id) | |
169 | ? { ...l, group: $localize`Instance languages`, groupOrder: 0 } | |
170 | : { ...l, group: $localize`All languages`, groupOrder: 1 } | |
171 | }) | |
02c01341 RK |
172 | .sort((a, b) => a.groupOrder - b.groupOrder) |
173 | }) | |
ba430d75 C |
174 | |
175 | this.serverService.getVideoPrivacies() | |
02c01341 RK |
176 | .subscribe(privacies => { |
177 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies) | |
178 | if (this.schedulePublicationPossible) { | |
179 | this.videoPrivacies.push({ | |
180 | id: this.SPECIAL_SCHEDULED_PRIVACY, | |
66357162 C |
181 | label: $localize`Scheduled`, |
182 | description: $localize`Hide the video until a specific date` | |
02c01341 RK |
183 | }) |
184 | } | |
185 | }) | |
bbe0f064 | 186 | |
ba430d75 C |
187 | this.serverConfig = this.serverService.getTmpConfig() |
188 | this.serverService.getConfig() | |
189 | .subscribe(config => this.serverConfig = config) | |
851f5daa | 190 | |
772d5642 | 191 | this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id) |
84c7cde6 C |
192 | |
193 | this.ngZone.runOutsideAngular(() => { | |
194 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute | |
195 | }) | |
7294aab0 C |
196 | |
197 | this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type }) | |
40e87e9e C |
198 | } |
199 | ||
200 | ngOnDestroy () { | |
201 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
202 | } | |
203 | ||
ddb62a85 C |
204 | getExistingCaptions () { |
205 | return this.videoCaptions | |
206 | .filter(c => c.action !== 'REMOVE') | |
207 | .map(c => c.language.id) | |
208 | } | |
209 | ||
40e87e9e | 210 | onCaptionAdded (caption: VideoCaptionEdit) { |
f4001cf4 C |
211 | const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id) |
212 | ||
213 | // Replace existing caption? | |
214 | if (existingCaption) { | |
215 | Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' }) | |
ad774752 C |
216 | } else { |
217 | this.videoCaptions.push( | |
218 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
219 | ) | |
f4001cf4 C |
220 | } |
221 | ||
ad774752 | 222 | this.sortVideoCaptions() |
40e87e9e C |
223 | } |
224 | ||
772d5642 C |
225 | async deleteCaption (caption: VideoCaptionEdit) { |
226 | // Caption recovers his former state | |
227 | if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) { | |
228 | caption.action = undefined | |
229 | return | |
230 | } | |
231 | ||
40e87e9e C |
232 | // This caption is not on the server, just remove it from our array |
233 | if (caption.action === 'CREATE') { | |
234 | removeElementFromArray(this.videoCaptions, caption) | |
235 | return | |
236 | } | |
237 | ||
238 | caption.action = 'REMOVE' as 'REMOVE' | |
239 | } | |
240 | ||
241 | openAddCaptionModal () { | |
242 | this.videoCaptionAddModal.show() | |
bbe0f064 C |
243 | } |
244 | ||
b5b68755 C |
245 | isSaveReplayEnabled () { |
246 | return this.serverConfig.live.allowReplay | |
247 | } | |
248 | ||
bb4ba6d9 C |
249 | isPermanentLiveEnabled () { |
250 | return this.form.value['permanentLive'] === true | |
251 | } | |
252 | ||
ad774752 C |
253 | private sortVideoCaptions () { |
254 | this.videoCaptions.sort((v1, v2) => { | |
255 | if (v1.language.label < v2.language.label) return -1 | |
256 | if (v1.language.label === v2.language.label) return 0 | |
257 | ||
258 | return 1 | |
259 | }) | |
260 | } | |
261 | ||
7294aab0 C |
262 | private updatePluginFields () { |
263 | this.pluginFields = this.pluginService.getRegisteredVideoFormFields(this.type) | |
264 | ||
265 | if (this.pluginFields.length === 0) return | |
266 | ||
267 | const obj: any = {} | |
268 | ||
269 | for (const setting of this.pluginFields) { | |
270 | obj[setting.commonOptions.name] = new FormControl(setting.commonOptions.default) | |
271 | } | |
272 | ||
273 | this.pluginDataFormGroup = new FormGroup(obj) | |
274 | this.form.addControl('pluginData', this.pluginDataFormGroup) | |
275 | ||
276 | this.pluginFieldsAdded.emit() | |
277 | } | |
278 | ||
bbe0f064 | 279 | private trackPrivacyChange () { |
4adf2673 | 280 | // We will update the schedule input and the wait transcoding checkbox validators |
bbe0f064 C |
281 | this.form.controls[ 'privacy' ] |
282 | .valueChanges | |
283 | .pipe(map(res => parseInt(res.toString(), 10))) | |
284 | .subscribe( | |
285 | newPrivacyId => { | |
0f7fedc3 | 286 | |
bbe0f064 C |
287 | this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY |
288 | ||
289 | // Value changed | |
290 | const scheduleControl = this.form.get('schedulePublicationAt') | |
291 | const waitTranscodingControl = this.form.get('waitTranscoding') | |
292 | ||
293 | if (this.schedulePublicationEnabled) { | |
294 | scheduleControl.setValidators([ Validators.required ]) | |
295 | ||
296 | waitTranscodingControl.disable() | |
297 | waitTranscodingControl.setValue(false) | |
298 | } else { | |
299 | scheduleControl.clearValidators() | |
300 | ||
301 | waitTranscodingControl.enable() | |
0f7fedc3 C |
302 | |
303 | // Do not update the control value on first patch (values come from the server) | |
304 | if (this.firstPatchDone === true) { | |
305 | waitTranscodingControl.setValue(true) | |
306 | } | |
bbe0f064 C |
307 | } |
308 | ||
309 | scheduleControl.updateValueAndValidity() | |
310 | waitTranscodingControl.updateValueAndValidity() | |
0f7fedc3 C |
311 | |
312 | this.firstPatchDone = true | |
313 | ||
bbe0f064 C |
314 | } |
315 | ) | |
316 | } | |
317 | ||
318 | private trackChannelChange () { | |
74af5145 | 319 | // We will update the "support" field depending on the channel |
2186386c | 320 | this.form.controls[ 'channelId' ] |
74af5145 C |
321 | .valueChanges |
322 | .pipe(map(res => parseInt(res.toString(), 10))) | |
323 | .subscribe( | |
324 | newChannelId => { | |
2186386c | 325 | const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10) |
74af5145 C |
326 | |
327 | // Not initialized yet | |
328 | if (isNaN(newChannelId)) return | |
329 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
d18d6478 | 330 | if (!newChannel) return |
74af5145 | 331 | |
6f79be11 C |
332 | // Wait support field update |
333 | setTimeout(() => { | |
334 | const currentSupport = this.form.value[ 'support' ] | |
74af5145 | 335 | |
6f79be11 | 336 | // First time we set the channel? |
c6c0fa6c C |
337 | if (isNaN(oldChannelId)) { |
338 | // Fill support if it's empty | |
339 | if (!currentSupport) this.updateSupportField(newChannel.support) | |
340 | ||
341 | return | |
342 | } | |
6f79be11 C |
343 | |
344 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
345 | if (!newChannel || !oldChannel) { | |
346 | console.error('Cannot find new or old channel.') | |
347 | return | |
348 | } | |
74af5145 | 349 | |
6f79be11 C |
350 | // If the current support text is not the same than the old channel, the user updated it. |
351 | // We don't want the user to lose his text, so stop here | |
352 | if (currentSupport && currentSupport !== oldChannel.support) return | |
74af5145 | 353 | |
6f79be11 C |
354 | // Update the support text with our new channel |
355 | this.updateSupportField(newChannel.support) | |
356 | }) | |
74af5145 C |
357 | } |
358 | ) | |
ff249f49 C |
359 | } |
360 | ||
bb4ba6d9 C |
361 | private trackLivePermanentFieldChange () { |
362 | // We will update the "support" field depending on the channel | |
363 | this.form.controls['permanentLive'] | |
364 | .valueChanges | |
365 | .subscribe( | |
366 | permanentLive => { | |
367 | const saveReplayControl = this.form.controls['saveReplay'] | |
368 | ||
369 | if (permanentLive === true) { | |
370 | saveReplayControl.setValue(false) | |
371 | saveReplayControl.disable() | |
372 | } else { | |
373 | saveReplayControl.enable() | |
374 | } | |
375 | } | |
376 | ) | |
377 | } | |
378 | ||
74af5145 C |
379 | private updateSupportField (support: string) { |
380 | return this.form.patchValue({ support: support || '' }) | |
381 | } | |
ff249f49 | 382 | } |