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