]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
84c7cde6 1import { Component, Input, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
40e87e9e 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'
851f5daa 16import { VideoService } from '@app/shared/video/video.service'
ff249f49
C
17
18@Component({
19 selector: 'my-video-edit',
20 styleUrls: [ './video-edit.component.scss' ],
21 templateUrl: './video-edit.component.html'
22})
40e87e9e 23export class VideoEditComponent implements OnInit, OnDestroy {
ff249f49
C
24 @Input() form: FormGroup
25 @Input() formErrors: { [ id: string ]: string } = {}
e309822b 26 @Input() validationMessages: FormReactiveValidationMessages = {}
74af5145 27 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
bbe0f064 28 @Input() schedulePublicationPossible = true
6913f691 29 @Input() videoCaptions: (VideoCaptionEdit & { captionPath?: string })[] = []
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
851f5daa 37 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
8cd7faaa
C
38 videoCategories: VideoConstant<number>[] = []
39 videoLicences: VideoConstant<number>[] = []
ef4c78da 40 videoLanguages: VideoConstant<string>[] = []
ff249f49 41
e309822b
C
42 tagValidators: ValidatorFn[]
43 tagValidatorsMessages: { [ name: string ]: string }
ff249f49 44
bbe0f064
C
45 schedulePublicationEnabled = false
46
bbe0f064
C
47 calendarLocale: any = {}
48 minScheduledDate = new Date()
1e74f19a 49 myYearRange = '1880:' + (new Date()).getFullYear()
bbe0f064
C
50
51 calendarTimezone: string
52 calendarDateFormat: string
ff249f49 53
244b4ae3 54 private schedulerInterval: any
0f7fedc3 55 private firstPatchDone = false
772d5642 56 private initialVideoCaptions: string[] = []
40e87e9e 57
ff249f49 58 constructor (
d18d6478 59 private formValidatorService: FormValidatorService,
e309822b 60 private videoValidatorsService: VideoValidatorsService,
40e87e9e 61 private videoCaptionService: VideoCaptionService,
851f5daa 62 private videoService: VideoService,
ff249f49
C
63 private route: ActivatedRoute,
64 private router: Router,
f8b2c1b4 65 private notifier: Notifier,
bbe0f064 66 private serverService: ServerService,
84c7cde6
C
67 private i18nPrimengCalendarService: I18nPrimengCalendarService,
68 private ngZone: NgZone
e309822b
C
69 ) {
70 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
71 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
bbe0f064
C
72
73 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
74 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
75 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
e309822b 76 }
ff249f49 77
f4001cf4
C
78 get existingCaptions () {
79 return this.videoCaptions
80 .filter(c => c.action !== 'REMOVE')
81 .map(c => c.language.id)
82 }
83
ff249f49 84 updateForm () {
244b4ae3 85 const defaultValues: any = {
d18d6478
C
86 nsfw: 'false',
87 commentsEnabled: 'true',
7f2cfe3a 88 downloadEnabled: 'true',
2186386c 89 waitTranscoding: 'true',
d18d6478
C
90 tags: []
91 }
244b4ae3 92 const obj: any = {
e309822b
C
93 name: this.videoValidatorsService.VIDEO_NAME,
94 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
95 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
d18d6478
C
96 nsfw: null,
97 commentsEnabled: null,
7f2cfe3a 98 downloadEnabled: null,
2186386c 99 waitTranscoding: null,
e309822b
C
100 category: this.videoValidatorsService.VIDEO_CATEGORY,
101 licence: this.videoValidatorsService.VIDEO_LICENCE,
102 language: this.videoValidatorsService.VIDEO_LANGUAGE,
103 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
d18d6478 104 tags: null,
d18d6478 105 previewfile: null,
bbe0f064 106 support: this.videoValidatorsService.VIDEO_SUPPORT,
1e74f19a 107 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT,
108 originallyPublishedAt: this.videoValidatorsService.VIDEO_ORIGINALLY_PUBLISHED_AT
d18d6478 109 }
ff249f49 110
d18d6478
C
111 this.formValidatorService.updateForm(
112 this.form,
113 this.formErrors,
114 this.validationMessages,
115 obj,
116 defaultValues
117 )
74af5145 118
40e87e9e
C
119 this.form.addControl('captions', new FormArray([
120 new FormGroup({
121 language: new FormControl(),
122 captionfile: new FormControl()
123 })
124 ]))
125
bbe0f064
C
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
851f5daa
C
137 const privacies = this.serverService.getVideoPrivacies()
138 this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies)
139
772d5642 140 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
84c7cde6
C
141
142 this.ngZone.runOutsideAngular(() => {
143 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
144 })
40e87e9e
C
145 }
146
147 ngOnDestroy () {
148 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
149 }
150
40e87e9e 151 onCaptionAdded (caption: VideoCaptionEdit) {
f4001cf4
C
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' })
ad774752
C
157 } else {
158 this.videoCaptions.push(
159 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
160 )
f4001cf4
C
161 }
162
ad774752 163 this.sortVideoCaptions()
40e87e9e
C
164 }
165
772d5642
C
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
40e87e9e
C
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()
bbe0f064
C
184 }
185
ad774752
C
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
bbe0f064 195 private trackPrivacyChange () {
4adf2673 196 // We will update the schedule input and the wait transcoding checkbox validators
bbe0f064
C
197 this.form.controls[ 'privacy' ]
198 .valueChanges
199 .pipe(map(res => parseInt(res.toString(), 10)))
200 .subscribe(
201 newPrivacyId => {
0f7fedc3 202
bbe0f064
C
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()
0f7fedc3
C
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 }
bbe0f064
C
223 }
224
225 scheduleControl.updateValueAndValidity()
226 waitTranscodingControl.updateValueAndValidity()
0f7fedc3
C
227
228 this.firstPatchDone = true
229
bbe0f064
C
230 }
231 )
232 }
233
234 private trackChannelChange () {
74af5145 235 // We will update the "support" field depending on the channel
2186386c 236 this.form.controls[ 'channelId' ]
74af5145
C
237 .valueChanges
238 .pipe(map(res => parseInt(res.toString(), 10)))
239 .subscribe(
240 newChannelId => {
2186386c
C
241 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
242 const currentSupport = this.form.value[ 'support' ]
74af5145
C
243
244 // Not initialized yet
245 if (isNaN(newChannelId)) return
246 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
d18d6478 247 if (!newChannel) return
74af5145
C
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 )
ff249f49
C
266 }
267
74af5145
C
268 private updateSupportField (support: string) {
269 return this.form.patchValue({ support: support || '' })
270 }
ff249f49 271}