1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, HostListener, OnInit } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { LoadingBarService } from '@ngx-loading-bar/core'
5 import { Notifier } from '@app/core'
6 import { VideoConstant, VideoPrivacy } from '../../../../../shared/models/videos'
7 import { ServerService } from '../../core'
8 import { FormReactive } from '../../shared'
9 import { VideoEdit } from '../../shared/video/video-edit.model'
10 import { VideoService } from '../../shared/video/video.service'
11 import { I18n } from '@ngx-translate/i18n-polyfill'
12 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
15 import { VideoDetails } from '@app/shared/video/video-details.model'
18 selector: 'my-videos-update',
19 styleUrls: [ './shared/video-edit.component.scss' ],
20 templateUrl: './video-update.component.html'
22 export class VideoUpdateComponent extends FormReactive implements OnInit {
25 isUpdatingVideo = false
26 videoPrivacies: VideoConstant<VideoPrivacy>[] = []
27 explainedVideoPrivacies: VideoConstant<VideoPrivacy>[] = []
28 userVideoChannels: { id: number, label: string, support: string }[] = []
29 schedulePublicationPossible = false
30 videoCaptions: VideoCaptionEdit[] = []
31 waitTranscodingEnabled = true
33 private updateDone = false
36 protected formValidatorService: FormValidatorService,
37 private route: ActivatedRoute,
38 private router: Router,
39 private notifier: Notifier,
40 private serverService: ServerService,
41 private videoService: VideoService,
42 private loadingBar: LoadingBarService,
43 private videoCaptionService: VideoCaptionService,
52 this.serverService.videoPrivaciesLoaded
53 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
56 .pipe(map(data => data.videoData))
57 .subscribe(({ video, videoChannels, videoCaptions }) => {
58 this.video = new VideoEdit(video)
59 this.userVideoChannels = videoChannels
60 this.videoCaptions = videoCaptions
62 // We cannot set private a video that was not private
63 if (this.video.privacy !== VideoPrivacy.PRIVATE) {
64 this.videoPrivacies = this.videoPrivacies.filter(p => p.id !== VideoPrivacy.PRIVATE)
65 } else { // We can schedule video publication only if it it is private
66 this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE
69 this.explainedVideoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
71 const videoFiles = (video as VideoDetails).files
72 if (videoFiles.length > 1) { // Already transcoded
73 this.waitTranscodingEnabled = false
76 // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout
77 setTimeout(() => this.hydrateFormFromVideo())
82 this.notifier.error(err.message)
87 @HostListener('window:beforeunload', [ '$event' ])
88 onUnload (event: any) {
89 const { text, canDeactivate } = this.canDeactivate()
91 if (canDeactivate) return
93 event.returnValue = text
97 canDeactivate (): { canDeactivate: boolean, text?: string } {
98 if (this.updateDone === true) return { canDeactivate: true }
100 const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.')
102 for (const caption of this.videoCaptions) {
103 if (caption.action) return { canDeactivate: false, text }
106 return { canDeactivate: this.formChanged === false, text }
112 return this.form.valid
116 if (this.checkForm() === false
117 || this.isUpdatingVideo === true) {
121 this.video.patch(this.form.value)
123 this.loadingBar.start()
124 this.isUpdatingVideo = true
127 this.videoService.updateVideo(this.video)
129 // Then update captions
130 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
134 this.updateDone = true
135 this.isUpdatingVideo = false
136 this.loadingBar.complete()
137 this.notifier.success(this.i18n('Video updated.'))
138 this.router.navigate([ '/videos/watch', this.video.uuid ])
142 this.loadingBar.complete()
143 this.isUpdatingVideo = false
144 this.notifier.error(err.message)
150 private hydrateFormFromVideo () {
151 this.form.patchValue(this.video.toFormPatch())
156 name: 'thumbnailfile'
164 for (const obj of objects) {
165 fetch(this.video[obj.url])
166 .then(response => response.blob())
168 this.form.patchValue({