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'
12 import { PluginService } from '@app/core/plugins/plugin.service'
16 templateUrl: './app.component.html',
17 styleUrls: [ './app.component.scss' ]
19 export class AppComponent implements OnInit {
20 isMenuDisplayed = true
21 isMenuChangedByUser = false
27 private viewportScroller: ViewportScroller,
28 private router: Router,
29 private authService: AuthService,
30 private serverService: ServerService,
31 private pluginService: PluginService,
32 private domSanitizer: DomSanitizer,
33 private redirectService: RedirectService,
34 private screenService: ScreenService,
35 private hotkeysService: HotkeysService,
36 private themeService: ThemeService
39 get serverVersion () {
40 return this.serverService.getConfig().serverVersion
44 const commit = this.serverService.getConfig().serverCommit || ''
45 return (commit !== '') ? '...' + commit : commit
49 return this.serverService.getConfig().instance.name
53 return RedirectService.DEFAULT_ROUTE
57 document.getElementById('incompatible-browser').className += ' browser-ok'
59 this.authService.loadClientCredentials()
61 if (this.isUserLoggedIn()) {
62 // The service will automatically redirect to the login page if the token is not valid anymore
63 this.authService.refreshUserInformation()
66 // Load custom data from server
67 this.serverService.loadConfig()
68 this.serverService.loadVideoCategories()
69 this.serverService.loadVideoLanguages()
70 this.serverService.loadVideoLicences()
71 this.serverService.loadVideoPrivacies()
72 this.serverService.loadVideoPlaylistPrivacies()
76 // Do not display menu on small screens
77 if (this.screenService.isInSmallView()) {
78 this.isMenuDisplayed = false
81 this.initRouteEvents()
87 fromEvent(window, 'resize')
88 .pipe(debounceTime(200))
89 .subscribe(() => this.onResize())
93 return this.authService.isLoggedIn()
97 this.isMenuDisplayed = !this.isMenuDisplayed
98 this.isMenuChangedByUser = true
102 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
105 private initRouteEvents () {
106 let resetScroll = true
107 const eventsObs = this.router.events
109 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
110 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
112 scrollEvent.subscribe(e => {
114 return this.viewportScroller.scrollToPosition(e.position)
118 return this.viewportScroller.scrollToAnchor(e.anchor)
122 return this.viewportScroller.scrollToPosition([ 0, 0 ])
126 // When we add the a-state parameter, we don't want to alter the scroll
127 navigationEndEvent.pipe(pairwise())
128 .subscribe(([ e1, e2 ]) => {
132 const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects)
133 const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects)
135 if (previousUrl.pathname !== nextUrl.pathname) {
140 const nextSearchParams = nextUrl.searchParams
141 nextSearchParams.delete('a-state')
143 const previousSearchParams = previousUrl.searchParams
145 nextSearchParams.sort()
146 previousSearchParams.sort()
148 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
152 console.error('Cannot parse URL to check next scroll.', e)
157 navigationEndEvent.pipe(
158 map(() => window.location.pathname),
159 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
160 ).subscribe(() => this.redirectService.redirectToHomepage(true))
163 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
164 filter(() => this.screenService.isInSmallView())
165 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
168 private injectJS () {
170 this.serverService.configLoaded
172 const config = this.serverService.getConfig()
174 if (config.instance.customizations.javascript) {
176 // tslint:disable:no-eval
177 eval(config.instance.customizations.javascript)
179 console.error('Cannot eval custom JavaScript.', err)
185 private injectCSS () {
186 // Inject CSS if modified (admin config settings)
187 this.serverService.configLoaded
188 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
190 const headStyle = document.querySelector('style.custom-css-style')
191 if (headStyle) headStyle.parentNode.removeChild(headStyle)
193 const config = this.serverService.getConfig()
195 // We test customCSS if the admin removed the css
196 if (this.customCSS || config.instance.customizations.css) {
197 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
198 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
203 private async loadPlugins () {
204 this.pluginService.initializePlugins()
206 await this.pluginService.loadPluginsByScope('common')
208 this.pluginService.runHook('action:application.loaded')
211 private initHotkeys () {
212 this.hotkeysService.add([
213 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
214 document.getElementById('search-video').focus()
216 }, undefined, this.i18n('Focus the search bar')),
217 new Hotkey('b', (event: KeyboardEvent): boolean => {
220 }, undefined, this.i18n('Toggle the left menu')),
221 new Hotkey('g o', (event: KeyboardEvent): boolean => {
222 this.router.navigate([ '/videos/overview' ])
224 }, undefined, this.i18n('Go to the videos overview page')),
225 new Hotkey('g t', (event: KeyboardEvent): boolean => {
226 this.router.navigate([ '/videos/trending' ])
228 }, undefined, this.i18n('Go to the trending videos page')),
229 new Hotkey('g r', (event: KeyboardEvent): boolean => {
230 this.router.navigate([ '/videos/recently-added' ])
232 }, undefined, this.i18n('Go to the recently added videos page')),
233 new Hotkey('g l', (event: KeyboardEvent): boolean => {
234 this.router.navigate([ '/videos/local' ])
236 }, undefined, this.i18n('Go to the local videos page')),
237 new Hotkey('g u', (event: KeyboardEvent): boolean => {
238 this.router.navigate([ '/videos/upload' ])
240 }, undefined, this.i18n('Go to the videos upload page')),
241 new Hotkey('shift+t', (event: KeyboardEvent): boolean => {
242 this.themeService.toggleDarkTheme()
244 }, undefined, this.i18n('Toggle Dark theme'))