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