1a9bf5171cab9a23381bb3edf254f5e33ac08a0f
[oweals/peertube.git] / client / src / app / videos / +video-edit / shared / video-caption-add-modal.component.ts
1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { FormReactive } from '@app/shared'
3 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
4 import { VideoCaptionsValidatorsService } from '@app/shared/forms/form-validators/video-captions-validators.service'
5 import { ServerService } from '@app/core'
6 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
7 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
8 import { ServerConfig, VideoConstant } from '../../../../../../shared'
9
10 @Component({
11   selector: 'my-video-caption-add-modal',
12   styleUrls: [ './video-caption-add-modal.component.scss' ],
13   templateUrl: './video-caption-add-modal.component.html'
14 })
15
16 export class VideoCaptionAddModalComponent extends FormReactive implements OnInit {
17   @Input() existingCaptions: string[]
18   @Input() serverConfig: ServerConfig
19
20   @Output() captionAdded = new EventEmitter<VideoCaptionEdit>()
21
22   @ViewChild('modal', { static: true }) modal: ElementRef
23
24   videoCaptionLanguages: VideoConstant<string>[] = []
25
26   private openedModal: NgbModalRef
27   private closingModal = false
28
29   constructor (
30     protected formValidatorService: FormValidatorService,
31     private modalService: NgbModal,
32     private serverService: ServerService,
33     private videoCaptionsValidatorsService: VideoCaptionsValidatorsService
34   ) {
35     super()
36   }
37
38   get videoCaptionExtensions () {
39     return this.serverConfig.videoCaption.file.extensions
40   }
41
42   get videoCaptionMaxSize () {
43     return this.serverConfig.videoCaption.file.size.max
44   }
45
46   ngOnInit () {
47     this.serverService.getVideoLanguages()
48         .subscribe(languages => this.videoCaptionLanguages = languages)
49
50     this.buildForm({
51       language: this.videoCaptionsValidatorsService.VIDEO_CAPTION_LANGUAGE,
52       captionfile: this.videoCaptionsValidatorsService.VIDEO_CAPTION_FILE
53     })
54   }
55
56   show () {
57     this.closingModal = false
58
59     this.openedModal = this.modalService.open(this.modal, { keyboard: false })
60   }
61
62   hide () {
63     this.closingModal = true
64     this.openedModal.close()
65     this.form.reset()
66   }
67
68   isReplacingExistingCaption () {
69     if (this.closingModal === true) return false
70
71     const languageId = this.form.value[ 'language' ]
72
73     return languageId && this.existingCaptions.indexOf(languageId) !== -1
74   }
75
76   async addCaption () {
77     const languageId = this.form.value[ 'language' ]
78     const languageObject = this.videoCaptionLanguages.find(l => l.id === languageId)
79
80     this.captionAdded.emit({
81       language: languageObject,
82       captionfile: this.form.value[ 'captionfile' ]
83     })
84
85     this.hide()
86   }
87 }