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