1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { ServerService } from '@app/core'
3 import { VIDEO_CAPTION_FILE_VALIDATOR, VIDEO_CAPTION_LANGUAGE_VALIDATOR } from '@app/shared/form-validators/video-captions-validators'
4 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
5 import { VideoCaptionEdit } from '@app/shared/shared-main'
6 import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
7 import { HTMLServerConfig, VideoConstant } from '@shared/models'
10 selector: 'my-video-caption-add-modal',
11 styleUrls: [ './video-caption-add-modal.component.scss' ],
12 templateUrl: './video-caption-add-modal.component.html'
15 export class VideoCaptionAddModalComponent extends FormReactive implements OnInit {
16 @Input() existingCaptions: string[]
17 @Input() serverConfig: HTMLServerConfig
19 @Output() captionAdded = new EventEmitter<VideoCaptionEdit>()
21 @ViewChild('modal', { static: true }) modal: ElementRef
23 videoCaptionLanguages: VideoConstant<string>[] = []
25 private openedModal: NgbModalRef
26 private closingModal = false
29 protected formValidatorService: FormValidatorService,
30 private modalService: NgbModal,
31 private serverService: ServerService
36 get videoCaptionExtensions () {
37 return this.serverConfig.videoCaption.file.extensions
40 get videoCaptionMaxSize () {
41 return this.serverConfig.videoCaption.file.size.max
45 this.serverService.getVideoLanguages()
46 .subscribe(languages => this.videoCaptionLanguages = languages)
49 language: VIDEO_CAPTION_LANGUAGE_VALIDATOR,
50 captionfile: VIDEO_CAPTION_FILE_VALIDATOR
55 this.closingModal = false
57 this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })
61 this.closingModal = true
62 this.openedModal.close()
66 isReplacingExistingCaption () {
67 if (this.closingModal === true) return false
69 const languageId = this.form.value['language']
71 return languageId && this.existingCaptions.includes(languageId)
75 const languageId = this.form.value['language']
76 const languageObject = this.videoCaptionLanguages.find(l => l.id === languageId)
78 this.captionAdded.emit({
79 language: languageObject,
80 captionfile: this.form.value['captionfile']