]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/shared/video-edit.component.ts
61515c0b0cb4069b958e0294814c6cd1947b75fa
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / shared / video-edit.component.ts
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'
10
11 @Component({
12 selector: 'my-video-edit',
13 styleUrls: [ './video-edit.component.scss' ],
14 templateUrl: './video-edit.component.html'
15 })
16
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 }[] = []
23
24 videoCategories = []
25 videoLicences = []
26 videoLanguages = []
27 video: VideoEdit
28
29 tagValidators: ValidatorFn[]
30 tagValidatorsMessages: { [ name: string ]: string }
31
32 error: string = null
33
34 constructor (
35 private formValidatorService: FormValidatorService,
36 private videoValidatorsService: VideoValidatorsService,
37 private route: ActivatedRoute,
38 private router: Router,
39 private notificationsService: NotificationsService,
40 private serverService: ServerService
41 ) {
42 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
43 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
44 }
45
46 updateForm () {
47 const defaultValues = {
48 nsfw: 'false',
49 commentsEnabled: 'true',
50 tags: []
51 }
52 const obj = {
53 name: this.videoValidatorsService.VIDEO_NAME,
54 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
55 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
56 nsfw: null,
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,
62 tags: null,
63 thumbnailfile: null,
64 previewfile: null,
65 support: this.videoValidatorsService.VIDEO_SUPPORT
66 }
67
68 this.formValidatorService.updateForm(
69 this.form,
70 this.formErrors,
71 this.validationMessages,
72 obj,
73 defaultValues
74 )
75
76 // We will update the "support" field depending on the channel
77 this.form.controls['channelId']
78 .valueChanges
79 .pipe(map(res => parseInt(res.toString(), 10)))
80 .subscribe(
81 newChannelId => {
82 const oldChannelId = parseInt(this.form.value['channelId'], 10)
83 const currentSupport = this.form.value['support']
84
85 // Not initialized yet
86 if (isNaN(newChannelId)) return
87 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
88 if (!newChannel) return
89
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)
93
94 if (!newChannel || !oldChannel) {
95 console.error('Cannot find new or old channel.')
96 return
97 }
98
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
102
103 // Update the support text with our new channel
104 this.updateSupportField(newChannel.support)
105 }
106 )
107 }
108
109 ngOnInit () {
110 this.updateForm()
111
112 this.videoCategories = this.serverService.getVideoCategories()
113 this.videoLicences = this.serverService.getVideoLicences()
114 this.videoLanguages = this.serverService.getVideoLanguages()
115 }
116
117 private updateSupportField (support: string) {
118 return this.form.patchValue({ support: support || '' })
119 }
120 }