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, ThemeService } from '@app/core'
5 import { is18nPath } from '../../../shared/models/i18n'
6 import { ScreenService } from '@app/shared/misc/screen.service'
7 import { skip, debounceTime } from 'rxjs/operators'
8 import { HotkeysService, Hotkey } from 'angular2-hotkeys'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { fromEvent } from 'rxjs'
14 templateUrl: './app.component.html',
15 styleUrls: [ './app.component.scss' ]
17 export class AppComponent implements OnInit {
18 isMenuDisplayed = true
19 isMenuChangedByUser = false
25 private router: Router,
26 private authService: AuthService,
27 private serverService: ServerService,
28 private domSanitizer: DomSanitizer,
29 private redirectService: RedirectService,
30 private screenService: ScreenService,
31 private hotkeysService: HotkeysService,
32 private themeService: ThemeService
35 get serverVersion () {
36 return this.serverService.getConfig().serverVersion
40 const commit = this.serverService.getConfig().serverCommit || ''
41 return (commit !== '') ? '...' + commit : commit
45 return this.serverService.getConfig().instance.name
49 return RedirectService.DEFAULT_ROUTE
53 document.getElementById('incompatible-browser').className += ' browser-ok'
55 this.router.events.subscribe(e => {
56 if (e instanceof NavigationEnd) {
57 const pathname = window.location.pathname
58 if (!pathname || pathname === '/' || is18nPath(pathname)) {
59 this.redirectService.redirectToHomepage(true)
64 this.authService.loadClientCredentials()
66 if (this.isUserLoggedIn()) {
67 // The service will automatically redirect to the login page if the token is not valid anymore
68 this.authService.refreshUserInformation()
71 // Load custom data from server
72 this.serverService.loadConfig()
73 this.serverService.loadVideoCategories()
74 this.serverService.loadVideoLanguages()
75 this.serverService.loadVideoLicences()
76 this.serverService.loadVideoPrivacies()
77 this.serverService.loadVideoPlaylistPrivacies()
79 // Do not display menu on small screens
80 if (this.screenService.isInSmallView()) {
81 this.isMenuDisplayed = false
84 this.router.events.subscribe(
86 // User clicked on a link in the menu, change the page
87 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
88 this.isMenuDisplayed = false
94 this.serverService.configLoaded
96 const config = this.serverService.getConfig()
98 if (config.instance.customizations.javascript) {
100 // tslint:disable:no-eval
101 eval(config.instance.customizations.javascript)
103 console.error('Cannot eval custom JavaScript.', err)
108 // Inject CSS if modified (admin config settings)
109 this.serverService.configLoaded
110 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
112 const headStyle = document.querySelector('style.custom-css-style')
113 if (headStyle) headStyle.parentNode.removeChild(headStyle)
115 const config = this.serverService.getConfig()
117 // We test customCSS if the admin removed the css
118 if (this.customCSS || config.instance.customizations.css) {
119 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
120 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
124 this.hotkeysService.add([
125 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
126 document.getElementById('search-video').focus()
128 }, undefined, this.i18n('Focus the search bar')),
129 new Hotkey('b', (event: KeyboardEvent): boolean => {
132 }, undefined, this.i18n('Toggle the left menu')),
133 new Hotkey('g o', (event: KeyboardEvent): boolean => {
134 this.router.navigate([ '/videos/overview' ])
136 }, undefined, this.i18n('Go to the videos overview page')),
137 new Hotkey('g t', (event: KeyboardEvent): boolean => {
138 this.router.navigate([ '/videos/trending' ])
140 }, undefined, this.i18n('Go to the trending videos page')),
141 new Hotkey('g r', (event: KeyboardEvent): boolean => {
142 this.router.navigate([ '/videos/recently-added' ])
144 }, undefined, this.i18n('Go to the recently added videos page')),
145 new Hotkey('g l', (event: KeyboardEvent): boolean => {
146 this.router.navigate([ '/videos/local' ])
148 }, undefined, this.i18n('Go to the local videos page')),
149 new Hotkey('g u', (event: KeyboardEvent): boolean => {
150 this.router.navigate([ '/videos/upload' ])
152 }, undefined, this.i18n('Go to the videos upload page')),
153 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
154 this.themeService.toggleDarkTheme()
156 }, undefined, this.i18n('Toggle Dark theme'))
159 fromEvent(window, 'resize')
160 .pipe(debounceTime(200))
161 .subscribe(() => this.onResize())
165 return this.authService.isLoggedIn()
169 this.isMenuDisplayed = !this.isMenuDisplayed
170 this.isMenuChangedByUser = true
174 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser