220b104b7f12f9ebdc0969fd91a0c764216fbaa7
[oweals/peertube.git] / client / src / app / app.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router'
3 import { AuthService, ServerService } from '@app/core'
4 import { isInSmallView } from '@app/shared/misc/utils'
5
6 @Component({
7   selector: 'my-app',
8   templateUrl: './app.component.html',
9   styleUrls: [ './app.component.scss' ]
10 })
11 export class AppComponent implements OnInit {
12   notificationOptions = {
13     timeOut: 5000,
14     lastOnBottom: true,
15     clickToClose: true,
16     maxLength: 0,
17     maxStack: 7,
18     showProgressBar: false,
19     pauseOnHover: false,
20     preventDuplicates: false,
21     preventLastDuplicates: 'visible',
22     rtl: false
23   }
24
25   isMenuDisplayed = true
26
27   constructor (
28     private router: Router,
29     private authService: AuthService,
30     private serverService: ServerService
31   ) {}
32
33   get serverVersion () {
34     return this.serverService.getConfig().serverVersion
35   }
36
37   get instanceName () {
38     return this.serverService.getConfig().instance.name
39   }
40
41   ngOnInit () {
42     this.authService.loadClientCredentials()
43
44     if (this.authService.isLoggedIn()) {
45       // The service will automatically redirect to the login page if the token is not valid anymore
46       this.authService.refreshUserInformation()
47     }
48
49     // Load custom data from server
50     this.serverService.loadConfig()
51     this.serverService.loadVideoCategories()
52     this.serverService.loadVideoLanguages()
53     this.serverService.loadVideoLicences()
54     this.serverService.loadVideoPrivacies()
55
56     // Do not display menu on small screens
57     if (isInSmallView()) {
58       this.isMenuDisplayed = false
59     }
60
61     this.router.events.subscribe(
62       e => {
63         // User clicked on a link in the menu, change the page
64         if (e instanceof GuardsCheckStart && isInSmallView()) {
65           this.isMenuDisplayed = false
66         }
67       }
68     )
69   }
70
71   toggleMenu () {
72     window.scrollTo(0, 0)
73     this.isMenuDisplayed = !this.isMenuDisplayed
74   }
75
76   getMainColClasses () {
77     // Take all width is the menu is not displayed
78     if (this.isMenuDisplayed === false) return [ 'expanded' ]
79
80     return []
81   }
82 }