]>
Commit | Line | Data |
---|---|---|
f8c00564 | 1 | import { of } from 'rxjs' |
db400f44 | 2 | import { map, switchMap } from 'rxjs/operators' |
674a66bb | 3 | import { Component, HostListener, OnInit } from '@angular/core' |
df98563e | 4 | import { ActivatedRoute, Router } from '@angular/router' |
f8b2c1b4 | 5 | import { Notifier } from '@app/core' |
52c4976f | 6 | import { FormReactive, FormValidatorService, SelectChannelItem } from '@app/shared/shared-forms' |
f8c00564 C |
7 | import { VideoCaptionEdit, VideoCaptionService, VideoDetails, VideoEdit, VideoService } from '@app/shared/shared-main' |
8 | import { LiveVideoService } from '@app/shared/shared-video-live' | |
67ed6552 | 9 | import { LoadingBarService } from '@ngx-loading-bar/core' |
b5b68755 | 10 | import { LiveVideo, LiveVideoUpdate, VideoPrivacy } from '@shared/models' |
c6c0fa6c | 11 | import { hydrateFormFromVideo } from './shared/video-edit-utils' |
1553e15d | 12 | |
dc8bc31b | 13 | @Component({ |
d8e689b8 | 14 | selector: 'my-videos-update', |
80958c78 | 15 | styleUrls: [ './shared/video-edit.component.scss' ], |
d8e689b8 | 16 | templateUrl: './video-update.component.html' |
dc8bc31b | 17 | }) |
d8e689b8 | 18 | export class VideoUpdateComponent extends FormReactive implements OnInit { |
404b54e1 | 19 | video: VideoEdit |
ddb62a85 | 20 | videoDetails: VideoDetails |
c6c0fa6c C |
21 | userVideoChannels: SelectChannelItem[] = [] |
22 | videoCaptions: VideoCaptionEdit[] = [] | |
a5cf76af | 23 | liveVideo: LiveVideo |
4b2f33f3 | 24 | |
68e24d72 | 25 | isUpdatingVideo = false |
bbe0f064 | 26 | schedulePublicationPossible = false |
14e2014a | 27 | waitTranscodingEnabled = true |
dc8bc31b | 28 | |
772d5642 C |
29 | private updateDone = false |
30 | ||
df98563e | 31 | constructor ( |
d18d6478 | 32 | protected formValidatorService: FormValidatorService, |
d8e689b8 | 33 | private route: ActivatedRoute, |
7ddd02c9 | 34 | private router: Router, |
f8b2c1b4 | 35 | private notifier: Notifier, |
15a7387d | 36 | private videoService: VideoService, |
6200d8d9 | 37 | private loadingBar: LoadingBarService, |
b5b68755 C |
38 | private videoCaptionService: VideoCaptionService, |
39 | private liveVideoService: LiveVideoService | |
66357162 | 40 | ) { |
df98563e | 41 | super() |
4b2f33f3 | 42 | } |
dc8bc31b | 43 | |
df98563e | 44 | ngOnInit () { |
d18d6478 | 45 | this.buildForm({}) |
d8e689b8 | 46 | |
308c4275 C |
47 | this.route.data |
48 | .pipe(map(data => data.videoData)) | |
a5cf76af | 49 | .subscribe(({ video, videoChannels, videoCaptions, liveVideo }) => { |
308c4275 | 50 | this.video = new VideoEdit(video) |
ddb62a85 C |
51 | this.videoDetails = video |
52 | ||
308c4275 C |
53 | this.userVideoChannels = videoChannels |
54 | this.videoCaptions = videoCaptions | |
a5cf76af | 55 | this.liveVideo = liveVideo |
db400f44 | 56 | |
12bec528 C |
57 | this.schedulePublicationPossible = this.video.privacy === VideoPrivacy.PRIVATE |
58 | ||
772d5642 | 59 | // FIXME: Angular does not detect the change inside this subscription, so use the patched setTimeout |
b5b68755 C |
60 | setTimeout(() => { |
61 | hydrateFormFromVideo(this.form, this.video, true) | |
62 | ||
63 | if (this.liveVideo) { | |
64 | this.form.patchValue({ | |
bb4ba6d9 C |
65 | saveReplay: this.liveVideo.saveReplay, |
66 | permanentLive: this.liveVideo.permanentLive | |
b5b68755 C |
67 | }) |
68 | } | |
69 | }) | |
308c4275 | 70 | }, |
2de96f4d | 71 | |
308c4275 C |
72 | err => { |
73 | console.error(err) | |
f8b2c1b4 | 74 | this.notifier.error(err.message) |
308c4275 C |
75 | } |
76 | ) | |
e822fdae C |
77 | } |
78 | ||
674a66bb C |
79 | @HostListener('window:beforeunload', [ '$event' ]) |
80 | onUnload (event: any) { | |
81 | const { text, canDeactivate } = this.canDeactivate() | |
82 | ||
83 | if (canDeactivate) return | |
84 | ||
85 | event.returnValue = text | |
86 | return text | |
87 | } | |
88 | ||
89 | canDeactivate (): { canDeactivate: boolean, text?: string } { | |
772d5642 C |
90 | if (this.updateDone === true) return { canDeactivate: true } |
91 | ||
66357162 | 92 | const text = $localize`You have unsaved changes! If you leave, your changes will be lost.` |
674a66bb | 93 | |
772d5642 | 94 | for (const caption of this.videoCaptions) { |
674a66bb | 95 | if (caption.action) return { canDeactivate: false, text } |
772d5642 C |
96 | } |
97 | ||
674a66bb | 98 | return { canDeactivate: this.formChanged === false, text } |
772d5642 C |
99 | } |
100 | ||
df98563e C |
101 | checkForm () { |
102 | this.forceCheck() | |
c24ac1c1 | 103 | |
df98563e | 104 | return this.form.valid |
c24ac1c1 C |
105 | } |
106 | ||
ddb62a85 C |
107 | isWaitTranscodingEnabled () { |
108 | if (this.videoDetails.getFiles().length > 1) { // Already transcoded | |
109 | return false | |
110 | } | |
111 | ||
112 | if (this.liveVideo && this.form.value['saveReplay'] !== true) { | |
113 | return false | |
114 | } | |
115 | ||
116 | return true | |
117 | } | |
118 | ||
df98563e | 119 | update () { |
8c2b9756 RK |
120 | if (this.checkForm() === false |
121 | || this.isUpdatingVideo === true) { | |
df98563e | 122 | return |
c24ac1c1 C |
123 | } |
124 | ||
df98563e | 125 | this.video.patch(this.form.value) |
d8e689b8 | 126 | |
a02b93ce | 127 | this.loadingBar.useRef().start() |
68e24d72 | 128 | this.isUpdatingVideo = true |
40e87e9e C |
129 | |
130 | // Update the video | |
d8e689b8 | 131 | this.videoService.updateVideo(this.video) |
40e87e9e C |
132 | .pipe( |
133 | // Then update captions | |
b5b68755 C |
134 | switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions)), |
135 | ||
136 | switchMap(() => { | |
137 | if (!this.liveVideo) return of(undefined) | |
138 | ||
fd0fdc46 C |
139 | const liveVideoUpdate: LiveVideoUpdate = { |
140 | saveReplay: this.form.value.saveReplay, | |
141 | permanentLive: this.form.value.permanentLive | |
142 | } | |
143 | ||
144 | console.log(liveVideoUpdate) | |
145 | console.log(this.form.value) | |
146 | ||
147 | // Don't update live attributes if they did not change | |
148 | const liveChanged = Object.keys(liveVideoUpdate) | |
149 | .some(key => this.liveVideo[key] !== liveVideoUpdate[key]) | |
150 | if (!liveChanged) return of(undefined) | |
151 | ||
b5b68755 C |
152 | return this.liveVideoService.updateLive(this.video.id, liveVideoUpdate) |
153 | }) | |
40e87e9e C |
154 | ) |
155 | .subscribe( | |
156 | () => { | |
772d5642 | 157 | this.updateDone = true |
40e87e9e | 158 | this.isUpdatingVideo = false |
a02b93ce | 159 | this.loadingBar.useRef().complete() |
66357162 | 160 | this.notifier.success($localize`Video updated.`) |
40e87e9e C |
161 | this.router.navigate([ '/videos/watch', this.video.uuid ]) |
162 | }, | |
163 | ||
164 | err => { | |
a02b93ce | 165 | this.loadingBar.useRef().complete() |
40e87e9e | 166 | this.isUpdatingVideo = false |
f8b2c1b4 | 167 | this.notifier.error(err.message) |
40e87e9e C |
168 | console.error(err) |
169 | } | |
170 | ) | |
dc8bc31b | 171 | } |
e54163c2 | 172 | |
7294aab0 C |
173 | hydratePluginFieldsFromVideo () { |
174 | if (!this.video.pluginData) return | |
175 | ||
176 | this.form.patchValue({ | |
177 | pluginData: this.video.pluginData | |
178 | }) | |
179 | } | |
dc8bc31b | 180 | } |