]>
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' |
ff249f49 | 5 | import { NotificationsService } from 'angular2-notifications' |
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' | |
ef4c78da | 15 | import { VideoConstant } 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 = {} |
ef4c78da | 26 | @Input() videoPrivacies: { id: number, label: string }[] = [] |
74af5145 | 27 | @Input() userVideoChannels: { id: number, label: string, support: string }[] = [] |
bbe0f064 | 28 | @Input() schedulePublicationPossible = true |
40e87e9e C |
29 | @Input() videoCaptions: VideoCaptionEdit[] = [] |
30 | ||
31 | @ViewChild('videoCaptionAddModal') 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 | |
ef4c78da C |
36 | videoCategories: VideoConstant<string>[] = [] |
37 | videoLicences: VideoConstant<string>[] = [] | |
38 | videoLanguages: VideoConstant<string>[] = [] | |
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() | |
47 | ||
48 | calendarTimezone: string | |
49 | calendarDateFormat: string | |
ff249f49 | 50 | |
40e87e9e | 51 | private schedulerInterval |
0f7fedc3 | 52 | private firstPatchDone = false |
40e87e9e | 53 | |
ff249f49 | 54 | constructor ( |
d18d6478 | 55 | private formValidatorService: FormValidatorService, |
e309822b | 56 | private videoValidatorsService: VideoValidatorsService, |
40e87e9e | 57 | private videoCaptionService: VideoCaptionService, |
ff249f49 C |
58 | private route: ActivatedRoute, |
59 | private router: Router, | |
60 | private notificationsService: NotificationsService, | |
bbe0f064 C |
61 | private serverService: ServerService, |
62 | private i18nPrimengCalendarService: I18nPrimengCalendarService | |
e309822b C |
63 | ) { |
64 | this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS | |
65 | this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES | |
bbe0f064 C |
66 | |
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 () { |
d18d6478 C |
79 | const defaultValues = { |
80 | nsfw: 'false', | |
81 | commentsEnabled: 'true', | |
2186386c | 82 | waitTranscoding: 'true', |
d18d6478 C |
83 | tags: [] |
84 | } | |
85 | const obj = { | |
e309822b C |
86 | name: this.videoValidatorsService.VIDEO_NAME, |
87 | privacy: this.videoValidatorsService.VIDEO_PRIVACY, | |
88 | channelId: this.videoValidatorsService.VIDEO_CHANNEL, | |
d18d6478 C |
89 | nsfw: null, |
90 | commentsEnabled: null, | |
2186386c | 91 | waitTranscoding: null, |
e309822b C |
92 | category: this.videoValidatorsService.VIDEO_CATEGORY, |
93 | licence: this.videoValidatorsService.VIDEO_LICENCE, | |
94 | language: this.videoValidatorsService.VIDEO_LANGUAGE, | |
95 | description: this.videoValidatorsService.VIDEO_DESCRIPTION, | |
d18d6478 C |
96 | tags: null, |
97 | thumbnailfile: null, | |
98 | previewfile: null, | |
bbe0f064 C |
99 | support: this.videoValidatorsService.VIDEO_SUPPORT, |
100 | schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT | |
d18d6478 | 101 | } |
ff249f49 | 102 | |
d18d6478 C |
103 | this.formValidatorService.updateForm( |
104 | this.form, | |
105 | this.formErrors, | |
106 | this.validationMessages, | |
107 | obj, | |
108 | defaultValues | |
109 | ) | |
74af5145 | 110 | |
40e87e9e C |
111 | this.form.addControl('captions', new FormArray([ |
112 | new FormGroup({ | |
113 | language: new FormControl(), | |
114 | captionfile: new FormControl() | |
115 | }) | |
116 | ])) | |
117 | ||
bbe0f064 C |
118 | this.trackChannelChange() |
119 | this.trackPrivacyChange() | |
120 | } | |
121 | ||
122 | ngOnInit () { | |
123 | this.updateForm() | |
124 | ||
125 | this.videoCategories = this.serverService.getVideoCategories() | |
126 | this.videoLicences = this.serverService.getVideoLicences() | |
127 | this.videoLanguages = this.serverService.getVideoLanguages() | |
128 | ||
40e87e9e C |
129 | this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute |
130 | } | |
131 | ||
132 | ngOnDestroy () { | |
133 | if (this.schedulerInterval) clearInterval(this.schedulerInterval) | |
134 | } | |
135 | ||
40e87e9e | 136 | onCaptionAdded (caption: VideoCaptionEdit) { |
f4001cf4 C |
137 | const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id) |
138 | ||
139 | // Replace existing caption? | |
140 | if (existingCaption) { | |
141 | Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' }) | |
142 | return | |
143 | } | |
144 | ||
40e87e9e C |
145 | this.videoCaptions.push( |
146 | Object.assign(caption, { action: 'CREATE' as 'CREATE' }) | |
147 | ) | |
148 | } | |
149 | ||
150 | deleteCaption (caption: VideoCaptionEdit) { | |
151 | // This caption is not on the server, just remove it from our array | |
152 | if (caption.action === 'CREATE') { | |
153 | removeElementFromArray(this.videoCaptions, caption) | |
154 | return | |
155 | } | |
156 | ||
157 | caption.action = 'REMOVE' as 'REMOVE' | |
158 | } | |
159 | ||
160 | openAddCaptionModal () { | |
161 | this.videoCaptionAddModal.show() | |
bbe0f064 C |
162 | } |
163 | ||
164 | private trackPrivacyChange () { | |
165 | // We will update the "support" field depending on the channel | |
166 | this.form.controls[ 'privacy' ] | |
167 | .valueChanges | |
168 | .pipe(map(res => parseInt(res.toString(), 10))) | |
169 | .subscribe( | |
170 | newPrivacyId => { | |
0f7fedc3 | 171 | |
bbe0f064 C |
172 | this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY |
173 | ||
174 | // Value changed | |
175 | const scheduleControl = this.form.get('schedulePublicationAt') | |
176 | const waitTranscodingControl = this.form.get('waitTranscoding') | |
177 | ||
178 | if (this.schedulePublicationEnabled) { | |
179 | scheduleControl.setValidators([ Validators.required ]) | |
180 | ||
181 | waitTranscodingControl.disable() | |
182 | waitTranscodingControl.setValue(false) | |
183 | } else { | |
184 | scheduleControl.clearValidators() | |
185 | ||
186 | waitTranscodingControl.enable() | |
0f7fedc3 C |
187 | |
188 | // Do not update the control value on first patch (values come from the server) | |
189 | if (this.firstPatchDone === true) { | |
190 | waitTranscodingControl.setValue(true) | |
191 | } | |
bbe0f064 C |
192 | } |
193 | ||
194 | scheduleControl.updateValueAndValidity() | |
195 | waitTranscodingControl.updateValueAndValidity() | |
0f7fedc3 C |
196 | |
197 | this.firstPatchDone = true | |
198 | ||
bbe0f064 C |
199 | } |
200 | ) | |
201 | } | |
202 | ||
203 | private trackChannelChange () { | |
74af5145 | 204 | // We will update the "support" field depending on the channel |
2186386c | 205 | this.form.controls[ 'channelId' ] |
74af5145 C |
206 | .valueChanges |
207 | .pipe(map(res => parseInt(res.toString(), 10))) | |
208 | .subscribe( | |
209 | newChannelId => { | |
2186386c C |
210 | const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10) |
211 | const currentSupport = this.form.value[ 'support' ] | |
74af5145 C |
212 | |
213 | // Not initialized yet | |
214 | if (isNaN(newChannelId)) return | |
215 | const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) | |
d18d6478 | 216 | if (!newChannel) return |
74af5145 C |
217 | |
218 | // First time we set the channel? | |
219 | if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support) | |
220 | const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId) | |
221 | ||
222 | if (!newChannel || !oldChannel) { | |
223 | console.error('Cannot find new or old channel.') | |
224 | return | |
225 | } | |
226 | ||
227 | // If the current support text is not the same than the old channel, the user updated it. | |
228 | // We don't want the user to lose his text, so stop here | |
229 | if (currentSupport && currentSupport !== oldChannel.support) return | |
230 | ||
231 | // Update the support text with our new channel | |
232 | this.updateSupportField(newChannel.support) | |
233 | } | |
234 | ) | |
ff249f49 C |
235 | } |
236 | ||
74af5145 C |
237 | private updateSupportField (support: string) { |
238 | return this.form.patchValue({ support: support || '' }) | |
239 | } | |
ff249f49 | 240 | } |