]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/shared/video-edit.component.ts
Update build steps for localization
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / shared / video-edit.component.ts
1 import { forkJoin } from 'rxjs'
2 import { map } from 'rxjs/operators'
3 import { Component, Input, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
4 import { FormArray, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'
5 import { ServerService } from '@app/core'
6 import { removeElementFromArray } from '@app/helpers'
7 import { FormReactiveValidationMessages, FormValidatorService, SelectChannelItem, VideoValidatorsService } from '@app/shared/shared-forms'
8 import { InstanceService } from '@app/shared/shared-instance'
9 import { VideoCaptionEdit, VideoEdit, VideoService } from '@app/shared/shared-main'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { ServerConfig, VideoConstant, VideoPrivacy } from '@shared/models'
12 import { I18nPrimengCalendarService } from './i18n-primeng-calendar.service'
13 import { VideoCaptionAddModalComponent } from './video-caption-add-modal.component'
14
15 type VideoLanguages = VideoConstant<string> & { group?: string }
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() userVideoChannels: SelectChannelItem[] = []
27 @Input() schedulePublicationPossible = true
28 @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = []
29 @Input() waitTranscodingEnabled = true
30
31 @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent
32
33 // So that it can be accessed in the template
34 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
35
36 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
37 videoCategories: VideoConstant<number>[] = []
38 videoLicences: VideoConstant<number>[] = []
39 videoLanguages: VideoLanguages[] = []
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 serverConfig: ServerConfig
54
55 private schedulerInterval: any
56 private firstPatchDone = false
57 private initialVideoCaptions: string[] = []
58
59 constructor (
60 private formValidatorService: FormValidatorService,
61 private videoValidatorsService: VideoValidatorsService,
62 private videoService: VideoService,
63 private serverService: ServerService,
64 private instanceService: InstanceService,
65 private i18nPrimengCalendarService: I18nPrimengCalendarService,
66 private i18n: I18n,
67 private ngZone: NgZone
68 ) {
69 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
70 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
71 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
72 }
73
74 get existingCaptions () {
75 return this.videoCaptions
76 .filter(c => c.action !== 'REMOVE')
77 .map(c => c.language.id)
78 }
79
80 updateForm () {
81 const defaultValues: any = {
82 nsfw: 'false',
83 commentsEnabled: 'true',
84 downloadEnabled: '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 downloadEnabled: null,
95 waitTranscoding: null,
96 category: this.videoValidatorsService.VIDEO_CATEGORY,
97 licence: this.videoValidatorsService.VIDEO_LICENCE,
98 language: this.videoValidatorsService.VIDEO_LANGUAGE,
99 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
100 tags: this.videoValidatorsService.VIDEO_TAGS_ARRAY,
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.serverService.getVideoCategories()
130 .subscribe(res => this.videoCategories = res)
131 this.serverService.getVideoLicences()
132 .subscribe(res => this.videoLicences = res)
133 forkJoin([
134 this.instanceService.getAbout(),
135 this.serverService.getVideoLanguages()
136 ]).pipe(map(([ about, languages ]) => ({ about, languages })))
137 .subscribe(res => {
138 this.videoLanguages = res.languages
139 .map(l => res.about.instance.languages.includes(l.id)
140 ? { ...l, group: this.i18n('Instance languages'), groupOrder: 0 }
141 : { ...l, group: this.i18n('All languages'), groupOrder: 1 })
142 .sort((a, b) => a.groupOrder - b.groupOrder)
143 })
144
145 this.serverService.getVideoPrivacies()
146 .subscribe(privacies => {
147 this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies)
148 if (this.schedulePublicationPossible) {
149 this.videoPrivacies.push({
150 id: this.SPECIAL_SCHEDULED_PRIVACY,
151 label: this.i18n('Scheduled'),
152 description: this.i18n('Hide the video until a specific date')
153 })
154 }
155 })
156
157 this.serverConfig = this.serverService.getTmpConfig()
158 this.serverService.getConfig()
159 .subscribe(config => this.serverConfig = config)
160
161 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
162
163 this.ngZone.runOutsideAngular(() => {
164 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
165 })
166 }
167
168 ngOnDestroy () {
169 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
170 }
171
172 onCaptionAdded (caption: VideoCaptionEdit) {
173 const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id)
174
175 // Replace existing caption?
176 if (existingCaption) {
177 Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' })
178 } else {
179 this.videoCaptions.push(
180 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
181 )
182 }
183
184 this.sortVideoCaptions()
185 }
186
187 async deleteCaption (caption: VideoCaptionEdit) {
188 // Caption recovers his former state
189 if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) {
190 caption.action = undefined
191 return
192 }
193
194 // This caption is not on the server, just remove it from our array
195 if (caption.action === 'CREATE') {
196 removeElementFromArray(this.videoCaptions, caption)
197 return
198 }
199
200 caption.action = 'REMOVE' as 'REMOVE'
201 }
202
203 openAddCaptionModal () {
204 this.videoCaptionAddModal.show()
205 }
206
207 private sortVideoCaptions () {
208 this.videoCaptions.sort((v1, v2) => {
209 if (v1.language.label < v2.language.label) return -1
210 if (v1.language.label === v2.language.label) return 0
211
212 return 1
213 })
214 }
215
216 private trackPrivacyChange () {
217 // We will update the schedule input and the wait transcoding checkbox validators
218 this.form.controls[ 'privacy' ]
219 .valueChanges
220 .pipe(map(res => parseInt(res.toString(), 10)))
221 .subscribe(
222 newPrivacyId => {
223
224 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
225
226 // Value changed
227 const scheduleControl = this.form.get('schedulePublicationAt')
228 const waitTranscodingControl = this.form.get('waitTranscoding')
229
230 if (this.schedulePublicationEnabled) {
231 scheduleControl.setValidators([ Validators.required ])
232
233 waitTranscodingControl.disable()
234 waitTranscodingControl.setValue(false)
235 } else {
236 scheduleControl.clearValidators()
237
238 waitTranscodingControl.enable()
239
240 // Do not update the control value on first patch (values come from the server)
241 if (this.firstPatchDone === true) {
242 waitTranscodingControl.setValue(true)
243 }
244 }
245
246 scheduleControl.updateValueAndValidity()
247 waitTranscodingControl.updateValueAndValidity()
248
249 this.firstPatchDone = true
250
251 }
252 )
253 }
254
255 private trackChannelChange () {
256 // We will update the "support" field depending on the channel
257 this.form.controls[ 'channelId' ]
258 .valueChanges
259 .pipe(map(res => parseInt(res.toString(), 10)))
260 .subscribe(
261 newChannelId => {
262 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
263
264 // Not initialized yet
265 if (isNaN(newChannelId)) return
266 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
267 if (!newChannel) return
268
269 // Wait support field update
270 setTimeout(() => {
271 const currentSupport = this.form.value[ 'support' ]
272
273 // First time we set the channel?
274 if (isNaN(oldChannelId) && !currentSupport) return this.updateSupportField(newChannel.support)
275
276 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
277 if (!newChannel || !oldChannel) {
278 console.error('Cannot find new or old channel.')
279 return
280 }
281
282 // If the current support text is not the same than the old channel, the user updated it.
283 // We don't want the user to lose his text, so stop here
284 if (currentSupport && currentSupport !== oldChannel.support) return
285
286 // Update the support text with our new channel
287 this.updateSupportField(newChannel.support)
288 })
289 }
290 )
291 }
292
293 private updateSupportField (support: string) {
294 return this.form.patchValue({ support: support || '' })
295 }
296 }