]>
Commit | Line | Data |
---|---|---|
9abd170d | 1 | import { forkJoin } from 'rxjs' |
67ed6552 | 2 | import { map } from 'rxjs/operators' |
84c7cde6 | 3 | import { Component, Input, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
40e87e9e | 4 | import { FormArray, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms' |
67ed6552 C |
5 | import { ServerService } from '@app/core' |
6 | import { removeElementFromArray } from '@app/helpers' | |
52c4976f | 7 | import { FormReactiveValidationMessages, FormValidatorService, SelectChannelItem, VideoValidatorsService } from '@app/shared/shared-forms' |
9abd170d | 8 | import { InstanceService } from '@app/shared/shared-instance' |
67ed6552 | 9 | import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main' |
9abd170d | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 C |
11 | import { ServerConfig, VideoConstant, VideoPrivacy } from '@shared/models' |
12 | import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service' | |
13 | import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component' | |
02c01341 RK |
14 | |
15 | type VideoLanguages = VideoConstant<string> & { group?: string } | |
ff249f49 C |
16 | |
17 | @Component({ | |
18 | selector: 'my-video-edit', | |
19 | styleUrls: [ './video-edit.component.scss' ], | |
20 | templateUrl: './video-edit.component.html' | |
21 | }) | |
40e87e9e | 22 | export class VideoEditComponent implements OnInit, OnDestroy { |
ff249f49 C |
23 | @Input() form: FormGroup |
24 | @Input() formErrors: { [ id: string ]: string } = {} | |
e309822b | 25 | @Input() validationMessages: FormReactiveValidationMessages = {} |
9abd170d | 26 | @Input() userVideoChannels: SelectChannelItem[] = [] |
bbe0f064 | 27 | @Input() schedulePublicationPossible = true |
6913f691 | 28 | @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = [] |
14e2014a | 29 | @Input() waitTranscodingEnabled = true |
40e87e9e | 30 | |
f36da21e | 31 | @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent |
bbe0f064 C |
32 | |
33 | // So that it can be accessed in the template | |
34 | readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY | |
ff249f49 | 35 | |
851f5daa | 36 | videoPrivacies: VideoConstant<VideoPrivacy>[] = [] |
8cd7faaa C |
37 | videoCategories: VideoConstant<number>[] = [] |
38 | videoLicences: VideoConstant<number>[] = [] | |
02c01341 | 39 | videoLanguages: VideoLanguages[] = [] |
ff249f49 | 40 | |
e309822b C |
41 | tagValidators: ValidatorFn[] |
42 | tagValidatorsMessages: { [ name: string ]: string } | |
ff249f49 | 43 | |
bbe0f064 C |
44 | schedulePublicationEnabled = false |
45 | ||
bbe0f064 C |
46 | calendarLocale: any = {} |
47 | minScheduledDate = new Date() | |
1e74f19a | 48 | myYearRange = '1880:' + (new Date()).getFullYear() |
bbe0f064 C |
49 | |
50 | calendarTimezone: string | |
51 | calendarDateFormat: string | |
ff249f49 | 52 | |
ba430d75 C |
53 | serverConfig: ServerConfig |
54 | ||
244b4ae3 | 55 | private schedulerInterval: any |
0f7fedc3 | 56 | private firstPatchDone = false |
772d5642 | 57 | private initialVideoCaptions: string[] = [] |
40e87e9e | 58 | |
ff249f49 | 59 | constructor ( |
d18d6478 | 60 | private formValidatorService: FormValidatorService, |
e309822b | 61 | private videoValidatorsService: VideoValidatorsService, |
851f5daa | 62 | private videoService: VideoService, |
bbe0f064 | 63 | private serverService: ServerService, |
02c01341 | 64 | private instanceService: InstanceService, |
84c7cde6 | 65 | private i18nPrimengCalendarService: I18nPrimengCalendarService, |
02c01341 | 66 | private i18n: I18n, |
84c7cde6 | 67 | private ngZone: NgZone |
e309822b | 68 | ) { |
bbe0f064 C |
69 | this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale() |
70 | this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone() | |
71 | this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat() | |
e309822b | 72 | } |
ff249f49 | 73 | |
f4001cf4 C |
74 | get existingCaptions () { |
75 | return this.videoCaptions | |
76 | .filter(c => c.action !== 'REMOVE') | |
77 | .map(c => c.language.id) | |
78 | } | |
79 | ||
ff249f49 | 80 | updateForm () { |
244b4ae3 | 81 | const defaultValues: any = { |
d18d6478 C |
82 | nsfw: 'false', |
83 | commentsEnabled: 'true', | |
7f2cfe3a | 84 | downloadEnabled: 'true', |
2186386c | 85 | waitTranscoding: 'true', |
d18d6478 C |
86 | tags: [] |
87 | } | |
244b4ae3 | 88 | const obj: any = { |
e309822b C |
89 | name: this.videoValidatorsService.VIDEO_NAME, |
90 | privacy: this.videoValidatorsService.VIDEO_PRIVACY, | |
91 | channelId: this.videoValidatorsService.VIDEO_CHANNEL, | |
d18d6478 C |
92 | nsfw: null, |
93 | commentsEnabled: null, | |
7f2cfe3a | 94 | downloadEnabled: null, |
2186386c | 95 | waitTranscoding: null, |
e309822b C |
96 | category: this.videoValidatorsService.VIDEO_CATEGORY, |
97 | licence: this.videoValidatorsService.VIDEO_LICENCE, | |
98 | language: this.videoValidatorsService.VIDEO_LANGUAGE, | |
99 | description: this.videoValidatorsService.VIDEO_DESCRIPTION, | |
02c01341 | 100 | tags: this.videoValidatorsService.VIDEO_TAGS_ARRAY, |
d18d6478 | 101 | previewfile: null, |
bbe0f064 | 102 | support: this.videoValidatorsService.VIDEO_SUPPORT, |
1e74f19a | 103 | schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT, |
104 | originallyPublishedAt: this.videoValidatorsService.VIDEO_ORIGINALLY_PUBLISHED_AT | |
d18d6478 | 105 | } |
ff249f49 | 106 | |
d18d6478 C |
107 | this.formValidatorService.updateForm( |
108 | this.form, | |
109 | this.formErrors, | |
110 | this.validationMessages, | |
111 | obj, | |
112 | defaultValues | |
113 | ) | |
74af5145 | 114 | |
40e87e9e C |
115 | this.form.addControl('captions', new FormArray([ |
116 | new FormGroup({ | |
117 | language: new FormControl(), | |
118 | captionfile: new FormControl() | |
119 | }) | |
120 | ])) | |
121 | ||
bbe0f064 C |
122 | this.trackChannelChange() |
123 | this.trackPrivacyChange() | |
124 | } | |
125 | ||
126 | ngOnInit () { | |
127 | this.updateForm() | |
128 | ||
ba430d75 C |
129 | this.serverService.getVideoCategories() |
130 | .subscribe(res => this.videoCategories = res) | |
131 | this.serverService.getVideoLicences() | |
132 | .subscribe(res => this.videoLicences = res) | |
02c01341 RK |
133 | forkJoin([ |
134 | this.instanceService.getAbout(), | |
135 | this.serverService.getVideoLanguages() | |
136 | ]).pipe(map(([ about, languages ]) => ({ about, languages }))) | |
137 | .subscribe(res => { | |
138 | this.videoLanguages = res.languages | |
139 | .map(l => res.about.instance.languages.includes(l.id) | |
140 | ? { ...l, group: this.i18n('Instance languages'), groupOrder: 0 } | |
141 | : { ...l, group: this.i18n('All languages'), groupOrder: 1 }) | |
142 | .sort((a, b) => a.groupOrder - b.groupOrder) | |
143 | }) | |
ba430d75 C |
144 | |
145 | this.serverService.getVideoPrivacies() | |
02c01341 RK |
146 | .subscribe(privacies => { |
147 | this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies) | |
148 | if (this.schedulePublicationPossible) { | |
149 | this.videoPrivacies.push({ | |
150 | id: this.SPECIAL_SCHEDULED_PRIVACY, | |
151 | label: this.i18n('Scheduled'), | |
152 | description: this.i18n('Hide the video until a specific date') | |
153 | }) | |
154 | } | |
155 | }) | |
bbe0f064 | 156 | |
ba430d75 C |
157 | this.serverConfig = this.serverService.getTmpConfig() |
158 | this.serverService.getConfig() | |
159 | .subscribe(config => this.serverConfig = config) | |
851f5daa | 160 | |
772d5642 | 161 | this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id) |
84c7cde6 C |
162 | |
163 | this.ngZone.runOutsideAngular(() => { | |
164 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute | |
165 | }) | |
40e87e9e C |
166 | } |
167 | ||
168 | ngOnDestroy () { | |
169 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
170 | } | |
171 | ||
40e87e9e | 172 | onCaptionAdded (caption: VideoCaptionEdit) { |
f4001cf4 C |
173 | const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id) |
174 | ||
175 | // Replace existing caption? | |
176 | if (existingCaption) { | |
177 | Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' }) | |
ad774752 C |
178 | } else { |
179 | this.videoCaptions.push( | |
180 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
181 | ) | |
f4001cf4 C |
182 | } |
183 | ||
ad774752 | 184 | this.sortVideoCaptions() |
40e87e9e C |
185 | } |
186 | ||
772d5642 C |
187 | async deleteCaption (caption: VideoCaptionEdit) { |
188 | // Caption recovers his former state | |
189 | if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) { | |
190 | caption.action = undefined | |
191 | return | |
192 | } | |
193 | ||
40e87e9e C |
194 | // This caption is not on the server, just remove it from our array |
195 | if (caption.action === 'CREATE') { | |
196 | removeElementFromArray(this.videoCaptions, caption) | |
197 | return | |
198 | } | |
199 | ||
200 | caption.action = 'REMOVE' as 'REMOVE' | |
201 | } | |
202 | ||
203 | openAddCaptionModal () { | |
204 | this.videoCaptionAddModal.show() | |
bbe0f064 C |
205 | } |
206 | ||
ad774752 C |
207 | private sortVideoCaptions () { |
208 | this.videoCaptions.sort((v1, v2) => { | |
209 | if (v1.language.label < v2.language.label) return -1 | |
210 | if (v1.language.label === v2.language.label) return 0 | |
211 | ||
212 | return 1 | |
213 | }) | |
214 | } | |
215 | ||
bbe0f064 | 216 | private trackPrivacyChange () { |
4adf2673 | 217 | // We will update the schedule input and the wait transcoding checkbox validators |
bbe0f064 C |
218 | this.form.controls[ 'privacy' ] |
219 | .valueChanges | |
220 | .pipe(map(res => parseInt(res.toString(), 10))) | |
221 | .subscribe( | |
222 | newPrivacyId => { | |
0f7fedc3 | 223 | |
bbe0f064 C |
224 | this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY |
225 | ||
226 | // Value changed | |
227 | const scheduleControl = this.form.get('schedulePublicationAt') | |
228 | const waitTranscodingControl = this.form.get('waitTranscoding') | |
229 | ||
230 | if (this.schedulePublicationEnabled) { | |
231 | scheduleControl.setValidators([ Validators.required ]) | |
232 | ||
233 | waitTranscodingControl.disable() | |
234 | waitTranscodingControl.setValue(false) | |
235 | } else { | |
236 | scheduleControl.clearValidators() | |
237 | ||
238 | waitTranscodingControl.enable() | |
0f7fedc3 C |
239 | |
240 | // Do not update the control value on first patch (values come from the server) | |
241 | if (this.firstPatchDone === true) { | |
242 | waitTranscodingControl.setValue(true) | |
243 | } | |
bbe0f064 C |
244 | } |
245 | ||
246 | scheduleControl.updateValueAndValidity() | |
247 | waitTranscodingControl.updateValueAndValidity() | |
0f7fedc3 C |
248 | |
249 | this.firstPatchDone = true | |
250 | ||
bbe0f064 C |
251 | } |
252 | ) | |
253 | } | |
254 | ||
255 | private trackChannelChange () { | |
74af5145 | 256 | // We will update the "support" field depending on the channel |
2186386c | 257 | this.form.controls[ 'channelId' ] |
74af5145 C |
258 | .valueChanges |
259 | .pipe(map(res => parseInt(res.toString(), 10))) | |
260 | .subscribe( | |
261 | newChannelId => { | |
2186386c | 262 | const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10) |
74af5145 C |
263 | |
264 | // Not initialized yet | |
265 | if (isNaN(newChannelId)) return | |
266 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
d18d6478 | 267 | if (!newChannel) return |
74af5145 | 268 | |
6f79be11 C |
269 | // Wait support field update |
270 | setTimeout(() => { | |
271 | const currentSupport = this.form.value[ 'support' ] | |
74af5145 | 272 | |
6f79be11 C |
273 | // First time we set the channel? |
274 | if (isNaN(oldChannelId) && !currentSupport) return this.updateSupportField(newChannel.support) | |
275 | ||
276 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
277 | if (!newChannel || !oldChannel) { | |
278 | console.error('Cannot find new or old channel.') | |
279 | return | |
280 | } | |
74af5145 | 281 | |
6f79be11 C |
282 | // If the current support text is not the same than the old channel, the user updated it. |
283 | // We don't want the user to lose his text, so stop here | |
284 | if (currentSupport && currentSupport !== oldChannel.support) return | |
74af5145 | 285 | |
6f79be11 C |
286 | // Update the support text with our new channel |
287 | this.updateSupportField(newChannel.support) | |
288 | }) | |
74af5145 C |
289 | } |
290 | ) | |
ff249f49 C |
291 | } |
292 | ||
74af5145 C |
293 | private updateSupportField (support: string) { |
294 | return this.form.patchValue({ support: support || '' }) | |
295 | } | |
ff249f49 | 296 | } |