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