5bd7688092020553001626b29f52321d9d606749
[oweals/peertube.git] / client / src / app / videos / +video-edit / video-add.component.ts
1 import { Component, HostListener, OnInit, ViewChild } from '@angular/core'
2 import { AuthService, CanComponentDeactivate, ServerService } from '@app/core'
3 import { ServerConfig } from '@shared/models'
4 import { VideoImportTorrentComponent } from './video-add-components/video-import-torrent.component'
5 import { VideoImportUrlComponent } from './video-add-components/video-import-url.component'
6 import { VideoUploadComponent } from './video-add-components/video-upload.component'
7
8 @Component({
9   selector: 'my-videos-add',
10   templateUrl: './video-add.component.html',
11   styleUrls: [ './video-add.component.scss' ]
12 })
13 export class VideoAddComponent implements OnInit, CanComponentDeactivate {
14   @ViewChild('videoUpload') videoUpload: VideoUploadComponent
15   @ViewChild('videoImportUrl') videoImportUrl: VideoImportUrlComponent
16   @ViewChild('videoImportTorrent') videoImportTorrent: VideoImportTorrentComponent
17
18   secondStepType: 'upload' | 'import-url' | 'import-torrent'
19   videoName: string
20   serverConfig: ServerConfig
21
22   constructor (
23     private auth: AuthService,
24     private serverService: ServerService
25   ) {}
26
27   ngOnInit () {
28     this.serverConfig = this.serverService.getTmpConfig()
29
30     this.serverService.getConfig()
31       .subscribe(config => this.serverConfig = config)
32   }
33
34   onFirstStepDone (type: 'upload' | 'import-url' | 'import-torrent', videoName: string) {
35     this.secondStepType = type
36     this.videoName = videoName
37   }
38
39   onError () {
40     this.videoName = undefined
41     this.secondStepType = undefined
42   }
43
44   @HostListener('window:beforeunload', [ '$event' ])
45   onUnload (event: any) {
46     const { text, canDeactivate } = this.canDeactivate()
47
48     if (canDeactivate) return
49
50     event.returnValue = text
51     return text
52   }
53
54   canDeactivate (): { canDeactivate: boolean, text?: string} {
55     if (this.secondStepType === 'upload') return this.videoUpload.canDeactivate()
56     if (this.secondStepType === 'import-url') return this.videoImportUrl.canDeactivate()
57     if (this.secondStepType === 'import-torrent') return this.videoImportTorrent.canDeactivate()
58
59     return { canDeactivate: true }
60   }
61
62   isVideoImportHttpEnabled () {
63     return this.serverConfig.import.videos.http.enabled
64   }
65
66   isVideoImportTorrentEnabled () {
67     return this.serverConfig.import.videos.torrent.enabled
68   }
69
70   isInSecondStep () {
71     return !!this.secondStepType
72   }
73
74   isRootUser () {
75     return this.auth.getUser().username === 'root'
76   }
77 }