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