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