1 import { Component, OnInit } from '@angular/core'
2 import { DomSanitizer, SafeHtml } from '@angular/platform-browser'
3 import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } 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 { debounceTime, filter, map, pairwise, skip } from 'rxjs/operators'
8 import { Hotkey, HotkeysService } from 'angular2-hotkeys'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { fromEvent } from 'rxjs'
11 import { ViewportScroller } from '@angular/common'
15 templateUrl: './app.component.html',
16 styleUrls: [ './app.component.scss' ]
18 export class AppComponent implements OnInit {
19 isMenuDisplayed = true
20 isMenuChangedByUser = false
26 private viewportScroller: ViewportScroller,
27 private router: Router,
28 private authService: AuthService,
29 private serverService: ServerService,
30 private domSanitizer: DomSanitizer,
31 private redirectService: RedirectService,
32 private screenService: ScreenService,
33 private hotkeysService: HotkeysService,
34 private themeService: ThemeService
37 get serverVersion () {
38 return this.serverService.getConfig().serverVersion
42 const commit = this.serverService.getConfig().serverCommit || ''
43 return (commit !== '') ? '...' + commit : commit
47 return this.serverService.getConfig().instance.name
51 return RedirectService.DEFAULT_ROUTE
55 document.getElementById('incompatible-browser').className += ' browser-ok'
57 this.authService.loadClientCredentials()
59 if (this.isUserLoggedIn()) {
60 // The service will automatically redirect to the login page if the token is not valid anymore
61 this.authService.refreshUserInformation()
64 // Load custom data from server
65 this.serverService.loadConfig()
66 this.serverService.loadVideoCategories()
67 this.serverService.loadVideoLanguages()
68 this.serverService.loadVideoLicences()
69 this.serverService.loadVideoPrivacies()
70 this.serverService.loadVideoPlaylistPrivacies()
72 // Do not display menu on small screens
73 if (this.screenService.isInSmallView()) {
74 this.isMenuDisplayed = false
77 this.initRouteEvents()
83 fromEvent(window, 'resize')
84 .pipe(debounceTime(200))
85 .subscribe(() => this.onResize())
89 return this.authService.isLoggedIn()
93 this.isMenuDisplayed = !this.isMenuDisplayed
94 this.isMenuChangedByUser = true
98 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
101 private initRouteEvents () {
102 let resetScroll = true
103 const eventsObs = this.router.events
105 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
106 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
108 scrollEvent.subscribe(e => {
110 return this.viewportScroller.scrollToPosition(e.position)
114 return this.viewportScroller.scrollToAnchor(e.anchor)
118 return this.viewportScroller.scrollToPosition([ 0, 0 ])
122 // When we add the a-state parameter, we don't want to alter the scroll
123 navigationEndEvent.pipe(pairwise())
124 .subscribe(([ e1, e2 ]) => {
128 const previousUrl = new URL(window.location.origin + e1.url)
129 const nextUrl = new URL(window.location.origin + e2.url)
131 if (previousUrl.pathname !== nextUrl.pathname) {
136 const nextSearchParams = nextUrl.searchParams
137 nextSearchParams.delete('a-state')
139 const previousSearchParams = previousUrl.searchParams
141 nextSearchParams.sort()
142 previousSearchParams.sort()
144 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
148 console.error('Cannot parse URL to check next scroll.', e)
153 navigationEndEvent.pipe(
154 map(() => window.location.pathname),
155 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
156 ).subscribe(() => this.redirectService.redirectToHomepage(true))
159 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
160 filter(() => this.screenService.isInSmallView())
161 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
164 private injectJS () {
166 this.serverService.configLoaded
168 const config = this.serverService.getConfig()
170 if (config.instance.customizations.javascript) {
172 // tslint:disable:no-eval
173 eval(config.instance.customizations.javascript)
175 console.error('Cannot eval custom JavaScript.', err)
181 private injectCSS () {
182 // Inject CSS if modified (admin config settings)
183 this.serverService.configLoaded
184 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
186 const headStyle = document.querySelector('style.custom-css-style')
187 if (headStyle) headStyle.parentNode.removeChild(headStyle)
189 const config = this.serverService.getConfig()
191 // We test customCSS if the admin removed the css
192 if (this.customCSS || config.instance.customizations.css) {
193 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
194 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
199 private initHotkeys () {
200 this.hotkeysService.add([
201 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
202 document.getElementById('search-video').focus()
204 }, undefined, this.i18n('Focus the search bar')),
205 new Hotkey('b', (event: KeyboardEvent): boolean => {
208 }, undefined, this.i18n('Toggle the left menu')),
209 new Hotkey('g o', (event: KeyboardEvent): boolean => {
210 this.router.navigate([ '/videos/overview' ])
212 }, undefined, this.i18n('Go to the videos overview page')),
213 new Hotkey('g t', (event: KeyboardEvent): boolean => {
214 this.router.navigate([ '/videos/trending' ])
216 }, undefined, this.i18n('Go to the trending videos page')),
217 new Hotkey('g r', (event: KeyboardEvent): boolean => {
218 this.router.navigate([ '/videos/recently-added' ])
220 }, undefined, this.i18n('Go to the recently added videos page')),
221 new Hotkey('g l', (event: KeyboardEvent): boolean => {
222 this.router.navigate([ '/videos/local' ])
224 }, undefined, this.i18n('Go to the local videos page')),
225 new Hotkey('g u', (event: KeyboardEvent): boolean => {
226 this.router.navigate([ '/videos/upload' ])
228 }, undefined, this.i18n('Go to the videos upload page')),
229 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
230 this.themeService.toggleDarkTheme()
232 }, undefined, this.i18n('Toggle Dark theme'))