]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { DomSanitizer, SafeHtml } from '@angular/platform-browser' | |
3 | import { GuardsCheckStart, Router, NavigationEnd } from '@angular/router' | |
4 | import { AuthService, RedirectService, ServerService } from '@app/core' | |
5 | import { isInSmallView } from '@app/shared/misc/utils' | |
6 | ||
7 | @Component({ | |
8 | selector: 'my-app', | |
9 | templateUrl: './app.component.html', | |
10 | styleUrls: [ './app.component.scss' ] | |
11 | }) | |
12 | export class AppComponent implements OnInit { | |
13 | notificationOptions = { | |
14 | timeOut: 5000, | |
15 | lastOnBottom: true, | |
16 | clickToClose: true, | |
17 | maxLength: 0, | |
18 | maxStack: 7, | |
19 | showProgressBar: false, | |
20 | pauseOnHover: false, | |
21 | preventDuplicates: false, | |
22 | preventLastDuplicates: 'visible', | |
23 | rtl: false | |
24 | } | |
25 | ||
26 | isMenuDisplayed = true | |
27 | ||
28 | customCSS: SafeHtml | |
29 | ||
30 | constructor ( | |
31 | private router: Router, | |
32 | private authService: AuthService, | |
33 | private serverService: ServerService, | |
34 | private domSanitizer: DomSanitizer, | |
35 | private redirectService: RedirectService | |
36 | ) {} | |
37 | ||
38 | get serverVersion () { | |
39 | return this.serverService.getConfig().serverVersion | |
40 | } | |
41 | ||
42 | get instanceName () { | |
43 | return this.serverService.getConfig().instance.name | |
44 | } | |
45 | ||
46 | get defaultRoute () { | |
47 | return RedirectService.DEFAULT_ROUTE | |
48 | } | |
49 | ||
50 | ngOnInit () { | |
51 | document.getElementById('incompatible-browser').className += ' browser-ok' | |
52 | ||
53 | this.router.events.subscribe(e => { | |
54 | if (e instanceof NavigationEnd) { | |
55 | const pathname = window.location.pathname | |
56 | if (!pathname || pathname === '/') { | |
57 | this.redirectService.redirectToHomepage() | |
58 | } | |
59 | } | |
60 | }) | |
61 | ||
62 | this.authService.loadClientCredentials() | |
63 | ||
64 | if (this.isUserLoggedIn()) { | |
65 | // The service will automatically redirect to the login page if the token is not valid anymore | |
66 | this.authService.refreshUserInformation() | |
67 | } | |
68 | ||
69 | // Load custom data from server | |
70 | this.serverService.loadConfig() | |
71 | this.serverService.loadVideoCategories() | |
72 | this.serverService.loadVideoLanguages() | |
73 | this.serverService.loadVideoLicences() | |
74 | this.serverService.loadVideoPrivacies() | |
75 | ||
76 | // Do not display menu on small screens | |
77 | if (isInSmallView()) { | |
78 | this.isMenuDisplayed = false | |
79 | } | |
80 | ||
81 | this.router.events.subscribe( | |
82 | e => { | |
83 | // User clicked on a link in the menu, change the page | |
84 | if (e instanceof GuardsCheckStart && isInSmallView()) { | |
85 | this.isMenuDisplayed = false | |
86 | } | |
87 | } | |
88 | ) | |
89 | ||
90 | this.serverService.configLoaded | |
91 | .subscribe(() => { | |
92 | const config = this.serverService.getConfig() | |
93 | ||
94 | // We test customCSS if the admin removed the css | |
95 | if (this.customCSS || config.instance.customizations.css) { | |
96 | const styleTag = '<style>' + config.instance.customizations.css + '</style>' | |
97 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
98 | } | |
99 | ||
100 | if (config.instance.customizations.javascript) { | |
101 | try { | |
102 | // tslint:disable:no-eval | |
103 | eval(config.instance.customizations.javascript) | |
104 | } catch (err) { | |
105 | console.error('Cannot eval custom JavaScript.', err) | |
106 | } | |
107 | } | |
108 | }) | |
109 | } | |
110 | ||
111 | isUserLoggedIn () { | |
112 | return this.authService.isLoggedIn() | |
113 | } | |
114 | ||
115 | toggleMenu () { | |
116 | window.scrollTo(0, 0) | |
117 | this.isMenuDisplayed = !this.isMenuDisplayed | |
118 | } | |
119 | } |