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