aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/+video-edit/shared/video-caption-add-modal.component.ts
blob: 07c33030aa7087fa904a88601a2b8b5f54ea4a6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
import { FormReactive } from '@app/shared'
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
import { VideoCaptionsValidatorsService } from '@app/shared/forms/form-validators/video-captions-validators.service'
import { ServerService } from '@app/core'
import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'

@Component({
  selector: 'my-video-caption-add-modal',
  styleUrls: [ './video-caption-add-modal.component.scss' ],
  templateUrl: './video-caption-add-modal.component.html'
})

export class VideoCaptionAddModalComponent extends FormReactive implements OnInit {
  @Input() existingCaptions: string[]

  @Output() captionAdded = new EventEmitter<VideoCaptionEdit>()

  @ViewChild('modal') modal: ElementRef

  videoCaptionLanguages = []

  private openedModal: NgbModalRef
  private closingModal = false

  constructor (
    protected formValidatorService: FormValidatorService,
    private modalService: NgbModal,
    private serverService: ServerService,
    private videoCaptionsValidatorsService: VideoCaptionsValidatorsService
  ) {
    super()
  }

  get videoCaptionExtensions () {
    return this.serverService.getConfig().videoCaption.file.extensions
  }

  get videoCaptionMaxSize () {
    return this.serverService.getConfig().videoCaption.file.size.max
  }

  ngOnInit () {
    this.videoCaptionLanguages = this.serverService.getVideoLanguages()

    this.buildForm({
      language: this.videoCaptionsValidatorsService.VIDEO_CAPTION_LANGUAGE,
      captionfile: this.videoCaptionsValidatorsService.VIDEO_CAPTION_FILE
    })
  }

  show () {
    this.closingModal = false

    this.openedModal = this.modalService.open(this.modal, { keyboard: false })
  }

  hide () {
    this.closingModal = true
    this.openedModal.close()
  }

  isReplacingExistingCaption () {
    if (this.closingModal === true) return false

    const languageId = this.form.value[ 'language' ]

    return languageId && this.existingCaptions.indexOf(languageId) !== -1
  }

  async addCaption () {
    this.hide()

    const languageId = this.form.value[ 'language' ]
    const languageObject = this.videoCaptionLanguages.find(l => l.id === languageId)

    this.captionAdded.emit({
      language: languageObject,
      captionfile: this.form.value[ 'captionfile' ]
    })

    this.form.reset()
  }
}