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