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