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