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