]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/videos/+video-edit/video-add.component.ts
Lazy load static objects
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add.component.ts
index 57a9d0ca7abd4d2d541f490a74b4ff44f50d9c84..401d8a08f3966bda34be3c735eb18ed87e1ec0fb 100644 (file)
@@ -1,27 +1,37 @@
-import { Component, ViewChild } from '@angular/core'
+import { Component, HostListener, OnInit, ViewChild } from '@angular/core'
 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
 import { VideoImportUrlComponent } from '@app/videos/+video-edit/video-add-components/video-import-url.component'
 import { VideoUploadComponent } from '@app/videos/+video-edit/video-add-components/video-upload.component'
-import { ServerService } from '@app/core'
+import { AuthService, ServerService } from '@app/core'
 import { VideoImportTorrentComponent } from '@app/videos/+video-edit/video-add-components/video-import-torrent.component'
+import { ServerConfig } from '@shared/models'
 
 @Component({
   selector: 'my-videos-add',
   templateUrl: './video-add.component.html',
   styleUrls: [ './video-add.component.scss' ]
 })
-export class VideoAddComponent implements CanComponentDeactivate {
-  @ViewChild('videoUpload') videoUpload: VideoUploadComponent
-  @ViewChild('videoImportUrl') videoImportUrl: VideoImportUrlComponent
-  @ViewChild('videoImportTorrent') videoImportTorrent: VideoImportTorrentComponent
+export class VideoAddComponent implements OnInit, CanComponentDeactivate {
+  @ViewChild('videoUpload', { static: false }) videoUpload: VideoUploadComponent
+  @ViewChild('videoImportUrl', { static: false }) videoImportUrl: VideoImportUrlComponent
+  @ViewChild('videoImportTorrent', { static: false }) videoImportTorrent: VideoImportTorrentComponent
 
   secondStepType: 'upload' | 'import-url' | 'import-torrent'
   videoName: string
+  serverConfig: ServerConfig
 
   constructor (
+    private auth: AuthService,
     private serverService: ServerService
   ) {}
 
+  ngOnInit () {
+    this.serverConfig = this.serverService.getTmpConfig()
+
+    this.serverService.getConfig()
+      .subscribe(config => this.serverConfig = config)
+  }
+
   onFirstStepDone (type: 'upload' | 'import-url' | 'import-torrent', videoName: string) {
     this.secondStepType = type
     this.videoName = videoName
@@ -32,7 +42,17 @@ export class VideoAddComponent implements CanComponentDeactivate {
     this.secondStepType = undefined
   }
 
-  canDeactivate () {
+  @HostListener('window:beforeunload', [ '$event' ])
+  onUnload (event: any) {
+    const { text, canDeactivate } = this.canDeactivate()
+
+    if (canDeactivate) return
+
+    event.returnValue = text
+    return text
+  }
+
+  canDeactivate (): { canDeactivate: boolean, text?: string} {
     if (this.secondStepType === 'upload') return this.videoUpload.canDeactivate()
     if (this.secondStepType === 'import-url') return this.videoImportUrl.canDeactivate()
     if (this.secondStepType === 'import-torrent') return this.videoImportTorrent.canDeactivate()
@@ -41,10 +61,18 @@ export class VideoAddComponent implements CanComponentDeactivate {
   }
 
   isVideoImportHttpEnabled () {
-    return this.serverService.getConfig().import.videos.http.enabled
+    return this.serverConfig.import.videos.http.enabled
   }
 
   isVideoImportTorrentEnabled () {
-    return this.serverService.getConfig().import.videos.torrent.enabled
+    return this.serverConfig.import.videos.torrent.enabled
+  }
+
+  isInSecondStep () {
+    return !!this.secondStepType
+  }
+
+  isRootUser () {
+    return this.auth.getUser().username === 'root'
   }
 }