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