]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit, ViewChild } 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 { filter, map, pairwise } from 'rxjs/operators' | |
8 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' | |
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
10 | import { PlatformLocation, ViewportScroller } from '@angular/common' | |
11 | import { PluginService } from '@app/core/plugins/plugin.service' | |
12 | import { HooksService } from '@app/core/plugins/hooks.service' | |
13 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
14 | import { POP_STATE_MODAL_DISMISS } from '@app/shared/misc/constants' | |
15 | import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' | |
16 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' | |
17 | import { ServerConfig, UserRole } from '@shared/models' | |
18 | import { User } from '@app/shared' | |
19 | import { InstanceService } from '@app/shared/instance/instance.service' | |
20 | import { MenuService } from './core/menu/menu.service' | |
21 | ||
22 | @Component({ | |
23 | selector: 'my-app', | |
24 | templateUrl: './app.component.html', | |
25 | styleUrls: [ './app.component.scss' ] | |
26 | }) | |
27 | export class AppComponent implements OnInit { | |
28 | @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent | |
29 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
30 | ||
31 | customCSS: SafeHtml | |
32 | ||
33 | private serverConfig: ServerConfig | |
34 | ||
35 | constructor ( | |
36 | private i18n: I18n, | |
37 | private viewportScroller: ViewportScroller, | |
38 | private router: Router, | |
39 | private authService: AuthService, | |
40 | private serverService: ServerService, | |
41 | private pluginService: PluginService, | |
42 | private instanceService: InstanceService, | |
43 | private domSanitizer: DomSanitizer, | |
44 | private redirectService: RedirectService, | |
45 | private screenService: ScreenService, | |
46 | private hotkeysService: HotkeysService, | |
47 | private themeService: ThemeService, | |
48 | private hooks: HooksService, | |
49 | private location: PlatformLocation, | |
50 | private modalService: NgbModal, | |
51 | public menu: MenuService | |
52 | ) { } | |
53 | ||
54 | get instanceName () { | |
55 | return this.serverConfig.instance.name | |
56 | } | |
57 | ||
58 | get defaultRoute () { | |
59 | return RedirectService.DEFAULT_ROUTE | |
60 | } | |
61 | ||
62 | ngOnInit () { | |
63 | document.getElementById('incompatible-browser').className += ' browser-ok' | |
64 | ||
65 | this.serverConfig = this.serverService.getTmpConfig() | |
66 | this.serverService.getConfig() | |
67 | .subscribe(config => this.serverConfig = config) | |
68 | ||
69 | this.loadPlugins() | |
70 | this.themeService.initialize() | |
71 | ||
72 | this.authService.loadClientCredentials() | |
73 | ||
74 | if (this.isUserLoggedIn()) { | |
75 | // The service will automatically redirect to the login page if the token is not valid anymore | |
76 | this.authService.refreshUserInformation() | |
77 | } | |
78 | ||
79 | this.initRouteEvents() | |
80 | this.injectJS() | |
81 | this.injectCSS() | |
82 | ||
83 | this.initHotkeys() | |
84 | ||
85 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) | |
86 | ||
87 | this.openModalsIfNeeded() | |
88 | } | |
89 | ||
90 | isUserLoggedIn () { | |
91 | return this.authService.isLoggedIn() | |
92 | } | |
93 | ||
94 | private initRouteEvents () { | |
95 | let resetScroll = true | |
96 | const eventsObs = this.router.events | |
97 | ||
98 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
99 | ||
100 | scrollEvent.subscribe(e => { | |
101 | if (e.position) { | |
102 | return this.viewportScroller.scrollToPosition(e.position) | |
103 | } | |
104 | ||
105 | if (e.anchor) { | |
106 | return this.viewportScroller.scrollToAnchor(e.anchor) | |
107 | } | |
108 | ||
109 | if (resetScroll) { | |
110 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
111 | } | |
112 | }) | |
113 | ||
114 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) | |
115 | ||
116 | // When we add the a-state parameter, we don't want to alter the scroll | |
117 | navigationEndEvent.pipe(pairwise()) | |
118 | .subscribe(([ e1, e2 ]) => { | |
119 | try { | |
120 | resetScroll = false | |
121 | ||
122 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) | |
123 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
124 | ||
125 | if (previousUrl.pathname !== nextUrl.pathname) { | |
126 | resetScroll = true | |
127 | return | |
128 | } | |
129 | ||
130 | const nextSearchParams = nextUrl.searchParams | |
131 | nextSearchParams.delete('a-state') | |
132 | ||
133 | const previousSearchParams = previousUrl.searchParams | |
134 | ||
135 | nextSearchParams.sort() | |
136 | previousSearchParams.sort() | |
137 | ||
138 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
139 | resetScroll = true | |
140 | } | |
141 | } catch (e) { | |
142 | console.error('Cannot parse URL to check next scroll.', e) | |
143 | resetScroll = true | |
144 | } | |
145 | }) | |
146 | ||
147 | navigationEndEvent.pipe( | |
148 | map(() => window.location.pathname), | |
149 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
150 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
151 | ||
152 | navigationEndEvent.subscribe(e => { | |
153 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
154 | }) | |
155 | ||
156 | eventsObs.pipe( | |
157 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
158 | filter(() => this.screenService.isInSmallView()) | |
159 | ).subscribe(() => this.menu.isMenuDisplayed = false) // User clicked on a link in the menu, change the page | |
160 | } | |
161 | ||
162 | private injectJS () { | |
163 | // Inject JS | |
164 | this.serverService.getConfig() | |
165 | .subscribe(config => { | |
166 | if (config.instance.customizations.javascript) { | |
167 | try { | |
168 | // tslint:disable:no-eval | |
169 | eval(config.instance.customizations.javascript) | |
170 | } catch (err) { | |
171 | console.error('Cannot eval custom JavaScript.', err) | |
172 | } | |
173 | } | |
174 | }) | |
175 | } | |
176 | ||
177 | private injectCSS () { | |
178 | // Inject CSS if modified (admin config settings) | |
179 | this.serverService.configReloaded | |
180 | .subscribe(() => { | |
181 | const headStyle = document.querySelector('style.custom-css-style') | |
182 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
183 | ||
184 | // We test customCSS if the admin removed the css | |
185 | if (this.customCSS || this.serverConfig.instance.customizations.css) { | |
186 | const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>' | |
187 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
188 | } | |
189 | }) | |
190 | } | |
191 | ||
192 | private async loadPlugins () { | |
193 | this.pluginService.initializePlugins() | |
194 | ||
195 | this.hooks.runAction('action:application.init', 'common') | |
196 | } | |
197 | ||
198 | private async openModalsIfNeeded () { | |
199 | this.authService.userInformationLoaded | |
200 | .pipe( | |
201 | map(() => this.authService.getUser()), | |
202 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
203 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template | |
204 | } | |
205 | ||
206 | private async _openAdminModalsIfNeeded (user: User) { | |
207 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() | |
208 | ||
209 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return | |
210 | ||
211 | this.instanceService.getAbout() | |
212 | .subscribe(about => { | |
213 | if ( | |
214 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || | |
215 | !about.instance.terms || | |
216 | !about.instance.administrator || | |
217 | !about.instance.maintenanceLifetime | |
218 | ) { | |
219 | this.instanceConfigWarningModal.show(about) | |
220 | } | |
221 | }) | |
222 | } | |
223 | ||
224 | private initHotkeys () { | |
225 | this.hotkeysService.add([ | |
226 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { | |
227 | document.getElementById('search-video').focus() | |
228 | return false | |
229 | }, undefined, this.i18n('Focus the search bar')), | |
230 | ||
231 | new Hotkey('b', (event: KeyboardEvent): boolean => { | |
232 | this.menu.toggleMenu() | |
233 | return false | |
234 | }, undefined, this.i18n('Toggle the left menu')), | |
235 | ||
236 | new Hotkey('g o', (event: KeyboardEvent): boolean => { | |
237 | this.router.navigate([ '/videos/overview' ]) | |
238 | return false | |
239 | }, undefined, this.i18n('Go to the discover videos page')), | |
240 | ||
241 | new Hotkey('g t', (event: KeyboardEvent): boolean => { | |
242 | this.router.navigate([ '/videos/trending' ]) | |
243 | return false | |
244 | }, undefined, this.i18n('Go to the trending videos page')), | |
245 | ||
246 | new Hotkey('g r', (event: KeyboardEvent): boolean => { | |
247 | this.router.navigate([ '/videos/recently-added' ]) | |
248 | return false | |
249 | }, undefined, this.i18n('Go to the recently added videos page')), | |
250 | ||
251 | new Hotkey('g l', (event: KeyboardEvent): boolean => { | |
252 | this.router.navigate([ '/videos/local' ]) | |
253 | return false | |
254 | }, undefined, this.i18n('Go to the local videos page')), | |
255 | ||
256 | new Hotkey('g u', (event: KeyboardEvent): boolean => { | |
257 | this.router.navigate([ '/videos/upload' ]) | |
258 | return false | |
259 | }, undefined, this.i18n('Go to the videos upload page')) | |
260 | ]) | |
261 | } | |
262 | } |