]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/shared/video-edit.component.ts
d02f18ee946b38c15e8992e53a3a7876fef6aff2
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / shared / video-edit.component.ts
1 import { Component, Input, 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[] = []
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 ) {
67 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
68 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
69
70 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
71 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
72 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
73 }
74
75 get existingCaptions () {
76 return this.videoCaptions
77 .filter(c => c.action !== 'REMOVE')
78 .map(c => c.language.id)
79 }
80
81 updateForm () {
82 const defaultValues: any = {
83 nsfw: 'false',
84 commentsEnabled: 'true',
85 waitTranscoding: 'true',
86 tags: []
87 }
88 const obj: any = {
89 name: this.videoValidatorsService.VIDEO_NAME,
90 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
91 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
92 nsfw: null,
93 commentsEnabled: null,
94 waitTranscoding: null,
95 category: this.videoValidatorsService.VIDEO_CATEGORY,
96 licence: this.videoValidatorsService.VIDEO_LICENCE,
97 language: this.videoValidatorsService.VIDEO_LANGUAGE,
98 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
99 tags: null,
100 thumbnailfile: null,
101 previewfile: null,
102 support: this.videoValidatorsService.VIDEO_SUPPORT,
103 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT,
104 originallyPublishedAt: this.videoValidatorsService.VIDEO_ORIGINALLY_PUBLISHED_AT
105 }
106
107 this.formValidatorService.updateForm(
108 this.form,
109 this.formErrors,
110 this.validationMessages,
111 obj,
112 defaultValues
113 )
114
115 this.form.addControl('captions', new FormArray([
116 new FormGroup({
117 language: new FormControl(),
118 captionfile: new FormControl()
119 })
120 ]))
121
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
133 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
134
135 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
136 }
137
138 ngOnDestroy () {
139 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
140 }
141
142 onCaptionAdded (caption: VideoCaptionEdit) {
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' })
148 } else {
149 this.videoCaptions.push(
150 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
151 )
152 }
153
154 this.sortVideoCaptions()
155 }
156
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
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()
175 }
176
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
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 => {
193
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()
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 }
214 }
215
216 scheduleControl.updateValueAndValidity()
217 waitTranscodingControl.updateValueAndValidity()
218
219 this.firstPatchDone = true
220
221 }
222 )
223 }
224
225 private trackChannelChange () {
226 // We will update the "support" field depending on the channel
227 this.form.controls[ 'channelId' ]
228 .valueChanges
229 .pipe(map(res => parseInt(res.toString(), 10)))
230 .subscribe(
231 newChannelId => {
232 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
233 const currentSupport = this.form.value[ 'support' ]
234
235 // Not initialized yet
236 if (isNaN(newChannelId)) return
237 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
238 if (!newChannel) return
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 )
257 }
258
259 private updateSupportField (support: string) {
260 return this.form.patchValue({ support: support || '' })
261 }
262 }