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 { ServerConfig, VideoConstant, VideoPrivacy } from '../../../../../../shared'
16 import { VideoService } from '@app/shared/video/video.service'
19 selector: 'my-video-edit',
20 styleUrls: [ './video-edit.component.scss' ],
21 templateUrl: './video-edit.component.html'
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
32 @ViewChild('videoCaptionAddModal', { static: true }) videoCaptionAddModal: VideoCaptionAddModalComponent
34 // So that it can be accessed in the template
35 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
37 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
38 videoCategories: VideoConstant<number>[] = []
39 videoLicences: VideoConstant<number>[] = []
40 videoLanguages: VideoConstant<string>[] = []
42 tagValidators: ValidatorFn[]
43 tagValidatorsMessages: { [ name: string ]: string }
45 schedulePublicationEnabled = false
47 calendarLocale: any = {}
48 minScheduledDate = new Date()
49 myYearRange = '1880:' + (new Date()).getFullYear()
51 calendarTimezone: string
52 calendarDateFormat: string
54 serverConfig: ServerConfig
56 private schedulerInterval: any
57 private firstPatchDone = false
58 private initialVideoCaptions: string[] = []
61 private formValidatorService: FormValidatorService,
62 private videoValidatorsService: VideoValidatorsService,
63 private videoCaptionService: VideoCaptionService,
64 private videoService: VideoService,
65 private route: ActivatedRoute,
66 private router: Router,
67 private notifier: Notifier,
68 private serverService: ServerService,
69 private i18nPrimengCalendarService: I18nPrimengCalendarService,
70 private ngZone: NgZone
72 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
73 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
75 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
76 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
77 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
80 get existingCaptions () {
81 return this.videoCaptions
82 .filter(c => c.action !== 'REMOVE')
83 .map(c => c.language.id)
87 const defaultValues: any = {
89 commentsEnabled: 'true',
90 downloadEnabled: 'true',
91 waitTranscoding: 'true',
95 name: this.videoValidatorsService.VIDEO_NAME,
96 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
97 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
99 commentsEnabled: null,
100 downloadEnabled: null,
101 waitTranscoding: null,
102 category: this.videoValidatorsService.VIDEO_CATEGORY,
103 licence: this.videoValidatorsService.VIDEO_LICENCE,
104 language: this.videoValidatorsService.VIDEO_LANGUAGE,
105 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
108 support: this.videoValidatorsService.VIDEO_SUPPORT,
109 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT,
110 originallyPublishedAt: this.videoValidatorsService.VIDEO_ORIGINALLY_PUBLISHED_AT
113 this.formValidatorService.updateForm(
116 this.validationMessages,
121 this.form.addControl('captions', new FormArray([
123 language: new FormControl(),
124 captionfile: new FormControl()
128 this.trackChannelChange()
129 this.trackPrivacyChange()
135 this.serverService.getVideoCategories()
136 .subscribe(res => this.videoCategories = res)
137 this.serverService.getVideoLicences()
138 .subscribe(res => this.videoLicences = res)
139 this.serverService.getVideoLanguages()
140 .subscribe(res => this.videoLanguages = res)
142 this.serverService.getVideoPrivacies()
143 .subscribe(privacies => this.videoPrivacies = this.videoService.explainedPrivacyLabels(privacies))
145 this.serverConfig = this.serverService.getTmpConfig()
146 this.serverService.getConfig()
147 .subscribe(config => this.serverConfig = config)
149 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
151 this.ngZone.runOutsideAngular(() => {
152 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
157 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
160 onCaptionAdded (caption: VideoCaptionEdit) {
161 const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id)
163 // Replace existing caption?
164 if (existingCaption) {
165 Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' })
167 this.videoCaptions.push(
168 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
172 this.sortVideoCaptions()
175 async deleteCaption (caption: VideoCaptionEdit) {
176 // Caption recovers his former state
177 if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) {
178 caption.action = undefined
182 // This caption is not on the server, just remove it from our array
183 if (caption.action === 'CREATE') {
184 removeElementFromArray(this.videoCaptions, caption)
188 caption.action = 'REMOVE' as 'REMOVE'
191 openAddCaptionModal () {
192 this.videoCaptionAddModal.show()
195 private sortVideoCaptions () {
196 this.videoCaptions.sort((v1, v2) => {
197 if (v1.language.label < v2.language.label) return -1
198 if (v1.language.label === v2.language.label) return 0
204 private trackPrivacyChange () {
205 // We will update the schedule input and the wait transcoding checkbox validators
206 this.form.controls[ 'privacy' ]
208 .pipe(map(res => parseInt(res.toString(), 10)))
212 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
215 const scheduleControl = this.form.get('schedulePublicationAt')
216 const waitTranscodingControl = this.form.get('waitTranscoding')
218 if (this.schedulePublicationEnabled) {
219 scheduleControl.setValidators([ Validators.required ])
221 waitTranscodingControl.disable()
222 waitTranscodingControl.setValue(false)
224 scheduleControl.clearValidators()
226 waitTranscodingControl.enable()
228 // Do not update the control value on first patch (values come from the server)
229 if (this.firstPatchDone === true) {
230 waitTranscodingControl.setValue(true)
234 scheduleControl.updateValueAndValidity()
235 waitTranscodingControl.updateValueAndValidity()
237 this.firstPatchDone = true
243 private trackChannelChange () {
244 // We will update the "support" field depending on the channel
245 this.form.controls[ 'channelId' ]
247 .pipe(map(res => parseInt(res.toString(), 10)))
250 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
252 // Not initialized yet
253 if (isNaN(newChannelId)) return
254 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
255 if (!newChannel) return
257 // Wait support field update
259 const currentSupport = this.form.value[ 'support' ]
261 // First time we set the channel?
262 if (isNaN(oldChannelId) && !currentSupport) return this.updateSupportField(newChannel.support)
264 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
265 if (!newChannel || !oldChannel) {
266 console.error('Cannot find new or old channel.')
270 // If the current support text is not the same than the old channel, the user updated it.
271 // We don't want the user to lose his text, so stop here
272 if (currentSupport && currentSupport !== oldChannel.support) return
274 // Update the support text with our new channel
275 this.updateSupportField(newChannel.support)
281 private updateSupportField (support: string) {
282 return this.form.patchValue({ support: support || '' })