1 import { Component, HostListener, OnInit, ViewChild } from '@angular/core'
2 import { AuthService, CanComponentDeactivate, ServerService, User } 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'
9 selector: 'my-videos-add',
10 templateUrl: './video-add.component.html',
11 styleUrls: [ './video-add.component.scss' ]
13 export class VideoAddComponent implements OnInit, CanComponentDeactivate {
14 @ViewChild('videoUpload') videoUpload: VideoUploadComponent
15 @ViewChild('videoImportUrl') videoImportUrl: VideoImportUrlComponent
16 @ViewChild('videoImportTorrent') videoImportTorrent: VideoImportTorrentComponent
20 secondStepType: 'upload' | 'import-url' | 'import-torrent'
22 serverConfig: ServerConfig
25 private auth: AuthService,
26 private serverService: ServerService
29 get userInformationLoaded () {
30 return this.auth.userInformationLoaded
34 this.user = this.auth.getUser()
36 this.serverConfig = this.serverService.getTmpConfig()
38 this.serverService.getConfig()
39 .subscribe(config => this.serverConfig = config)
42 onFirstStepDone (type: 'upload' | 'import-url' | 'import-torrent', videoName: string) {
43 this.secondStepType = type
44 this.videoName = videoName
48 this.videoName = undefined
49 this.secondStepType = undefined
52 @HostListener('window:beforeunload', [ '$event' ])
53 onUnload (event: any) {
54 const { text, canDeactivate } = this.canDeactivate()
56 if (canDeactivate) return
58 event.returnValue = text
62 canDeactivate (): { canDeactivate: boolean, text?: string} {
63 if (this.secondStepType === 'upload') return this.videoUpload.canDeactivate()
64 if (this.secondStepType === 'import-url') return this.videoImportUrl.canDeactivate()
65 if (this.secondStepType === 'import-torrent') return this.videoImportTorrent.canDeactivate()
67 return { canDeactivate: true }
70 isVideoImportHttpEnabled () {
71 return this.serverConfig.import.videos.http.enabled
74 isVideoImportTorrentEnabled () {
75 return this.serverConfig.import.videos.torrent.enabled
79 return !!this.secondStepType
83 return this.auth.getUser().username === 'root'