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