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