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