]>
Commit | Line | Data |
---|---|---|
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' | |
2 | import { concat } from 'rxjs' | |
3 | import { filter, first, map, pairwise } from 'rxjs/operators' | |
4 | import { DOCUMENT, PlatformLocation, ViewportScroller } from '@angular/common' | |
5 | import { AfterViewInit, Component, Inject, LOCALE_ID, OnInit, ViewChild } from '@angular/core' | |
6 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' | |
7 | import { Event, GuardsCheckStart, NavigationEnd, Router, Scroll } from '@angular/router' | |
8 | import { AuthService, MarkdownService, RedirectService, ScreenService, ServerService, ThemeService, User } from '@app/core' | |
9 | import { HooksService } from '@app/core/plugins/hooks.service' | |
10 | import { PluginService } from '@app/core/plugins/plugin.service' | |
11 | import { CustomModalComponent } from '@app/modal/custom-modal.component' | |
12 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' | |
13 | import { WelcomeModalComponent } from '@app/modal/welcome-modal.component' | |
14 | import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
15 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | |
16 | import { getShortLocale, is18nPath } from '@shared/core-utils/i18n' | |
17 | import { BroadcastMessageLevel, ServerConfig, UserRole } from '@shared/models' | |
18 | import { MenuService } from './core/menu/menu.service' | |
19 | import { POP_STATE_MODAL_DISMISS } from './helpers' | |
20 | import { InstanceService } from './shared/shared-instance' | |
21 | ||
22 | @Component({ | |
23 | selector: 'my-app', | |
24 | templateUrl: './app.component.html', | |
25 | styleUrls: [ './app.component.scss' ] | |
26 | }) | |
27 | export class AppComponent implements OnInit, AfterViewInit { | |
28 | private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' | |
29 | ||
30 | @ViewChild('welcomeModal') welcomeModal: WelcomeModalComponent | |
31 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
32 | @ViewChild('customModal') customModal: CustomModalComponent | |
33 | ||
34 | customCSS: SafeHtml | |
35 | broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null | |
36 | ||
37 | private serverConfig: ServerConfig | |
38 | ||
39 | constructor ( | |
40 | @Inject(DOCUMENT) private document: Document, | |
41 | @Inject(LOCALE_ID) private localeId: string, | |
42 | private viewportScroller: ViewportScroller, | |
43 | private router: Router, | |
44 | private authService: AuthService, | |
45 | private serverService: ServerService, | |
46 | private pluginService: PluginService, | |
47 | private instanceService: InstanceService, | |
48 | private domSanitizer: DomSanitizer, | |
49 | private redirectService: RedirectService, | |
50 | private screenService: ScreenService, | |
51 | private hotkeysService: HotkeysService, | |
52 | private themeService: ThemeService, | |
53 | private hooks: HooksService, | |
54 | private location: PlatformLocation, | |
55 | private modalService: NgbModal, | |
56 | private markdownService: MarkdownService, | |
57 | private ngbConfig: NgbConfig, | |
58 | public menu: MenuService | |
59 | ) { | |
60 | this.ngbConfig.animation = false | |
61 | } | |
62 | ||
63 | get instanceName () { | |
64 | return this.serverConfig.instance.name | |
65 | } | |
66 | ||
67 | get defaultRoute () { | |
68 | return RedirectService.DEFAULT_ROUTE | |
69 | } | |
70 | ||
71 | ngOnInit () { | |
72 | document.getElementById('incompatible-browser').className += ' browser-ok' | |
73 | ||
74 | this.serverConfig = this.serverService.getTmpConfig() | |
75 | this.serverService.getConfig() | |
76 | .subscribe(config => this.serverConfig = config) | |
77 | ||
78 | this.loadPlugins() | |
79 | this.themeService.initialize() | |
80 | ||
81 | this.authService.loadClientCredentials() | |
82 | ||
83 | if (this.isUserLoggedIn()) { | |
84 | // The service will automatically redirect to the login page if the token is not valid anymore | |
85 | this.authService.refreshUserInformation() | |
86 | } | |
87 | ||
88 | this.initRouteEvents() | |
89 | this.injectJS() | |
90 | this.injectCSS() | |
91 | this.injectBroadcastMessage() | |
92 | ||
93 | this.initHotkeys() | |
94 | ||
95 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) | |
96 | ||
97 | this.openModalsIfNeeded() | |
98 | ||
99 | this.document.documentElement.lang = getShortLocale(this.localeId) | |
100 | } | |
101 | ||
102 | ngAfterViewInit () { | |
103 | this.pluginService.initializeCustomModal(this.customModal) | |
104 | } | |
105 | ||
106 | isUserLoggedIn () { | |
107 | return this.authService.isLoggedIn() | |
108 | } | |
109 | ||
110 | hideBroadcastMessage () { | |
111 | peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message) | |
112 | ||
113 | this.broadcastMessage = null | |
114 | this.screenService.isBroadcastMessageDisplayed = false | |
115 | } | |
116 | ||
117 | private initRouteEvents () { | |
118 | let resetScroll = true | |
119 | const eventsObs = this.router.events | |
120 | ||
121 | const scrollEvent = eventsObs.pipe(filter((e: Event): e is Scroll => e instanceof Scroll)) | |
122 | ||
123 | scrollEvent.subscribe(e => { | |
124 | // scrollToAnchor first to preserve anchor position when using history navigation | |
125 | if (e.anchor) { | |
126 | setTimeout(() => { | |
127 | this.viewportScroller.scrollToAnchor(e.anchor) | |
128 | }) | |
129 | ||
130 | return | |
131 | } | |
132 | ||
133 | if (e.position) { | |
134 | return this.viewportScroller.scrollToPosition(e.position) | |
135 | } | |
136 | ||
137 | if (resetScroll) { | |
138 | return this.viewportScroller.scrollToPosition([ 0, 0 ]) | |
139 | } | |
140 | }) | |
141 | ||
142 | const navigationEndEvent = eventsObs.pipe(filter((e: Event): e is NavigationEnd => e instanceof NavigationEnd)) | |
143 | ||
144 | // When we add the a-state parameter, we don't want to alter the scroll | |
145 | navigationEndEvent.pipe(pairwise()) | |
146 | .subscribe(([ e1, e2 ]) => { | |
147 | try { | |
148 | resetScroll = false | |
149 | ||
150 | const previousUrl = new URL(window.location.origin + e1.urlAfterRedirects) | |
151 | const nextUrl = new URL(window.location.origin + e2.urlAfterRedirects) | |
152 | ||
153 | if (previousUrl.pathname !== nextUrl.pathname) { | |
154 | resetScroll = true | |
155 | return | |
156 | } | |
157 | ||
158 | const nextSearchParams = nextUrl.searchParams | |
159 | nextSearchParams.delete('a-state') | |
160 | ||
161 | const previousSearchParams = previousUrl.searchParams | |
162 | ||
163 | nextSearchParams.sort() | |
164 | previousSearchParams.sort() | |
165 | ||
166 | if (nextSearchParams.toString() !== previousSearchParams.toString()) { | |
167 | resetScroll = true | |
168 | } | |
169 | } catch (e) { | |
170 | console.error('Cannot parse URL to check next scroll.', e) | |
171 | resetScroll = true | |
172 | } | |
173 | }) | |
174 | ||
175 | navigationEndEvent.pipe( | |
176 | map(() => window.location.pathname), | |
177 | filter(pathname => !pathname || pathname === '/' || is18nPath(pathname)) | |
178 | ).subscribe(() => this.redirectService.redirectToHomepage(true)) | |
179 | ||
180 | navigationEndEvent.subscribe(e => { | |
181 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
182 | }) | |
183 | ||
184 | eventsObs.pipe( | |
185 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
186 | filter(() => this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) | |
187 | ).subscribe(() => this.menu.setMenuDisplay(false)) // User clicked on a link in the menu, change the page | |
188 | } | |
189 | ||
190 | private injectBroadcastMessage () { | |
191 | concat( | |
192 | this.serverService.getConfig().pipe(first()), | |
193 | this.serverService.configReloaded | |
194 | ).subscribe(async config => { | |
195 | this.broadcastMessage = null | |
196 | this.screenService.isBroadcastMessageDisplayed = false | |
197 | ||
198 | const messageConfig = config.broadcastMessage | |
199 | ||
200 | if (messageConfig.enabled) { | |
201 | // Already dismissed this message? | |
202 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
203 | return | |
204 | } | |
205 | ||
206 | const classes: { [id in BroadcastMessageLevel]: string } = { | |
207 | info: 'alert-info', | |
208 | warning: 'alert-warning', | |
209 | error: 'alert-danger' | |
210 | } | |
211 | ||
212 | this.broadcastMessage = { | |
213 | message: await this.markdownService.completeMarkdownToHTML(messageConfig.message), | |
214 | dismissable: messageConfig.dismissable, | |
215 | class: classes[messageConfig.level] | |
216 | } | |
217 | ||
218 | this.screenService.isBroadcastMessageDisplayed = true | |
219 | } | |
220 | }) | |
221 | } | |
222 | ||
223 | private injectJS () { | |
224 | // Inject JS | |
225 | this.serverService.getConfig() | |
226 | .subscribe(config => { | |
227 | if (config.instance.customizations.javascript) { | |
228 | try { | |
229 | // tslint:disable:no-eval | |
230 | eval(config.instance.customizations.javascript) | |
231 | } catch (err) { | |
232 | console.error('Cannot eval custom JavaScript.', err) | |
233 | } | |
234 | } | |
235 | }) | |
236 | } | |
237 | ||
238 | private injectCSS () { | |
239 | // Inject CSS if modified (admin config settings) | |
240 | concat( | |
241 | this.serverService.getConfig().pipe(first()), | |
242 | this.serverService.configReloaded | |
243 | ).subscribe(config => { | |
244 | const headStyle = document.querySelector('style.custom-css-style') | |
245 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
246 | ||
247 | // We test customCSS if the admin removed the css | |
248 | if (this.customCSS || config.instance.customizations.css) { | |
249 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
250 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
251 | } | |
252 | }) | |
253 | } | |
254 | ||
255 | private async loadPlugins () { | |
256 | this.pluginService.initializePlugins() | |
257 | ||
258 | this.hooks.runAction('action:application.init', 'common') | |
259 | } | |
260 | ||
261 | private async openModalsIfNeeded () { | |
262 | this.authService.userInformationLoaded | |
263 | .pipe( | |
264 | map(() => this.authService.getUser()), | |
265 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
266 | ).subscribe(user => setTimeout(() => this._openAdminModalsIfNeeded(user))) // setTimeout because of ngIf in template | |
267 | } | |
268 | ||
269 | private async _openAdminModalsIfNeeded (user: User) { | |
270 | if (user.noWelcomeModal !== true) return this.welcomeModal.show() | |
271 | ||
272 | if (user.noInstanceConfigWarningModal === true || !this.serverConfig.signup.allowed) return | |
273 | ||
274 | this.instanceService.getAbout() | |
275 | .subscribe(about => { | |
276 | if ( | |
277 | this.serverConfig.instance.name.toLowerCase() === 'peertube' || | |
278 | !about.instance.terms || | |
279 | !about.instance.administrator || | |
280 | !about.instance.maintenanceLifetime | |
281 | ) { | |
282 | this.instanceConfigWarningModal.show(about) | |
283 | } | |
284 | }) | |
285 | } | |
286 | ||
287 | private initHotkeys () { | |
288 | this.hotkeysService.add([ | |
289 | new Hotkey(['/', 's'], (event: KeyboardEvent): boolean => { | |
290 | document.getElementById('search-video').focus() | |
291 | return false | |
292 | }, undefined, $localize`Focus the search bar`), | |
293 | ||
294 | new Hotkey('b', (event: KeyboardEvent): boolean => { | |
295 | this.menu.toggleMenu() | |
296 | return false | |
297 | }, undefined, $localize`Toggle the left menu`), | |
298 | ||
299 | new Hotkey('g o', (event: KeyboardEvent): boolean => { | |
300 | this.router.navigate([ '/videos/overview' ]) | |
301 | return false | |
302 | }, undefined, $localize`Go to the discover videos page`), | |
303 | ||
304 | new Hotkey('g t', (event: KeyboardEvent): boolean => { | |
305 | this.router.navigate([ '/videos/trending' ]) | |
306 | return false | |
307 | }, undefined, $localize`Go to the trending videos page`), | |
308 | ||
309 | new Hotkey('g r', (event: KeyboardEvent): boolean => { | |
310 | this.router.navigate([ '/videos/recently-added' ]) | |
311 | return false | |
312 | }, undefined, $localize`Go to the recently added videos page`), | |
313 | ||
314 | new Hotkey('g l', (event: KeyboardEvent): boolean => { | |
315 | this.router.navigate([ '/videos/local' ]) | |
316 | return false | |
317 | }, undefined, $localize`Go to the local videos page`), | |
318 | ||
319 | new Hotkey('g u', (event: KeyboardEvent): boolean => { | |
320 | this.router.navigate([ '/videos/upload' ]) | |
321 | return false | |
322 | }, undefined, $localize`Go to the videos upload page`) | |
323 | ]) | |
324 | } | |
325 | } |