]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | @Component({ | |
14 | selector: 'my-app', | |
15 | templateUrl: './app.component.html', | |
16 | styleUrls: [ './app.component.scss' ] | |
17 | }) | |
18 | export class AppComponent implements OnInit { | |
19 | isMenuDisplayed = true | |
20 | isMenuChangedByUser = false | |
21 | ||
22 | customCSS: SafeHtml | |
23 | ||
24 | constructor ( | |
25 | private i18n: I18n, | |
26 | private viewportScroller: ViewportScroller, | |
27 | private router: Router, | |
28 | private authService: AuthService, | |
29 | private serverService: ServerService, | |
30 | private domSanitizer: DomSanitizer, | |
31 | private redirectService: RedirectService, | |
32 | private screenService: ScreenService, | |
33 | private hotkeysService: HotkeysService, | |
34 | private themeService: ThemeService | |
35 | ) { } | |
36 | ||
37 | get serverVersion () { | |
38 | return this.serverService.getConfig().serverVersion | |
39 | } | |
40 | ||
41 | get serverCommit () { | |
42 | const commit = this.serverService.getConfig().serverCommit || '' | |
43 | return (commit !== '') ? '...' + commit : commit | |
44 | } | |
45 | ||
46 | get instanceName () { | |
47 | return this.serverService.getConfig().instance.name | |
48 | } | |
49 | ||
50 | get defaultRoute () { | |
51 | return RedirectService.DEFAULT_ROUTE | |
52 | } | |
53 | ||
54 | ngOnInit () { | |
55 | document.getElementById('incompatible-browser').className += ' browser-ok' | |
56 | ||
57 | this.authService.loadClientCredentials() | |
58 | ||
59 | if (this.isUserLoggedIn()) { | |
60 | // The service will automatically redirect to the login page if the token is not valid anymore | |
61 | this.authService.refreshUserInformation() | |
62 | } | |
63 | ||
64 | // Load custom data from server | |
65 | this.serverService.loadConfig() | |
66 | this.serverService.loadVideoCategories() | |
67 | this.serverService.loadVideoLanguages() | |
68 | this.serverService.loadVideoLicences() | |
69 | this.serverService.loadVideoPrivacies() | |
70 | this.serverService.loadVideoPlaylistPrivacies() | |
71 | ||
72 | // Do not display menu on small screens | |
73 | if (this.screenService.isInSmallView()) { | |
74 | this.isMenuDisplayed = false | |
75 | } | |
76 | ||
77 | this.initRouteEvents() | |
78 | this.injectJS() | |
79 | this.injectCSS() | |
80 | ||
81 | this.initHotkeys() | |
82 | ||
83 | fromEvent(window, 'resize') | |
84 | .pipe(debounceTime(200)) | |
85 | .subscribe(() => this.onResize()) | |
86 | } | |
87 | ||
88 | isUserLoggedIn () { | |
89 | return this.authService.isLoggedIn() | |
90 | } | |
91 | ||
92 | toggleMenu () { | |
93 | this.isMenuDisplayed = !this.isMenuDisplayed | |
94 | this.isMenuChangedByUser = true | |
95 | } | |
96 | ||
97 | onResize () { | |
98 | this.isMenuDisplayed = window.innerWidth >= 800 && !this.isMenuChangedByUser | |
99 | } | |
100 | ||
101 | private initRouteEvents () { | |
102 | let resetScroll = true | |
103 | const eventsObs = this.router.events | |
104 | ||
105 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
106 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) | |
107 | ||
108 | scrollEvent.subscribe(e => { | |
109 | if (e.position) { | |
110 | return this.viewportScroller.scrollToPosition(e.position) | |
111 | } | |
112 | ||
113 | if (e.anchor) { | |
114 | return this.viewportScroller.scrollToAnchor(e.anchor) | |
115 | } | |
116 | ||
117 | if (resetScroll) { | |
118 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
119 | } | |
120 | }) | |
121 | ||
122 | // When we add the a-state parameter, we don't want to alter the scroll | |
123 | navigationEndEvent.pipe(pairwise()) | |
124 | .subscribe(([ e1, e2 ]) => { | |
125 | try { | |
126 | resetScroll = false | |
127 | ||
128 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) | |
129 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
130 | ||
131 | if (previousUrl.pathname !== nextUrl.pathname) { | |
132 | resetScroll = true | |
133 | return | |
134 | } | |
135 | ||
136 | const nextSearchParams = nextUrl.searchParams | |
137 | nextSearchParams.delete('a-state') | |
138 | ||
139 | const previousSearchParams = previousUrl.searchParams | |
140 | ||
141 | nextSearchParams.sort() | |
142 | previousSearchParams.sort() | |
143 | ||
144 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
145 | resetScroll = true | |
146 | } | |
147 | } catch (e) { | |
148 | console.error('Cannot parse URL to check next scroll.', e) | |
149 | resetScroll = true | |
150 | } | |
151 | }) | |
152 | ||
153 | navigationEndEvent.pipe( | |
154 | map(() => window.location.pathname), | |
155 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
156 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
157 | ||
158 | eventsObs.pipe( | |
159 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
160 | filter(() => this.screenService.isInSmallView()) | |
161 | ).subscribe(() => this.isMenuDisplayed = false) // User clicked on a link in the menu, change the page | |
162 | } | |
163 | ||
164 | private injectJS () { | |
165 | // Inject JS | |
166 | this.serverService.configLoaded | |
167 | .subscribe(() => { | |
168 | const config = this.serverService.getConfig() | |
169 | ||
170 | if (config.instance.customizations.javascript) { | |
171 | try { | |
172 | // tslint:disable:no-eval | |
173 | eval(config.instance.customizations.javascript) | |
174 | } catch (err) { | |
175 | console.error('Cannot eval custom JavaScript.', err) | |
176 | } | |
177 | } | |
178 | }) | |
179 | } | |
180 | ||
181 | private injectCSS () { | |
182 | // Inject CSS if modified (admin config settings) | |
183 | this.serverService.configLoaded | |
184 | .pipe(skip(1)) // We only want to subscribe to reloads, because the CSS is already injected by the server | |
185 | .subscribe(() => { | |
186 | const headStyle = document.querySelector('style.custom-css-style') | |
187 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
188 | ||
189 | const config = this.serverService.getConfig() | |
190 | ||
191 | // We test customCSS if the admin removed the css | |
192 | if (this.customCSS || config.instance.customizations.css) { | |
193 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
194 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
195 | } | |
196 | }) | |
197 | } | |
198 | ||
199 | private initHotkeys () { | |
200 | this.hotkeysService.add([ | |
201 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { | |
202 | document.getElementById('search-video').focus() | |
203 | return false | |
204 | }, undefined, this.i18n('Focus the search bar')), | |
205 | new Hotkey('b', (event: KeyboardEvent): boolean => { | |
206 | this.toggleMenu() | |
207 | return false | |
208 | }, undefined, this.i18n('Toggle the left menu')), | |
209 | new Hotkey('g o', (event: KeyboardEvent): boolean => { | |
210 | this.router.navigate([ '/videos/overview' ]) | |
211 | return false | |
212 | }, undefined, this.i18n('Go to the videos overview page')), | |
213 | new Hotkey('g t', (event: KeyboardEvent): boolean => { | |
214 | this.router.navigate([ '/videos/trending' ]) | |
215 | return false | |
216 | }, undefined, this.i18n('Go to the trending videos page')), | |
217 | new Hotkey('g r', (event: KeyboardEvent): boolean => { | |
218 | this.router.navigate([ '/videos/recently-added' ]) | |
219 | return false | |
220 | }, undefined, this.i18n('Go to the recently added videos page')), | |
221 | new Hotkey('g l', (event: KeyboardEvent): boolean => { | |
222 | this.router.navigate([ '/videos/local' ]) | |
223 | return false | |
224 | }, undefined, this.i18n('Go to the local videos page')), | |
225 | new Hotkey('g u', (event: KeyboardEvent): boolean => { | |
226 | this.router.navigate([ '/videos/upload' ]) | |
227 | return false | |
228 | }, undefined, this.i18n('Go to the videos upload page')), | |
229 | new Hotkey('shift+t', (event: KeyboardEvent): boolean => { | |
230 | this.themeService.toggleDarkTheme() | |
231 | return false | |
232 | }, undefined, this.i18n('Toggle Dark theme')) | |
233 | ]) | |
234 | } | |
235 | } |