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