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 notificationOptions = {
24 showProgressBar: false,
26 preventDuplicates: false,
27 preventLastDuplicates: 'visible',
31 isMenuDisplayed = true
32 isMenuChangedByUser = false
38 private router: Router,
39 private authService: AuthService,
40 private serverService: ServerService,
41 private domSanitizer: DomSanitizer,
42 private redirectService: RedirectService,
43 private screenService: ScreenService,
44 private hotkeysService: HotkeysService,
45 private themeService: ThemeService
48 get serverVersion () {
49 return this.serverService.getConfig().serverVersion
53 const commit = this.serverService.getConfig().serverCommit || ''
54 return (commit !== '') ? '...' + commit : commit
58 return this.serverService.getConfig().instance.name
62 return RedirectService.DEFAULT_ROUTE
66 document.getElementById('incompatible-browser').className += ' browser-ok'
68 this.router.events.subscribe(e => {
69 if (e instanceof NavigationEnd) {
70 const pathname = window.location.pathname
71 if (!pathname || pathname === '/' || is18nPath(pathname)) {
72 this.redirectService.redirectToHomepage(true)
77 this.authService.loadClientCredentials()
79 if (this.isUserLoggedIn()) {
80 // The service will automatically redirect to the login page if the token is not valid anymore
81 this.authService.refreshUserInformation()
84 // Load custom data from server
85 this.serverService.loadConfig()
86 this.serverService.loadVideoCategories()
87 this.serverService.loadVideoLanguages()
88 this.serverService.loadVideoLicences()
89 this.serverService.loadVideoPrivacies()
91 // Do not display menu on small screens
92 if (this.screenService.isInSmallView()) {
93 this.isMenuDisplayed = false
96 this.router.events.subscribe(
98 // User clicked on a link in the menu, change the page
99 if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) {
100 this.isMenuDisplayed = false
106 this.serverService.configLoaded
108 const config = this.serverService.getConfig()
110 if (config.instance.customizations.javascript) {
112 // tslint:disable:no-eval
113 eval(config.instance.customizations.javascript)
115 console.error('Cannot eval custom JavaScript.', err)
120 // Inject CSS if modified (admin config settings)
121 this.serverService.configLoaded
122 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
124 const headStyle = document.querySelector('style.custom-css-style')
125 if (headStyle) headStyle.parentNode.removeChild(headStyle)
127 const config = this.serverService.getConfig()
129 // We test customCSS if the admin removed the css
130 if (this.customCSS || config.instance.customizations.css) {
131 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
132 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
136 this.hotkeysService.add([
137 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
138 document.getElementById('search-video').focus()
140 }, undefined, this.i18n('Focus the search bar')),
141 new Hotkey('b', (event: KeyboardEvent): boolean => {
144 }, undefined, this.i18n('Toggle the left menu')),
145 new Hotkey('g o', (event: KeyboardEvent): boolean => {
146 this.router.navigate([ '/videos/overview' ])
148 }, undefined, this.i18n('Go to the videos overview page')),
149 new Hotkey('g t', (event: KeyboardEvent): boolean => {
150 this.router.navigate([ '/videos/trending' ])
152 }, undefined, this.i18n('Go to the trending videos page')),
153 new Hotkey('g r', (event: KeyboardEvent): boolean => {
154 this.router.navigate([ '/videos/recently-added' ])
156 }, undefined, this.i18n('Go to the recently added videos page')),
157 new Hotkey('g l', (event: KeyboardEvent): boolean => {
158 this.router.navigate([ '/videos/local' ])
160 }, undefined, this.i18n('Go to the local videos page')),
161 new Hotkey('g u', (event: KeyboardEvent): boolean => {
162 this.router.navigate([ '/videos/upload' ])
164 }, undefined, this.i18n('Go to the videos upload page')),
165 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
166 this.themeService.toggleDarkTheme()
168 }, undefined, this.i18n('Toggle Dark theme'))
171 fromEvent(window, 'resize')
172 .pipe(debounceTime(200))
173 .subscribe(() => this.onResize())
177 return this.authService.isLoggedIn()
181 this.isMenuDisplayed = !this.isMenuDisplayed
182 this.isMenuChangedByUser = true
186 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser