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()
75 this.themeService.initialize()
77 // Do not display menu on small screens
78 if (this.screenService.isInSmallView()) {
79 this.isMenuDisplayed = false
82 this.initRouteEvents()
88 fromEvent(window, 'resize')
89 .pipe(debounceTime(200))
90 .subscribe(() => this.onResize())
94 return this.authService.isLoggedIn()
98 this.isMenuDisplayed = !this.isMenuDisplayed
99 this.isMenuChangedByUser = true
103 this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser
106 private initRouteEvents () {
107 let resetScroll = true
108 const eventsObs = this.router.events
110 const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll))
111 const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd))
113 scrollEvent.subscribe(e => {
115 return this.viewportScroller.scrollToPosition(e.position)
119 return this.viewportScroller.scrollToAnchor(e.anchor)
123 return this.viewportScroller.scrollToPosition([ 0, 0 ])
127 // When we add the a-state parameter, we don't want to alter the scroll
128 navigationEndEvent.pipe(pairwise())
129 .subscribe(([ e1, e2 ]) => {
133 const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects)
134 const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects)
136 if (previousUrl.pathname !== nextUrl.pathname) {
141 const nextSearchParams = nextUrl.searchParams
142 nextSearchParams.delete('a-state')
144 const previousSearchParams = previousUrl.searchParams
146 nextSearchParams.sort()
147 previousSearchParams.sort()
149 if (nextSearchParams.toString() !== previousSearchParams.toString()) {
153 console.error('Cannot parse URL to check next scroll.', e)
158 navigationEndEvent.pipe(
159 map(() => window.location.pathname),
160 filter(pathname => !pathname || pathname === '/' || is18nPath(pathname))
161 ).subscribe(() => this.redirectService.redirectToHomepage(true))
164 filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart),
165 filter(() => this.screenService.isInSmallView())
166 ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page
169 private injectJS () {
171 this.serverService.configLoaded
173 const config = this.serverService.getConfig()
175 if (config.instance.customizations.javascript) {
177 // tslint:disable:no-eval
178 eval(config.instance.customizations.javascript)
180 console.error('Cannot eval custom JavaScript.', err)
186 private injectCSS () {
187 // Inject CSS if modified (admin config settings)
188 this.serverService.configLoaded
189 .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server
191 const headStyle = document.querySelector('style.custom-css-style')
192 if (headStyle) headStyle.parentNode.removeChild(headStyle)
194 const config = this.serverService.getConfig()
196 // We test customCSS if the admin removed the css
197 if (this.customCSS || config.instance.customizations.css) {
198 const styleTag = '<style>' + config.instance.customizations.css + '</style>'
199 this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag)
204 private async loadPlugins () {
205 this.pluginService.initializePlugins()
207 await this.pluginService.loadPluginsByScope('common')
209 this.pluginService.runHook('action:application.loaded')
212 private initHotkeys () {
213 this.hotkeysService.add([
214 new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => {
215 document.getElementById('search-video').focus()
217 }, undefined, this.i18n('Focus the search bar')),
218 new Hotkey('b', (event: KeyboardEvent): boolean => {
221 }, undefined, this.i18n('Toggle the left menu')),
222 new Hotkey('g o', (event: KeyboardEvent): boolean => {
223 this.router.navigate([ '/videos/overview' ])
225 }, undefined, this.i18n('Go to the videos overview page')),
226 new Hotkey('g t', (event: KeyboardEvent): boolean => {
227 this.router.navigate([ '/videos/trending' ])
229 }, undefined, this.i18n('Go to the trending videos page')),
230 new Hotkey('g r', (event: KeyboardEvent): boolean => {
231 this.router.navigate([ '/videos/recently-added' ])
233 }, undefined, this.i18n('Go to the recently added videos page')),
234 new Hotkey('g l', (event: KeyboardEvent): boolean => {
235 this.router.navigate([ '/videos/local' ])
237 }, undefined, this.i18n('Go to the local videos page')),
238 new Hotkey('g u', (event: KeyboardEvent): boolean => {
239 this.router.navigate([ '/videos/upload' ])
241 }, undefined, this.i18n('Go to the videos upload page'))