]>
Commit | Line | Data |
---|---|---|
db400f44 | 1 | import { map, switchMap } from 'rxjs/operators' |
674a66bb | 2 | import { Component, HostListener, OnInit } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
68e24d72 | 4 | import { LoadingBarService } from '@ngx-loading-bar/core' |
f8b2c1b4 | 5 | import { Notifier } from '@app/core' |
db7af09b | 6 | import { ServerService } from '../../core' |
4cc66133 | 7 | import { FormReactive } from '../../shared' |
202f6b6c | 8 | import { VideoEdit } from '../../shared/video/video-edit.model' |
4cc66133 | 9 | import { VideoService } from '../../shared/video/video.service' |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
40e87e9e | 12 | import { VideoCaptionService } from '@app/shared/video-caption' |
ef4c78da | 13 | import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model' |
14e2014a | 14 | import { VideoDetails } from '@app/shared/video/video-details.model' |
1553e15d | 15 | |
dc8bc31b | 16 | @Component({ |
d8e689b8 | 17 | selector: 'my-videos-update', |
80958c78 | 18 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 19 | templateUrl: './video-update.component.html' |
dc8bc31b | 20 | }) |
d8e689b8 | 21 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 22 | video: VideoEdit |
4b2f33f3 | 23 | |
68e24d72 | 24 | isUpdatingVideo = false |
ef4c78da | 25 | userVideoChannels: { id: number, label: string, support: string }[] = [] |
bbe0f064 | 26 | schedulePublicationPossible = false |
ef4c78da | 27 | videoCaptions: VideoCaptionEdit[] = [] |
14e2014a | 28 | waitTranscodingEnabled = true |
dc8bc31b | 29 | |
772d5642 C |
30 | private updateDone = false |
31 | ||
df98563e | 32 | constructor ( |
d18d6478 | 33 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 34 | private route: ActivatedRoute, |
7ddd02c9 | 35 | private router: Router, |
f8b2c1b4 | 36 | private notifier: Notifier, |
db7af09b | 37 | private serverService: ServerService, |
15a7387d | 38 | private videoService: VideoService, |
6200d8d9 | 39 | private loadingBar: LoadingBarService, |
40e87e9e | 40 | private videoCaptionService: VideoCaptionService, |
b1d40cff | 41 | private i18n: I18n |
4b2f33f3 | 42 | ) { |
df98563e | 43 | super() |
4b2f33f3 | 44 | } |
dc8bc31b | 45 | |
df98563e | 46 | ngOnInit () { |
d18d6478 | 47 | this.buildForm({}) |
d8e689b8 | 48 | |
308c4275 C |
49 | this.route.data |
50 | .pipe(map(data => data.videoData)) | |
51 | .subscribe(({ video, videoChannels, videoCaptions }) => { | |
52 | this.video = new VideoEdit(video) | |
53 | this.userVideoChannels = videoChannels | |
54 | this.videoCaptions = videoCaptions | |
db400f44 | 55 | |
14e2014a C |
56 | const videoFiles = (video as VideoDetails).files |
57 | if (videoFiles.length > 1) { // Already transcoded | |
58 | this.waitTranscodingEnabled = false | |
59 | } | |
60 | ||
772d5642 | 61 | // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout |
308c4275 C |
62 | setTimeout(() => this.hydrateFormFromVideo()) |
63 | }, | |
2de96f4d | 64 | |
308c4275 C |
65 | err => { |
66 | console.error(err) | |
f8b2c1b4 | 67 | this.notifier.error(err.message) |
308c4275 C |
68 | } |
69 | ) | |
e822fdae C |
70 | } |
71 | ||
674a66bb C |
72 | @HostListener('window:beforeunload', [ '$event' ]) |
73 | onUnload (event: any) { | |
74 | const { text, canDeactivate } = this.canDeactivate() | |
75 | ||
76 | if (canDeactivate) return | |
77 | ||
78 | event.returnValue = text | |
79 | return text | |
80 | } | |
81 | ||
82 | canDeactivate (): { canDeactivate: boolean, text?: string } { | |
772d5642 C |
83 | if (this.updateDone === true) return { canDeactivate: true } |
84 | ||
674a66bb C |
85 | const text = this.i18n('You have unsaved changes! If you leave, your changes will be lost.') |
86 | ||
772d5642 | 87 | for (const caption of this.videoCaptions) { |
674a66bb | 88 | if (caption.action) return { canDeactivate: false, text } |
772d5642 C |
89 | } |
90 | ||
674a66bb | 91 | return { canDeactivate: this.formChanged === false, text } |
772d5642 C |
92 | } |
93 | ||
df98563e C |
94 | checkForm () { |
95 | this.forceCheck() | |
c24ac1c1 | 96 | |
df98563e | 97 | return this.form.valid |
c24ac1c1 C |
98 | } |
99 | ||
df98563e | 100 | update () { |
8c2b9756 RK |
101 | if (this.checkForm() === false |
102 | || this.isUpdatingVideo === true) { | |
df98563e | 103 | return |
c24ac1c1 C |
104 | } |
105 | ||
df98563e | 106 | this.video.patch(this.form.value) |
d8e689b8 | 107 | |
68e24d72 C |
108 | this.loadingBar.start() |
109 | this.isUpdatingVideo = true | |
40e87e9e C |
110 | |
111 | // Update the video | |
d8e689b8 | 112 | this.videoService.updateVideo(this.video) |
40e87e9e C |
113 | .pipe( |
114 | // Then update captions | |
115 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)) | |
116 | ) | |
117 | .subscribe( | |
118 | () => { | |
772d5642 | 119 | this.updateDone = true |
40e87e9e C |
120 | this.isUpdatingVideo = false |
121 | this.loadingBar.complete() | |
f8b2c1b4 | 122 | this.notifier.success(this.i18n('Video updated.')) |
40e87e9e C |
123 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
124 | }, | |
125 | ||
126 | err => { | |
0f7fedc3 | 127 | this.loadingBar.complete() |
40e87e9e | 128 | this.isUpdatingVideo = false |
f8b2c1b4 | 129 | this.notifier.error(err.message) |
40e87e9e C |
130 | console.error(err) |
131 | } | |
132 | ) | |
dc8bc31b | 133 | } |
e54163c2 | 134 | |
df98563e | 135 | private hydrateFormFromVideo () { |
bbe0f064 | 136 | this.form.patchValue(this.video.toFormPatch()) |
6de36768 C |
137 | |
138 | const objects = [ | |
139 | { | |
140 | url: 'thumbnailUrl', | |
141 | name: 'thumbnailfile' | |
142 | }, | |
143 | { | |
144 | url: 'previewUrl', | |
145 | name: 'previewfile' | |
146 | } | |
147 | ] | |
148 | ||
149 | for (const obj of objects) { | |
150 | fetch(this.video[obj.url]) | |
151 | .then(response => response.blob()) | |
152 | .then(data => { | |
153 | this.form.patchValue({ | |
154 | [ obj.name ]: data | |
155 | }) | |
156 | }) | |
157 | } | |
d8e689b8 | 158 | } |
dc8bc31b | 159 | } |