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 { VideoConstant, VideoPrivacy } from '../../../../../../shared'
18 selector: 'my-video-edit',
19 styleUrls: [ './video-edit.component.scss' ],
20 templateUrl: './video-edit.component.html'
22 export class VideoEditComponent implements OnInit, OnDestroy {
23 @Input() form: FormGroup
24 @Input() formErrors: { [ id: string ]: string } = {}
25 @Input() validationMessages: FormReactiveValidationMessages = {}
26 @Input() videoPrivacies: VideoConstant<VideoPrivacy>[] = []
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') videoCaptionAddModal: VideoCaptionAddModalComponent
34 // So that it can be accessed in the template
35 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
37 videoCategories: VideoConstant<number>[] = []
38 videoLicences: VideoConstant<number>[] = []
39 videoLanguages: VideoConstant<string>[] = []
41 tagValidators: ValidatorFn[]
42 tagValidatorsMessages: { [ name: string ]: string }
44 schedulePublicationEnabled = false
46 calendarLocale: any = {}
47 minScheduledDate = new Date()
48 myYearRange = '1880:' + (new Date()).getFullYear()
50 calendarTimezone: string
51 calendarDateFormat: string
53 private schedulerInterval: any
54 private firstPatchDone = false
55 private initialVideoCaptions: string[] = []
58 private formValidatorService: FormValidatorService,
59 private videoValidatorsService: VideoValidatorsService,
60 private videoCaptionService: VideoCaptionService,
61 private route: ActivatedRoute,
62 private router: Router,
63 private notifier: Notifier,
64 private serverService: ServerService,
65 private i18nPrimengCalendarService: I18nPrimengCalendarService,
66 private ngZone: NgZone
68 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
69 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
71 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
72 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
73 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
76 get existingCaptions () {
77 return this.videoCaptions
78 .filter(c => c.action !== 'REMOVE')
79 .map(c => c.language.id)
83 const defaultValues: any = {
85 commentsEnabled: 'true',
86 downloadEnabled: 'true',
87 waitTranscoding: 'true',
91 name: this.videoValidatorsService.VIDEO_NAME,
92 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
93 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
95 commentsEnabled: null,
96 downloadEnabled: null,
97 waitTranscoding: null,
98 category: this.videoValidatorsService.VIDEO_CATEGORY,
99 licence: this.videoValidatorsService.VIDEO_LICENCE,
100 language: this.videoValidatorsService.VIDEO_LANGUAGE,
101 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
104 support: this.videoValidatorsService.VIDEO_SUPPORT,
105 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT,
106 originallyPublishedAt: this.videoValidatorsService.VIDEO_ORIGINALLY_PUBLISHED_AT
109 this.formValidatorService.updateForm(
112 this.validationMessages,
117 this.form.addControl('captions', new FormArray([
119 language: new FormControl(),
120 captionfile: new FormControl()
124 this.trackChannelChange()
125 this.trackPrivacyChange()
131 this.videoCategories = this.serverService.getVideoCategories()
132 this.videoLicences = this.serverService.getVideoLicences()
133 this.videoLanguages = this.serverService.getVideoLanguages()
135 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
137 this.ngZone.runOutsideAngular(() => {
138 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
143 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
146 onCaptionAdded (caption: VideoCaptionEdit) {
147 const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id)
149 // Replace existing caption?
150 if (existingCaption) {
151 Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' })
153 this.videoCaptions.push(
154 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
158 this.sortVideoCaptions()
161 async deleteCaption (caption: VideoCaptionEdit) {
162 // Caption recovers his former state
163 if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) {
164 caption.action = undefined
168 // This caption is not on the server, just remove it from our array
169 if (caption.action === 'CREATE') {
170 removeElementFromArray(this.videoCaptions, caption)
174 caption.action = 'REMOVE' as 'REMOVE'
177 openAddCaptionModal () {
178 this.videoCaptionAddModal.show()
181 private sortVideoCaptions () {
182 this.videoCaptions.sort((v1, v2) => {
183 if (v1.language.label < v2.language.label) return -1
184 if (v1.language.label === v2.language.label) return 0
190 private trackPrivacyChange () {
191 // We will update the "support" field depending on the channel
192 this.form.controls[ 'privacy' ]
194 .pipe(map(res => parseInt(res.toString(), 10)))
198 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
201 const scheduleControl = this.form.get('schedulePublicationAt')
202 const waitTranscodingControl = this.form.get('waitTranscoding')
204 if (this.schedulePublicationEnabled) {
205 scheduleControl.setValidators([ Validators.required ])
207 waitTranscodingControl.disable()
208 waitTranscodingControl.setValue(false)
210 scheduleControl.clearValidators()
212 waitTranscodingControl.enable()
214 // Do not update the control value on first patch (values come from the server)
215 if (this.firstPatchDone === true) {
216 waitTranscodingControl.setValue(true)
220 scheduleControl.updateValueAndValidity()
221 waitTranscodingControl.updateValueAndValidity()
223 this.firstPatchDone = true
229 private trackChannelChange () {
230 // We will update the "support" field depending on the channel
231 this.form.controls[ 'channelId' ]
233 .pipe(map(res => parseInt(res.toString(), 10)))
236 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
237 const currentSupport = this.form.value[ 'support' ]
239 // Not initialized yet
240 if (isNaN(newChannelId)) return
241 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
242 if (!newChannel) return
244 // First time we set the channel?
245 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
246 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
248 if (!newChannel || !oldChannel) {
249 console.error('Cannot find new or old channel.')
253 // If the current support text is not the same than the old channel, the user updated it.
254 // We don't want the user to lose his text, so stop here
255 if (currentSupport && currentSupport !== oldChannel.support) return
257 // Update the support text with our new channel
258 this.updateSupportField(newChannel.support)
263 private updateSupportField (support: string) {
264 return this.form.patchValue({ support: support || '' })