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