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