1 import { Component, OnInit } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router'
4 import { AuthService, RedirectService, ServerService } from '@app/core'
5 import { is18nPath } from '../../../shared/models/i18n'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { skip } from 'rxjs/operators'
8 import { HotkeysService, Hotkey } from 'angular2-hotkeys'
12 templateUrl: './app.component.html',
13 styleUrls: [ './app.component.scss' ]
15 export class AppComponent implements OnInit {
16 notificationOptions = {
22 showProgressBar: false,
24 preventDuplicates: false,
25 preventLastDuplicates: 'visible',
29 isMenuDisplayed = true
34 private router: Router,
35 private authService: AuthService,
36 private serverService: ServerService,
37 private domSanitizer: DomSanitizer,
38 private redirectService: RedirectService,
39 private screenService: ScreenService,
40 private hotkeysService: HotkeysService
43 get serverVersion () {
44 return this.serverService.getConfig().serverVersion
48 return this.serverService.getConfig().instance.name
52 return RedirectService.DEFAULT_ROUTE
56 document.getElementById('incompatible-browser').className += ' browser-ok'
58 this.router.events.subscribe(e => {
59 if (e instanceof NavigationEnd) {
60 const pathname = window.location.pathname
61 if (!pathname || pathname === '/' || is18nPath(pathname)) {
62 this.redirectService.redirectToHomepage(true)
67 this.authService.loadClientCredentials()
69 if (this.isUserLoggedIn()) {
70 // The service will automatically redirect to the login page if the token is not valid anymore
71 this.authService.refreshUserInformation()
74 // Load custom data from server
75 this.serverService.loadConfig()
76 this.serverService.loadVideoCategories()
77 this.serverService.loadVideoLanguages()
78 this.serverService.loadVideoLicences()
79 this.serverService.loadVideoPrivacies()
81 // Do not display menu on small screens
82 if (this.screenService.isInSmallView()) {
83 this.isMenuDisplayed = false
86 this.router.events.subscribe(
88 // User clicked on a link in the menu, change the page
89 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
90 this.isMenuDisplayed = false
96 this.serverService.configLoaded
98 const config = this.serverService.getConfig()
100 if (config.instance.customizations.javascript) {
102 // tslint:disable:no-eval
103 eval(config.instance.customizations.javascript)
105 console.error('Cannot eval custom JavaScript.', err)
110 // Inject CSS if modified (admin config settings)
111 this.serverService.configLoaded
112 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
114 const headStyle = document.querySelector('style.custom-css-style')
115 if (headStyle) headStyle.parentNode.removeChild(headStyle)
117 const config = this.serverService.getConfig()
119 // We test customCSS if the admin removed the css
120 if (this.customCSS || config.instance.customizations.css) {
121 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
122 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
126 this.hotkeysService.add([
127 new Hotkey('/', (event: KeyboardEvent): boolean => {
128 document.getElementById('search-video').focus()
129 return false // Prevent bubbling
130 }, undefined, 'Focus the search bar'),
131 new Hotkey('g+s', (event: KeyboardEvent): boolean => {
132 this.router.navigate([ '/videos/subscriptions' ])
134 }, undefined, 'Go to the subscriptions videos page'),
135 new Hotkey('g+t', (event: KeyboardEvent): boolean => {
136 this.router.navigate([ '/videos/trending' ])
138 }, undefined, 'Go to the trending videos page'),
139 new Hotkey('g+r', (event: KeyboardEvent): boolean => {
140 this.router.navigate([ '/videos/recently-added' ])
142 }, undefined, 'Go to the recently added videos page'),
143 new Hotkey('g+l', (event: KeyboardEvent): boolean => {
144 this.router.navigate([ '/videos/local' ])
146 }, undefined, 'Go to the local videos page'),
147 new Hotkey('g+u', (event: KeyboardEvent): boolean => {
148 this.router.navigate([ '/videos/upload' ])
150 }, undefined, 'Go to the videos upload page')
155 return this.authService.isLoggedIn()
159 this.isMenuDisplayed = !this.isMenuDisplayed