1 import { Component, Input, OnInit } from '@angular/core'
2 import { FormGroup, ValidatorFn } 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'
12 selector: 'my-video-edit',
13 styleUrls: [ './video-edit.component.scss' ],
14 templateUrl: './video-edit.component.html'
17 export class VideoEditComponent implements OnInit {
18 @Input() form: FormGroup
19 @Input() formErrors: { [ id: string ]: string } = {}
20 @Input() validationMessages: FormReactiveValidationMessages = {}
21 @Input() videoPrivacies = []
22 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
29 tagValidators: ValidatorFn[]
30 tagValidatorsMessages: { [ name: string ]: string }
35 private formValidatorService: FormValidatorService,
36 private videoValidatorsService: VideoValidatorsService,
37 private route: ActivatedRoute,
38 private router: Router,
39 private notificationsService: NotificationsService,
40 private serverService: ServerService
42 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
43 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
47 const defaultValues = {
49 commentsEnabled: 'true',
50 waitTranscoding: 'true',
54 name: this.videoValidatorsService.VIDEO_NAME,
55 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
56 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
58 commentsEnabled: null,
59 waitTranscoding: null,
60 category: this.videoValidatorsService.VIDEO_CATEGORY,
61 licence: this.videoValidatorsService.VIDEO_LICENCE,
62 language: this.videoValidatorsService.VIDEO_LANGUAGE,
63 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
67 support: this.videoValidatorsService.VIDEO_SUPPORT
70 this.formValidatorService.updateForm(
73 this.validationMessages,
78 // We will update the "support" field depending on the channel
79 this.form.controls[ 'channelId' ]
81 .pipe(map(res => parseInt(res.toString(), 10)))
84 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
85 const currentSupport = this.form.value[ 'support' ]
87 // Not initialized yet
88 if (isNaN(newChannelId)) return
89 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
90 if (!newChannel) return
92 // First time we set the channel?
93 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
94 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
96 if (!newChannel || !oldChannel) {
97 console.error('Cannot find new or old channel.')
101 // If the current support text is not the same than the old channel, the user updated it.
102 // We don't want the user to lose his text, so stop here
103 if (currentSupport && currentSupport !== oldChannel.support) return
105 // Update the support text with our new channel
106 this.updateSupportField(newChannel.support)
114 this.videoCategories = this.serverService.getVideoCategories()
115 this.videoLicences = this.serverService.getVideoLicences()
116 this.videoLanguages = this.serverService.getVideoLanguages()
119 private updateSupportField (support: string) {
120 return this.form.patchValue({ support: support || '' })