]>
Commit | Line | Data |
---|---|---|
40e87e9e C |
1 | import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core' |
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' | |
8cd7faaa | 15 | import { VideoConstant, VideoPrivacy } from '../../../../../../shared' |
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 = {} |
8cd7faaa | 26 | @Input() videoPrivacies: VideoConstant<VideoPrivacy>[] = [] |
74af5145 | 27 | @Input() userVideoChannels: { id: number, label: string, support: string }[] = [] |
bbe0f064 | 28 | @Input() schedulePublicationPossible = true |
40e87e9e | 29 | @Input() videoCaptions: VideoCaptionEdit[] = [] |
14e2014a | 30 | @Input() waitTranscodingEnabled = true |
40e87e9e C |
31 | |
32 | @ViewChild('videoCaptionAddModal') 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 | |
8cd7faaa C |
37 | videoCategories: VideoConstant<number>[] = [] |
38 | videoLicences: VideoConstant<number>[] = [] | |
ef4c78da | 39 | videoLanguages: VideoConstant<string>[] = [] |
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() | |
48 | ||
49 | calendarTimezone: string | |
50 | calendarDateFormat: string | |
ff249f49 | 51 | |
244b4ae3 | 52 | private schedulerInterval: any |
0f7fedc3 | 53 | private firstPatchDone = false |
772d5642 | 54 | private initialVideoCaptions: string[] = [] |
40e87e9e | 55 | |
ff249f49 | 56 | constructor ( |
d18d6478 | 57 | private formValidatorService: FormValidatorService, |
e309822b | 58 | private videoValidatorsService: VideoValidatorsService, |
40e87e9e | 59 | private videoCaptionService: VideoCaptionService, |
ff249f49 C |
60 | private route: ActivatedRoute, |
61 | private router: Router, | |
f8b2c1b4 | 62 | private notifier: Notifier, |
bbe0f064 C |
63 | private serverService: ServerService, |
64 | private i18nPrimengCalendarService: I18nPrimengCalendarService | |
e309822b C |
65 | ) { |
66 | this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS | |
67 | this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES | |
bbe0f064 C |
68 | |
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', | |
2186386c | 84 | waitTranscoding: 'true', |
d18d6478 C |
85 | tags: [] |
86 | } | |
244b4ae3 | 87 | const obj: any = { |
e309822b C |
88 | name: this.videoValidatorsService.VIDEO_NAME, |
89 | privacy: this.videoValidatorsService.VIDEO_PRIVACY, | |
90 | channelId: this.videoValidatorsService.VIDEO_CHANNEL, | |
d18d6478 C |
91 | nsfw: null, |
92 | commentsEnabled: 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, | |
d18d6478 C |
98 | tags: null, |
99 | thumbnailfile: null, | |
100 | previewfile: null, | |
bbe0f064 C |
101 | support: this.videoValidatorsService.VIDEO_SUPPORT, |
102 | schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_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 | ||
127 | this.videoCategories = this.serverService.getVideoCategories() | |
128 | this.videoLicences = this.serverService.getVideoLicences() | |
129 | this.videoLanguages = this.serverService.getVideoLanguages() | |
130 | ||
40e87e9e | 131 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute |
772d5642 C |
132 | |
133 | this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id) | |
40e87e9e C |
134 | } |
135 | ||
136 | ngOnDestroy () { | |
137 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
138 | } | |
139 | ||
40e87e9e | 140 | onCaptionAdded (caption: VideoCaptionEdit) { |
f4001cf4 C |
141 | const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id) |
142 | ||
143 | // Replace existing caption? | |
144 | if (existingCaption) { | |
145 | Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' }) | |
ad774752 C |
146 | } else { |
147 | this.videoCaptions.push( | |
148 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
149 | ) | |
f4001cf4 C |
150 | } |
151 | ||
ad774752 | 152 | this.sortVideoCaptions() |
40e87e9e C |
153 | } |
154 | ||
772d5642 C |
155 | async deleteCaption (caption: VideoCaptionEdit) { |
156 | // Caption recovers his former state | |
157 | if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) { | |
158 | caption.action = undefined | |
159 | return | |
160 | } | |
161 | ||
40e87e9e C |
162 | // This caption is not on the server, just remove it from our array |
163 | if (caption.action === 'CREATE') { | |
164 | removeElementFromArray(this.videoCaptions, caption) | |
165 | return | |
166 | } | |
167 | ||
168 | caption.action = 'REMOVE' as 'REMOVE' | |
169 | } | |
170 | ||
171 | openAddCaptionModal () { | |
172 | this.videoCaptionAddModal.show() | |
bbe0f064 C |
173 | } |
174 | ||
ad774752 C |
175 | private sortVideoCaptions () { |
176 | this.videoCaptions.sort((v1, v2) => { | |
177 | if (v1.language.label < v2.language.label) return -1 | |
178 | if (v1.language.label === v2.language.label) return 0 | |
179 | ||
180 | return 1 | |
181 | }) | |
182 | } | |
183 | ||
bbe0f064 C |
184 | private trackPrivacyChange () { |
185 | // We will update the "support" field depending on the channel | |
186 | this.form.controls[ 'privacy' ] | |
187 | .valueChanges | |
188 | .pipe(map(res => parseInt(res.toString(), 10))) | |
189 | .subscribe( | |
190 | newPrivacyId => { | |
0f7fedc3 | 191 | |
bbe0f064 C |
192 | this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY |
193 | ||
194 | // Value changed | |
195 | const scheduleControl = this.form.get('schedulePublicationAt') | |
196 | const waitTranscodingControl = this.form.get('waitTranscoding') | |
197 | ||
198 | if (this.schedulePublicationEnabled) { | |
199 | scheduleControl.setValidators([ Validators.required ]) | |
200 | ||
201 | waitTranscodingControl.disable() | |
202 | waitTranscodingControl.setValue(false) | |
203 | } else { | |
204 | scheduleControl.clearValidators() | |
205 | ||
206 | waitTranscodingControl.enable() | |
0f7fedc3 C |
207 | |
208 | // Do not update the control value on first patch (values come from the server) | |
209 | if (this.firstPatchDone === true) { | |
210 | waitTranscodingControl.setValue(true) | |
211 | } | |
bbe0f064 C |
212 | } |
213 | ||
214 | scheduleControl.updateValueAndValidity() | |
215 | waitTranscodingControl.updateValueAndValidity() | |
0f7fedc3 C |
216 | |
217 | this.firstPatchDone = true | |
218 | ||
bbe0f064 C |
219 | } |
220 | ) | |
221 | } | |
222 | ||
223 | private trackChannelChange () { | |
74af5145 | 224 | // We will update the "support" field depending on the channel |
2186386c | 225 | this.form.controls[ 'channelId' ] |
74af5145 C |
226 | .valueChanges |
227 | .pipe(map(res => parseInt(res.toString(), 10))) | |
228 | .subscribe( | |
229 | newChannelId => { | |
2186386c C |
230 | const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10) |
231 | const currentSupport = this.form.value[ 'support' ] | |
74af5145 C |
232 | |
233 | // Not initialized yet | |
234 | if (isNaN(newChannelId)) return | |
235 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
d18d6478 | 236 | if (!newChannel) return |
74af5145 C |
237 | |
238 | // First time we set the channel? | |
239 | if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support) | |
240 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
241 | ||
242 | if (!newChannel || !oldChannel) { | |
243 | console.error('Cannot find new or old channel.') | |
244 | return | |
245 | } | |
246 | ||
247 | // If the current support text is not the same than the old channel, the user updated it. | |
248 | // We don't want the user to lose his text, so stop here | |
249 | if (currentSupport && currentSupport !== oldChannel.support) return | |
250 | ||
251 | // Update the support text with our new channel | |
252 | this.updateSupportField(newChannel.support) | |
253 | } | |
254 | ) | |
ff249f49 C |
255 | } |
256 | ||
74af5145 C |
257 | private updateSupportField (support: string) { |
258 | return this.form.patchValue({ support: support || '' }) | |
259 | } | |
ff249f49 | 260 | } |