]>
Commit | Line | Data |
---|---|---|
fd45e8f4 | 1 | import { Component, OnInit } from '@angular/core' |
00b5556c | 2 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' |
989e526a | 3 | import { GuardsCheckStart, NavigationEnd, Router } from '@angular/router' |
1a00c561 | 4 | import { AuthService, RedirectService, ServerService, ThemeService } from '@app/core' |
989e526a | 5 | import { is18nPath } from '../../../shared/models/i18n' |
bbe0f064 | 6 | import { ScreenService } from '@app/shared/misc/screen.service' |
a5858241 | 7 | import { skip, debounceTime } from 'rxjs/operators' |
ee1fc23a | 8 | import { HotkeysService, Hotkey } from 'angular2-hotkeys' |
e33f888b | 9 | import { I18n } from '@ngx-translate/i18n-polyfill' |
a5858241 | 10 | import { fromEvent } from 'rxjs' |
e2a2d6c8 | 11 | |
dc8bc31b | 12 | @Component({ |
3154f382 C |
13 | selector: 'my-app', |
14 | templateUrl: './app.component.html', | |
15 | styleUrls: [ './app.component.scss' ] | |
dc8bc31b | 16 | }) |
e2a2d6c8 | 17 | export class AppComponent implements OnInit { |
df98563e | 18 | isMenuDisplayed = true |
a5858241 | 19 | isMenuChangedByUser = false |
67167390 | 20 | |
00b5556c C |
21 | customCSS: SafeHtml |
22 | ||
df98563e | 23 | constructor ( |
e33f888b | 24 | private i18n: I18n, |
3154f382 | 25 | private router: Router, |
e2a2d6c8 | 26 | private authService: AuthService, |
00b5556c | 27 | private serverService: ServerService, |
901637bb | 28 | private domSanitizer: DomSanitizer, |
bbe0f064 | 29 | private redirectService: RedirectService, |
ee1fc23a | 30 | private screenService: ScreenService, |
1a00c561 RK |
31 | private hotkeysService: HotkeysService, |
32 | private themeService: ThemeService | |
989e526a | 33 | ) { } |
a99593ed | 34 | |
915c5bbe C |
35 | get serverVersion () { |
36 | return this.serverService.getConfig().serverVersion | |
37 | } | |
38 | ||
abb2c792 RK |
39 | get serverCommit () { |
40 | const commit = this.serverService.getConfig().serverCommit || '' | |
41 | return (commit !== '') ? '...' + commit : commit | |
42 | } | |
43 | ||
36f9424f C |
44 | get instanceName () { |
45 | return this.serverService.getConfig().instance.name | |
46 | } | |
47 | ||
29f9b562 C |
48 | get defaultRoute () { |
49 | return RedirectService.DEFAULT_ROUTE | |
50 | } | |
51 | ||
df98563e | 52 | ngOnInit () { |
b3eeb529 | 53 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 54 | |
b851dabf C |
55 | this.router.events.subscribe(e => { |
56 | if (e instanceof NavigationEnd) { | |
57 | const pathname = window.location.pathname | |
989e526a | 58 | if (!pathname || pathname === '/' || is18nPath(pathname)) { |
7cf26f43 | 59 | this.redirectService.redirectToHomepage(true) |
b851dabf C |
60 | } |
61 | } | |
62 | }) | |
901637bb | 63 | |
d592e0a9 C |
64 | this.authService.loadClientCredentials() |
65 | ||
d414207f | 66 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 67 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 68 | this.authService.refreshUserInformation() |
e2a2d6c8 | 69 | } |
6e07c3de | 70 | |
db7af09b C |
71 | // Load custom data from server |
72 | this.serverService.loadConfig() | |
73 | this.serverService.loadVideoCategories() | |
74 | this.serverService.loadVideoLanguages() | |
75 | this.serverService.loadVideoLicences() | |
fd45e8f4 | 76 | this.serverService.loadVideoPrivacies() |
830b4faf | 77 | this.serverService.loadVideoPlaylistPrivacies() |
3eeeb87f C |
78 | |
79 | // Do not display menu on small screens | |
bbe0f064 | 80 | if (this.screenService.isInSmallView()) { |
df98563e | 81 | this.isMenuDisplayed = false |
3eeeb87f | 82 | } |
c8cf5952 C |
83 | |
84 | this.router.events.subscribe( | |
85 | e => { | |
86 | // User clicked on a link in the menu, change the page | |
bbe0f064 | 87 | if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) { |
c8cf5952 C |
88 | this.isMenuDisplayed = false |
89 | } | |
90 | } | |
91 | ) | |
00b5556c | 92 | |
e032aec9 | 93 | // Inject JS |
00b5556c | 94 | this.serverService.configLoaded |
e032aec9 C |
95 | .subscribe(() => { |
96 | const config = this.serverService.getConfig() | |
97 | ||
98 | if (config.instance.customizations.javascript) { | |
99 | try { | |
100 | // tslint:disable:no-eval | |
101 | eval(config.instance.customizations.javascript) | |
102 | } catch (err) { | |
103 | console.error('Cannot eval custom JavaScript.', err) | |
104 | } | |
105 | } | |
106 | }) | |
00b5556c | 107 | |
e032aec9 C |
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 | |
111 | .subscribe(() => { | |
112 | const headStyle = document.querySelector('style.custom-css-style') | |
113 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
00b5556c | 114 | |
e032aec9 C |
115 | const config = this.serverService.getConfig() |
116 | ||
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) | |
00b5556c | 121 | } |
e032aec9 | 122 | }) |
ee1fc23a RK |
123 | |
124 | this.hotkeysService.add([ | |
8542dc33 | 125 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 126 | document.getElementById('search-video').focus() |
8542dc33 | 127 | return false |
e33f888b | 128 | }, undefined, this.i18n('Focus the search bar')), |
8542dc33 RK |
129 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
130 | this.toggleMenu() | |
131 | return false | |
e33f888b | 132 | }, undefined, this.i18n('Toggle the left menu')), |
20d21199 | 133 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
134 | this.router.navigate([ '/videos/overview' ]) |
135 | return false | |
e33f888b | 136 | }, undefined, this.i18n('Go to the videos overview page')), |
20d21199 | 137 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
138 | this.router.navigate([ '/videos/trending' ]) |
139 | return false | |
e33f888b | 140 | }, undefined, this.i18n('Go to the trending videos page')), |
20d21199 | 141 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
142 | this.router.navigate([ '/videos/recently-added' ]) |
143 | return false | |
e33f888b | 144 | }, undefined, this.i18n('Go to the recently added videos page')), |
20d21199 | 145 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
146 | this.router.navigate([ '/videos/local' ]) |
147 | return false | |
e33f888b | 148 | }, undefined, this.i18n('Go to the local videos page')), |
20d21199 | 149 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
150 | this.router.navigate([ '/videos/upload' ]) |
151 | return false | |
e33f888b | 152 | }, undefined, this.i18n('Go to the videos upload page')), |
a157b3a3 | 153 | new Hotkey('shift+t', (event: KeyboardEvent): boolean => { |
1a00c561 RK |
154 | this.themeService.toggleDarkTheme() |
155 | return false | |
e33f888b | 156 | }, undefined, this.i18n('Toggle Dark theme')) |
ee1fc23a | 157 | ]) |
a5858241 B |
158 | |
159 | fromEvent(window, 'resize') | |
160 | .pipe(debounceTime(200)) | |
161 | .subscribe(() => this.onResize()) | |
e2a2d6c8 C |
162 | } |
163 | ||
d414207f C |
164 | isUserLoggedIn () { |
165 | return this.authService.isLoggedIn() | |
166 | } | |
167 | ||
df98563e C |
168 | toggleMenu () { |
169 | this.isMenuDisplayed = !this.isMenuDisplayed | |
a5858241 B |
170 | this.isMenuChangedByUser = true |
171 | } | |
172 | ||
173 | onResize () { | |
174 | this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser | |
67167390 | 175 | } |
dc8bc31b | 176 | } |