1 import { Component, Input, OnInit } from '@angular/core'
2 import { 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'
13 selector: 'my-video-edit',
14 styleUrls: [ './video-edit.component.scss' ],
15 templateUrl: './video-edit.component.html'
18 export class VideoEditComponent implements OnInit {
19 @Input() form: FormGroup
20 @Input() formErrors: { [ id: string ]: string } = {}
21 @Input() validationMessages: FormReactiveValidationMessages = {}
22 @Input() videoPrivacies = []
23 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
24 @Input() schedulePublicationPossible = true
26 // So that it can be accessed in the template
27 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
33 tagValidators: ValidatorFn[]
34 tagValidatorsMessages: { [ name: string ]: string }
36 schedulePublicationEnabled = false
38 calendarLocale: any = {}
39 minScheduledDate = new Date()
41 calendarTimezone: string
42 calendarDateFormat: string
45 private formValidatorService: FormValidatorService,
46 private videoValidatorsService: VideoValidatorsService,
47 private route: ActivatedRoute,
48 private router: Router,
49 private notificationsService: NotificationsService,
50 private serverService: ServerService,
51 private i18nPrimengCalendarService: I18nPrimengCalendarService
53 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
54 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
56 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
57 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
58 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
62 const defaultValues = {
64 commentsEnabled: 'true',
65 waitTranscoding: 'true',
69 name: this.videoValidatorsService.VIDEO_NAME,
70 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
71 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
73 commentsEnabled: null,
74 waitTranscoding: null,
75 category: this.videoValidatorsService.VIDEO_CATEGORY,
76 licence: this.videoValidatorsService.VIDEO_LICENCE,
77 language: this.videoValidatorsService.VIDEO_LANGUAGE,
78 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
82 support: this.videoValidatorsService.VIDEO_SUPPORT,
83 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT
86 this.formValidatorService.updateForm(
89 this.validationMessages,
94 this.trackChannelChange()
95 this.trackPrivacyChange()
101 this.videoCategories = this.serverService.getVideoCategories()
102 this.videoLicences = this.serverService.getVideoLicences()
103 this.videoLanguages = this.serverService.getVideoLanguages()
104 console.log(this.videoLanguages)
106 setTimeout(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
109 private trackPrivacyChange () {
110 // We will update the "support" field depending on the channel
111 this.form.controls[ 'privacy' ]
113 .pipe(map(res => parseInt(res.toString(), 10)))
116 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
119 const scheduleControl = this.form.get('schedulePublicationAt')
120 const waitTranscodingControl = this.form.get('waitTranscoding')
122 if (this.schedulePublicationEnabled) {
123 scheduleControl.setValidators([ Validators.required ])
125 waitTranscodingControl.disable()
126 waitTranscodingControl.setValue(false)
128 scheduleControl.clearValidators()
130 waitTranscodingControl.enable()
131 waitTranscodingControl.setValue(true)
134 scheduleControl.updateValueAndValidity()
135 waitTranscodingControl.updateValueAndValidity()
140 private trackChannelChange () {
141 // We will update the "support" field depending on the channel
142 this.form.controls[ 'channelId' ]
144 .pipe(map(res => parseInt(res.toString(), 10)))
147 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
148 const currentSupport = this.form.value[ 'support' ]
150 // Not initialized yet
151 if (isNaN(newChannelId)) return
152 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
153 if (!newChannel) return
155 // First time we set the channel?
156 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
157 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
159 if (!newChannel || !oldChannel) {
160 console.error('Cannot find new or old channel.')
164 // If the current support text is not the same than the old channel, the user updated it.
165 // We don't want the user to lose his text, so stop here
166 if (currentSupport && currentSupport !== oldChannel.support) return
168 // Update the support text with our new channel
169 this.updateSupportField(newChannel.support)
174 private updateSupportField (support: string) {
175 return this.form.patchValue({ support: support || '' })