]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/shared/video-edit.component.ts
Update iso639 translations for french and deutch
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / shared / video-edit.component.ts
CommitLineData
ff249f49 1import { Component, Input, OnInit } from '@angular/core'
bbe0f064 2import { FormGroup, ValidatorFn, Validators } from '@angular/forms'
ff249f49 3import { ActivatedRoute, Router } from '@angular/router'
e309822b 4import { FormReactiveValidationMessages, VideoValidatorsService } from '@app/shared'
ff249f49 5import { NotificationsService } from 'angular2-notifications'
63c4db6d 6import { ServerService } from '../../../core/server'
63c4db6d 7import { VideoEdit } from '../../../shared/video/video-edit.model'
74af5145 8import { map } from 'rxjs/operators'
d18d6478 9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
bbe0f064 10import { I18nPrimengCalendarService } from '@app/shared/i18n/i18n-primeng-calendar'
ff249f49
C
11
12@Component({
13 selector: 'my-video-edit',
14 styleUrls: [ './video-edit.component.scss' ],
15 templateUrl: './video-edit.component.html'
16})
17
18export class VideoEditComponent implements OnInit {
19 @Input() form: FormGroup
20 @Input() formErrors: { [ id: string ]: string } = {}
e309822b 21 @Input() validationMessages: FormReactiveValidationMessages = {}
ff249f49 22 @Input() videoPrivacies = []
74af5145 23 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
bbe0f064
C
24 @Input() schedulePublicationPossible = true
25
26 // So that it can be accessed in the template
27 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
ff249f49 28
ff249f49
C
29 videoCategories = []
30 videoLicences = []
31 videoLanguages = []
ff249f49 32
e309822b
C
33 tagValidators: ValidatorFn[]
34 tagValidatorsMessages: { [ name: string ]: string }
ff249f49 35
bbe0f064
C
36 schedulePublicationEnabled = false
37
bbe0f064
C
38 calendarLocale: any = {}
39 minScheduledDate = new Date()
40
41 calendarTimezone: string
42 calendarDateFormat: string
ff249f49
C
43
44 constructor (
d18d6478 45 private formValidatorService: FormValidatorService,
e309822b 46 private videoValidatorsService: VideoValidatorsService,
ff249f49
C
47 private route: ActivatedRoute,
48 private router: Router,
49 private notificationsService: NotificationsService,
bbe0f064
C
50 private serverService: ServerService,
51 private i18nPrimengCalendarService: I18nPrimengCalendarService
e309822b
C
52 ) {
53 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
54 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
bbe0f064
C
55
56 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
57 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
58 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
e309822b 59 }
ff249f49
C
60
61 updateForm () {
d18d6478
C
62 const defaultValues = {
63 nsfw: 'false',
64 commentsEnabled: 'true',
2186386c 65 waitTranscoding: 'true',
d18d6478
C
66 tags: []
67 }
68 const obj = {
e309822b
C
69 name: this.videoValidatorsService.VIDEO_NAME,
70 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
71 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
d18d6478
C
72 nsfw: null,
73 commentsEnabled: null,
2186386c 74 waitTranscoding: null,
e309822b
C
75 category: this.videoValidatorsService.VIDEO_CATEGORY,
76 licence: this.videoValidatorsService.VIDEO_LICENCE,
77 language: this.videoValidatorsService.VIDEO_LANGUAGE,
78 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
d18d6478
C
79 tags: null,
80 thumbnailfile: null,
81 previewfile: null,
bbe0f064
C
82 support: this.videoValidatorsService.VIDEO_SUPPORT,
83 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT
d18d6478 84 }
ff249f49 85
d18d6478
C
86 this.formValidatorService.updateForm(
87 this.form,
88 this.formErrors,
89 this.validationMessages,
90 obj,
91 defaultValues
92 )
74af5145 93
bbe0f064
C
94 this.trackChannelChange()
95 this.trackPrivacyChange()
96 }
97
98 ngOnInit () {
99 this.updateForm()
100
101 this.videoCategories = this.serverService.getVideoCategories()
102 this.videoLicences = this.serverService.getVideoLicences()
103 this.videoLanguages = this.serverService.getVideoLanguages()
9c2e0dbf 104 console.log(this.videoLanguages)
bbe0f064
C
105
106 setTimeout(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
107 }
108
109 private trackPrivacyChange () {
110 // We will update the "support" field depending on the channel
111 this.form.controls[ 'privacy' ]
112 .valueChanges
113 .pipe(map(res => parseInt(res.toString(), 10)))
114 .subscribe(
115 newPrivacyId => {
116 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
117
118 // Value changed
119 const scheduleControl = this.form.get('schedulePublicationAt')
120 const waitTranscodingControl = this.form.get('waitTranscoding')
121
122 if (this.schedulePublicationEnabled) {
123 scheduleControl.setValidators([ Validators.required ])
124
125 waitTranscodingControl.disable()
126 waitTranscodingControl.setValue(false)
127 } else {
128 scheduleControl.clearValidators()
129
130 waitTranscodingControl.enable()
131 waitTranscodingControl.setValue(true)
132 }
133
134 scheduleControl.updateValueAndValidity()
135 waitTranscodingControl.updateValueAndValidity()
136 }
137 )
138 }
139
140 private trackChannelChange () {
74af5145 141 // We will update the "support" field depending on the channel
2186386c 142 this.form.controls[ 'channelId' ]
74af5145
C
143 .valueChanges
144 .pipe(map(res => parseInt(res.toString(), 10)))
145 .subscribe(
146 newChannelId => {
2186386c
C
147 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
148 const currentSupport = this.form.value[ 'support' ]
74af5145
C
149
150 // Not initialized yet
151 if (isNaN(newChannelId)) return
152 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
d18d6478 153 if (!newChannel) return
74af5145
C
154
155 // First time we set the channel?
156 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
157 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
158
159 if (!newChannel || !oldChannel) {
160 console.error('Cannot find new or old channel.')
161 return
162 }
163
164 // If the current support text is not the same than the old channel, the user updated it.
165 // We don't want the user to lose his text, so stop here
166 if (currentSupport && currentSupport !== oldChannel.support) return
167
168 // Update the support text with our new channel
169 this.updateSupportField(newChannel.support)
170 }
171 )
ff249f49
C
172 }
173
74af5145
C
174 private updateSupportField (support: string) {
175 return this.form.patchValue({ support: support || '' })
176 }
ff249f49 177}