Try to optimize frontend
[oweals/peertube.git] / client / src / app / app.component.ts
1 import { Component, OnInit, ViewContainerRef } from '@angular/core'
2 import { Router } from '@angular/router'
3
4 import { AuthService, ConfigService } from './core'
5 import { UserService } from './shared'
6
7 @Component({
8   selector: 'my-app',
9   templateUrl: './app.component.html',
10   styleUrls: [ './app.component.scss' ]
11 })
12 export class AppComponent implements OnInit {
13   notificationOptions = {
14     timeOut: 3000,
15     lastOnBottom: true,
16     clickToClose: true,
17     maxLength: 0,
18     maxStack: 7,
19     showProgressBar: false,
20     pauseOnHover: false,
21     preventDuplicates: false,
22     preventLastDuplicates: 'visible',
23     rtl: false
24   }
25
26   isMenuDisplayed = true
27
28   constructor (
29     private router: Router,
30     private authService: AuthService,
31     private configService: ConfigService,
32     private userService: UserService
33   ) {}
34
35   ngOnInit () {
36     this.authService.loadClientCredentials()
37
38     if (this.authService.isLoggedIn()) {
39       // The service will automatically redirect to the login page if the token is not valid anymore
40       this.userService.checkTokenValidity()
41     }
42
43     this.configService.loadConfig()
44
45     // Do not display menu on small screens
46     if (window.innerWidth < 600) {
47       this.isMenuDisplayed = false
48     }
49   }
50
51   isInAdmin () {
52     return this.router.url.indexOf('/admin/') !== -1
53   }
54
55   toggleMenu () {
56     this.isMenuDisplayed = !this.isMenuDisplayed
57   }
58
59   getMainColClasses () {
60     const colSizes = {
61       md: 10,
62       sm: 9,
63       xs: 9
64     }
65
66     // Take all width is the menu is not displayed
67     if (this.isMenuDisplayed === false) {
68       Object.keys(colSizes).forEach(col => colSizes[col] = 12)
69     }
70
71     const classes = [ 'main-col' ]
72     Object.keys(colSizes).forEach(col => classes.push(`col-${col}-${colSizes[col]}`))
73
74     return classes
75   }
76 }