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