1 import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { FormArray, FormControl, FormGroup, ValidatorFn, Validators } 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 import { I18nPrimengCalendarService } from '@app/shared/i18n/i18n-primeng-calendar'
11 import { VideoCaptionService } from '@app/shared/video-caption'
12 import { VideoCaptionAddModalComponent } from '@app/videos/+video-edit/shared/video-caption-add-modal.component'
13 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
14 import { removeElementFromArray } from '@app/shared/misc/utils'
15 import { VideoConstant, VideoPrivacy } from '../../../../../../shared'
18 selector: 'my-video-edit',
19 styleUrls: [ './video-edit.component.scss' ],
20 templateUrl: './video-edit.component.html'
22 export class VideoEditComponent implements OnInit, OnDestroy {
23 @Input() form: FormGroup
24 @Input() formErrors: { [ id: string ]: string } = {}
25 @Input() validationMessages: FormReactiveValidationMessages = {}
26 @Input() videoPrivacies: VideoConstant<VideoPrivacy>[] = []
27 @Input() userVideoChannels: { id: number, label: string, support: string }[] = []
28 @Input() schedulePublicationPossible = true
29 @Input() videoCaptions: VideoCaptionEdit[] = []
31 @ViewChild('videoCaptionAddModal') videoCaptionAddModal: VideoCaptionAddModalComponent
33 // So that it can be accessed in the template
34 readonly SPECIAL_SCHEDULED_PRIVACY = VideoEdit.SPECIAL_SCHEDULED_PRIVACY
36 videoCategories: VideoConstant<number>[] = []
37 videoLicences: VideoConstant<number>[] = []
38 videoLanguages: VideoConstant<string>[] = []
40 tagValidators: ValidatorFn[]
41 tagValidatorsMessages: { [ name: string ]: string }
43 schedulePublicationEnabled = false
45 calendarLocale: any = {}
46 minScheduledDate = new Date()
48 calendarTimezone: string
49 calendarDateFormat: string
51 private schedulerInterval
52 private firstPatchDone = false
53 private initialVideoCaptions: string[] = []
56 private formValidatorService: FormValidatorService,
57 private videoValidatorsService: VideoValidatorsService,
58 private videoCaptionService: VideoCaptionService,
59 private route: ActivatedRoute,
60 private router: Router,
61 private notificationsService: NotificationsService,
62 private serverService: ServerService,
63 private i18nPrimengCalendarService: I18nPrimengCalendarService
65 this.tagValidators = this.videoValidatorsService.VIDEO_TAGS.VALIDATORS
66 this.tagValidatorsMessages = this.videoValidatorsService.VIDEO_TAGS.MESSAGES
68 this.calendarLocale = this.i18nPrimengCalendarService.getCalendarLocale()
69 this.calendarTimezone = this.i18nPrimengCalendarService.getTimezone()
70 this.calendarDateFormat = this.i18nPrimengCalendarService.getDateFormat()
73 get existingCaptions () {
74 return this.videoCaptions
75 .filter(c => c.action !== 'REMOVE')
76 .map(c => c.language.id)
80 const defaultValues = {
82 commentsEnabled: 'true',
83 downloadEnabled: 'true',
84 waitTranscoding: 'true',
88 name: this.videoValidatorsService.VIDEO_NAME,
89 privacy: this.videoValidatorsService.VIDEO_PRIVACY,
90 channelId: this.videoValidatorsService.VIDEO_CHANNEL,
92 commentsEnabled: null,
93 downloadEnabled: null,
94 waitTranscoding: null,
95 category: this.videoValidatorsService.VIDEO_CATEGORY,
96 licence: this.videoValidatorsService.VIDEO_LICENCE,
97 language: this.videoValidatorsService.VIDEO_LANGUAGE,
98 description: this.videoValidatorsService.VIDEO_DESCRIPTION,
102 support: this.videoValidatorsService.VIDEO_SUPPORT,
103 schedulePublicationAt: this.videoValidatorsService.VIDEO_SCHEDULE_PUBLICATION_AT
106 this.formValidatorService.updateForm(
109 this.validationMessages,
114 this.form.addControl('captions', new FormArray([
116 language: new FormControl(),
117 captionfile: new FormControl()
121 this.trackChannelChange()
122 this.trackPrivacyChange()
128 this.videoCategories = this.serverService.getVideoCategories()
129 this.videoLicences = this.serverService.getVideoLicences()
130 this.videoLanguages = this.serverService.getVideoLanguages()
132 this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute
134 this.initialVideoCaptions = this.videoCaptions.map(c => c.language.id)
138 if (this.schedulerInterval) clearInterval(this.schedulerInterval)
141 onCaptionAdded (caption: VideoCaptionEdit) {
142 const existingCaption = this.videoCaptions.find(c => c.language.id === caption.language.id)
144 // Replace existing caption?
145 if (existingCaption) {
146 Object.assign(existingCaption, caption, { action: 'CREATE' as 'CREATE' })
148 this.videoCaptions.push(
149 Object.assign(caption, { action: 'CREATE' as 'CREATE' })
153 this.sortVideoCaptions()
156 async deleteCaption (caption: VideoCaptionEdit) {
157 // Caption recovers his former state
158 if (caption.action && this.initialVideoCaptions.indexOf(caption.language.id) !== -1) {
159 caption.action = undefined
163 // This caption is not on the server, just remove it from our array
164 if (caption.action === 'CREATE') {
165 removeElementFromArray(this.videoCaptions, caption)
169 caption.action = 'REMOVE' as 'REMOVE'
172 openAddCaptionModal () {
173 this.videoCaptionAddModal.show()
176 private sortVideoCaptions () {
177 this.videoCaptions.sort((v1, v2) => {
178 if (v1.language.label < v2.language.label) return -1
179 if (v1.language.label === v2.language.label) return 0
185 private trackPrivacyChange () {
186 // We will update the "support" field depending on the channel
187 this.form.controls[ 'privacy' ]
189 .pipe(map(res => parseInt(res.toString(), 10)))
193 this.schedulePublicationEnabled = newPrivacyId === this.SPECIAL_SCHEDULED_PRIVACY
196 const scheduleControl = this.form.get('schedulePublicationAt')
197 const waitTranscodingControl = this.form.get('waitTranscoding')
199 if (this.schedulePublicationEnabled) {
200 scheduleControl.setValidators([ Validators.required ])
202 waitTranscodingControl.disable()
203 waitTranscodingControl.setValue(false)
205 scheduleControl.clearValidators()
207 waitTranscodingControl.enable()
209 // Do not update the control value on first patch (values come from the server)
210 if (this.firstPatchDone === true) {
211 waitTranscodingControl.setValue(true)
215 scheduleControl.updateValueAndValidity()
216 waitTranscodingControl.updateValueAndValidity()
218 this.firstPatchDone = true
224 private trackChannelChange () {
225 // We will update the "support" field depending on the channel
226 this.form.controls[ 'channelId' ]
228 .pipe(map(res => parseInt(res.toString(), 10)))
231 const oldChannelId = parseInt(this.form.value[ 'channelId' ], 10)
232 const currentSupport = this.form.value[ 'support' ]
234 // Not initialized yet
235 if (isNaN(newChannelId)) return
236 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
237 if (!newChannel) return
239 // First time we set the channel?
240 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
241 const oldChannel = this.userVideoChannels.find(c => c.id === oldChannelId)
243 if (!newChannel || !oldChannel) {
244 console.error('Cannot find new or old channel.')
248 // If the current support text is not the same than the old channel, the user updated it.
249 // We don't want the user to lose his text, so stop here
250 if (currentSupport && currentSupport !== oldChannel.support) return
252 // Update the support text with our new channel
253 this.updateSupportField(newChannel.support)
258 private updateSupportField (support: string) {
259 return this.form.patchValue({ support: support || '' })