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