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