]>
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 | ||
36f9424f C |
48 | get instanceName () { |
49 | return this.serverService.getConfig().instance.name | |
50 | } | |
51 | ||
29f9b562 C |
52 | get defaultRoute () { |
53 | return RedirectService.DEFAULT_ROUTE | |
54 | } | |
55 | ||
df98563e | 56 | ngOnInit () { |
b3eeb529 | 57 | document.getElementById('incompatible-browser').className += ' browser-ok' |
73e09f27 | 58 | |
b851dabf C |
59 | this.router.events.subscribe(e => { |
60 | if (e instanceof NavigationEnd) { | |
61 | const pathname = window.location.pathname | |
989e526a | 62 | if (!pathname || pathname === '/' || is18nPath(pathname)) { |
7cf26f43 | 63 | this.redirectService.redirectToHomepage(true) |
b851dabf C |
64 | } |
65 | } | |
66 | }) | |
901637bb | 67 | |
d592e0a9 C |
68 | this.authService.loadClientCredentials() |
69 | ||
d414207f | 70 | if (this.isUserLoggedIn()) { |
e2a2d6c8 | 71 | // The service will automatically redirect to the login page if the token is not valid anymore |
bcd9f81e | 72 | this.authService.refreshUserInformation() |
e2a2d6c8 | 73 | } |
6e07c3de | 74 | |
db7af09b C |
75 | // Load custom data from server |
76 | this.serverService.loadConfig() | |
77 | this.serverService.loadVideoCategories() | |
78 | this.serverService.loadVideoLanguages() | |
79 | this.serverService.loadVideoLicences() | |
fd45e8f4 | 80 | this.serverService.loadVideoPrivacies() |
3eeeb87f C |
81 | |
82 | // Do not display menu on small screens | |
bbe0f064 | 83 | if (this.screenService.isInSmallView()) { |
df98563e | 84 | this.isMenuDisplayed = false |
3eeeb87f | 85 | } |
c8cf5952 C |
86 | |
87 | this.router.events.subscribe( | |
88 | e => { | |
89 | // User clicked on a link in the menu, change the page | |
bbe0f064 | 90 | if (e instanceof GuardsCheckStart && this.screenService.isInSmallView()) { |
c8cf5952 C |
91 | this.isMenuDisplayed = false |
92 | } | |
93 | } | |
94 | ) | |
00b5556c | 95 | |
e032aec9 | 96 | // Inject JS |
00b5556c | 97 | this.serverService.configLoaded |
e032aec9 C |
98 | .subscribe(() => { |
99 | const config = this.serverService.getConfig() | |
100 | ||
101 | if (config.instance.customizations.javascript) { | |
102 | try { | |
103 | // tslint:disable:no-eval | |
104 | eval(config.instance.customizations.javascript) | |
105 | } catch (err) { | |
106 | console.error('Cannot eval custom JavaScript.', err) | |
107 | } | |
108 | } | |
109 | }) | |
00b5556c | 110 | |
e032aec9 C |
111 | // Inject CSS if modified (admin config settings) |
112 | this.serverService.configLoaded | |
113 | .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server | |
114 | .subscribe(() => { | |
115 | const headStyle = document.querySelector('style.custom-css-style') | |
116 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
00b5556c | 117 | |
e032aec9 C |
118 | const config = this.serverService.getConfig() |
119 | ||
120 | // We test customCSS if the admin removed the css | |
121 | if (this.customCSS || config.instance.customizations.css) { | |
122 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
123 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
00b5556c | 124 | } |
e032aec9 | 125 | }) |
ee1fc23a RK |
126 | |
127 | this.hotkeysService.add([ | |
8542dc33 | 128 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { |
ee1fc23a | 129 | document.getElementById('search-video').focus() |
8542dc33 | 130 | return false |
ee1fc23a | 131 | }, undefined, 'Focus the search bar'), |
8542dc33 RK |
132 | new Hotkey('b', (event: KeyboardEvent): boolean => { |
133 | this.toggleMenu() | |
134 | return false | |
135 | }, undefined, 'Toggle the left menu'), | |
20d21199 | 136 | new Hotkey('g s', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
137 | this.router.navigate([ '/videos/subscriptions' ]) |
138 | return false | |
139 | }, undefined, 'Go to the subscriptions videos page'), | |
20d21199 | 140 | new Hotkey('g o', (event: KeyboardEvent): boolean => { |
a54991da RK |
141 | this.router.navigate([ '/videos/overview' ]) |
142 | return false | |
143 | }, undefined, 'Go to the videos overview page'), | |
20d21199 | 144 | new Hotkey('g t', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
145 | this.router.navigate([ '/videos/trending' ]) |
146 | return false | |
147 | }, undefined, 'Go to the trending videos page'), | |
20d21199 | 148 | new Hotkey('g r', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
149 | this.router.navigate([ '/videos/recently-added' ]) |
150 | return false | |
151 | }, undefined, 'Go to the recently added videos page'), | |
20d21199 | 152 | new Hotkey('g l', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
153 | this.router.navigate([ '/videos/local' ]) |
154 | return false | |
155 | }, undefined, 'Go to the local videos page'), | |
20d21199 | 156 | new Hotkey('g u', (event: KeyboardEvent): boolean => { |
ee1fc23a RK |
157 | this.router.navigate([ '/videos/upload' ]) |
158 | return false | |
1a00c561 RK |
159 | }, undefined, 'Go to the videos upload page'), |
160 | new Hotkey('T', (event: KeyboardEvent): boolean => { | |
161 | this.themeService.toggleDarkTheme() | |
162 | return false | |
163 | }, undefined, 'Toggle Dark theme') | |
ee1fc23a | 164 | ]) |
e2a2d6c8 C |
165 | } |
166 | ||
d414207f C |
167 | isUserLoggedIn () { |
168 | return this.authService.isLoggedIn() | |
169 | } | |
170 | ||
df98563e C |
171 | toggleMenu () { |
172 | this.isMenuDisplayed = !this.isMenuDisplayed | |
67167390 | 173 | } |
dc8bc31b | 174 | } |