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',
53 name: this.videoValidatorsService.VIDEO_NAME,
54 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
55 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
57 commentsEnabled: null,
58 category: this.videoValidatorsService.VIDEO_CATEGORY,
59 licence: this.videoValidatorsService.VIDEO_LICENCE,
60 language: this.videoValidatorsService.VIDEO_LANGUAGE,
61 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
65 support: this.videoValidatorsService.VIDEO_SUPPORT
68 this.formValidatorService.updateForm(
71 this.validationMessages,
76 // We will update the "support" field depending on the channel
77 this.form.controls['channelId']
79 .pipe(map(res => parseInt(res.toString(), 10)))
82 const oldChannelId = parseInt(this.form.value['channelId'], 10)
83 const currentSupport = this.form.value['support']
85 // Not initialized yet
86 if (isNaN(newChannelId)) return
87 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
88 if (!newChannel) return
90 // First time we set the channel?
91 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
92 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
94 if (!newChannel || !oldChannel) {
95 console.error('Cannot find new or old channel.')
99 // If the current support text is not the same than the old channel, the user updated it.
100 // We don't want the user to lose his text, so stop here
101 if (currentSupport && currentSupport !== oldChannel.support) return
103 // Update the support text with our new channel
104 this.updateSupportField(newChannel.support)
112 this.videoCategories = this.serverService.getVideoCategories()
113 this.videoLicences = this.serverService.getVideoLicences()
114 this.videoLanguages = this.serverService.getVideoLanguages()
117 private updateSupportField (support: string) {
118 return this.form.patchValue({ support: support || '' })