1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { VIDEO_CAPTION_FILE_CONTENT_VALIDATOR } from '@app/shared/form-validators/video-captions-validators'
3 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4 import { VideoCaptionEdit, VideoCaptionService, VideoCaptionWithPathEdit } from '@app/shared/shared-main'
5 import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
6 import { HTMLServerConfig, VideoConstant } from '@shared/models'
7 import { ServerService } from '../../../../core'
10 * https://github.com/valor-software/ngx-bootstrap/issues/3825
11 * https://stackblitz.com/edit/angular-t5dfp7
12 * https://medium.com/@izzatnadiri/how-to-pass-data-to-and-receive-from-ng-bootstrap-modals-916f2ad5d66e
15 selector: 'my-video-caption-edit-modal-content',
16 styleUrls: [ './video-caption-edit-modal-content.component.scss' ],
17 templateUrl: './video-caption-edit-modal-content.component.html'
20 export class VideoCaptionEditModalContentComponent extends FormReactive implements OnInit {
21 @Input() videoCaption: VideoCaptionWithPathEdit
22 @Input() serverConfig: HTMLServerConfig
24 @Output() captionEdited = new EventEmitter<VideoCaptionEdit>()
26 @ViewChild('textarea', { static: true }) textarea!: ElementRef
28 videoCaptionLanguages: VideoConstant<string>[] = []
31 protected openedModal: NgbActiveModal,
32 protected formValidatorService: FormValidatorService,
33 private modalService: NgbModal,
34 private videoCaptionService: VideoCaptionService,
35 private serverService: ServerService
41 this.serverService.getVideoLanguages().subscribe(languages => this.videoCaptionLanguages = languages)
43 this.buildForm({ captionFileContent: VIDEO_CAPTION_FILE_CONTENT_VALIDATOR })
45 this.loadCaptionContent()
48 loadCaptionContent () {
49 const { captionPath } = this.videoCaption
50 if (!captionPath) return
52 this.videoCaptionService.getCaptionContent({ captionPath })
54 this.form.patchValue({
55 captionFileContent: res
62 this.textarea.nativeElement.scrollTop = 0
63 this.textarea.nativeElement.selectionStart = 0
64 this.textarea.nativeElement.selectionEnd = 0
68 this.openedModal.close()
77 const languageId = this.videoCaption.language.id
78 const languageObject = this.videoCaptionLanguages.find(l => l.id === languageId)
80 this.captionEdited.emit({
81 language: languageObject,
82 captionfile: new File([ this.form.value['captionFileContent'] ], `${languageId}.${format}`, {
84 lastModified: Date.now()