]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/shared/video-edit.component.ts
Add concept of video state, and add ability to wait transcoding before
[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 waitTranscoding: 'true',
51 tags: []
52 }
53 const obj = {
54 name: this.videoValidatorsService.VIDEO_NAME,
55 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
56 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
57 nsfw: null,
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,
64 tags: null,
65 thumbnailfile: null,
66 previewfile: null,
67 support: this.videoValidatorsService.VIDEO_SUPPORT
68 }
69
70 this.formValidatorService.updateForm(
71 this.form,
72 this.formErrors,
73 this.validationMessages,
74 obj,
75 defaultValues
76 )
77
78 // We will update the "support" field depending on the channel
79 this.form.controls[ 'channelId' ]
80 .valueChanges
81 .pipe(map(res => parseInt(res.toString(), 10)))
82 .subscribe(
83 newChannelId => {
84 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
85 const currentSupport = this.form.value[ 'support' ]
86
87 // Not initialized yet
88 if (isNaN(newChannelId)) return
89 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
90 if (!newChannel) return
91
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)
95
96 if (!newChannel || !oldChannel) {
97 console.error('Cannot find new or old channel.')
98 return
99 }
100
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
104
105 // Update the support text with our new channel
106 this.updateSupportField(newChannel.support)
107 }
108 )
109 }
110
111 ngOnInit () {
112 this.updateForm()
113
114 this.videoCategories = this.serverService.getVideoCategories()
115 this.videoLicences = this.serverService.getVideoLicences()
116 this.videoLanguages = this.serverService.getVideoLanguages()
117 }
118
119 private updateSupportField (support: string) {
120 return this.form.patchValue({ support: support || '' })
121 }
122 }