]>
Commit | Line | Data |
---|---|---|
1 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' | |
2 | import { forkJoin, delay } from 'rxjs' | |
3 | import { filter, first, map } from 'rxjs/operators' | |
4 | import { DOCUMENT, getLocaleDirection, PlatformLocation } 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, RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router' | |
8 | import { | |
9 | AuthService, | |
10 | MarkdownService, | |
11 | PeerTubeRouterService, | |
12 | RedirectService, | |
13 | ScreenService, | |
14 | ScrollService, | |
15 | ServerService, | |
16 | ThemeService, | |
17 | User | |
18 | } from '@app/core' | |
19 | import { HooksService } from '@app/core/plugins/hooks.service' | |
20 | import { PluginService } from '@app/core/plugins/plugin.service' | |
21 | import { AccountSetupWarningModalComponent } from '@app/modal/account-setup-warning-modal.component' | |
22 | import { CustomModalComponent } from '@app/modal/custom-modal.component' | |
23 | import { InstanceConfigWarningModalComponent } from '@app/modal/instance-config-warning-modal.component' | |
24 | import { AdminWelcomeModalComponent } from '@app/modal/admin-welcome-modal.component' | |
25 | import { NgbConfig, NgbModal } from '@ng-bootstrap/ng-bootstrap' | |
26 | import { LoadingBarService } from '@ngx-loading-bar/core' | |
27 | import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' | |
28 | import { getShortLocale } from '@shared/core-utils/i18n' | |
29 | import { BroadcastMessageLevel, HTMLServerConfig, UserRole } from '@shared/models' | |
30 | import { MenuService } from './core/menu/menu.service' | |
31 | import { POP_STATE_MODAL_DISMISS } from './helpers' | |
32 | import { InstanceService } from './shared/shared-instance' | |
33 | ||
34 | @Component({ | |
35 | selector: 'my-app', | |
36 | templateUrl: './app.component.html', | |
37 | styleUrls: [ './app.component.scss' ] | |
38 | }) | |
39 | export class AppComponent implements OnInit, AfterViewInit { | |
40 | private static BROADCAST_MESSAGE_KEY = 'app-broadcast-message-dismissed' | |
41 | ||
42 | @ViewChild('accountSetupWarningModal') accountSetupWarningModal: AccountSetupWarningModalComponent | |
43 | @ViewChild('adminWelcomeModal') adminWelcomeModal: AdminWelcomeModalComponent | |
44 | @ViewChild('instanceConfigWarningModal') instanceConfigWarningModal: InstanceConfigWarningModalComponent | |
45 | @ViewChild('customModal') customModal: CustomModalComponent | |
46 | ||
47 | customCSS: SafeHtml | |
48 | broadcastMessage: { message: string, dismissable: boolean, class: string } | null = null | |
49 | ||
50 | private serverConfig: HTMLServerConfig | |
51 | ||
52 | constructor ( | |
53 | @Inject(DOCUMENT) private document: Document, | |
54 | @Inject(LOCALE_ID) private localeId: string, | |
55 | private router: Router, | |
56 | private authService: AuthService, | |
57 | private serverService: ServerService, | |
58 | private peertubeRouter: PeerTubeRouterService, | |
59 | private pluginService: PluginService, | |
60 | private instanceService: InstanceService, | |
61 | private domSanitizer: DomSanitizer, | |
62 | private redirectService: RedirectService, | |
63 | private screenService: ScreenService, | |
64 | private hotkeysService: HotkeysService, | |
65 | private themeService: ThemeService, | |
66 | private hooks: HooksService, | |
67 | private location: PlatformLocation, | |
68 | private modalService: NgbModal, | |
69 | private markdownService: MarkdownService, | |
70 | private ngbConfig: NgbConfig, | |
71 | private loadingBar: LoadingBarService, | |
72 | private scrollService: ScrollService, | |
73 | public menu: MenuService | |
74 | ) { | |
75 | this.ngbConfig.animation = false | |
76 | } | |
77 | ||
78 | get instanceName () { | |
79 | return this.serverConfig.instance.name | |
80 | } | |
81 | ||
82 | goToDefaultRoute () { | |
83 | return this.router.navigateByUrl(this.redirectService.getDefaultRoute()) | |
84 | } | |
85 | ||
86 | ngOnInit () { | |
87 | document.getElementById('incompatible-browser').className += ' browser-ok' | |
88 | ||
89 | this.serverConfig = this.serverService.getHTMLConfig() | |
90 | ||
91 | this.hooks.runAction('action:application.init', 'common') | |
92 | this.themeService.initialize() | |
93 | ||
94 | this.authService.loadClientCredentials() | |
95 | ||
96 | if (this.isUserLoggedIn()) { | |
97 | // The service will automatically redirect to the login page if the token is not valid anymore | |
98 | this.authService.refreshUserInformation() | |
99 | } | |
100 | ||
101 | this.initRouteEvents() | |
102 | this.scrollService.enableScrollRestoration() | |
103 | ||
104 | this.injectJS() | |
105 | this.injectCSS() | |
106 | this.injectBroadcastMessage() | |
107 | ||
108 | this.serverService.configReloaded | |
109 | .subscribe(config => { | |
110 | this.serverConfig = config | |
111 | ||
112 | this.injectBroadcastMessage() | |
113 | this.injectCSS() | |
114 | ||
115 | // Don't reinject JS since it could conflict with existing one | |
116 | }) | |
117 | ||
118 | this.initHotkeys() | |
119 | ||
120 | this.location.onPopState(() => this.modalService.dismissAll(POP_STATE_MODAL_DISMISS)) | |
121 | ||
122 | this.openModalsIfNeeded() | |
123 | ||
124 | this.document.documentElement.lang = getShortLocale(this.localeId) | |
125 | this.document.documentElement.dir = getLocaleDirection(this.localeId) | |
126 | } | |
127 | ||
128 | ngAfterViewInit () { | |
129 | this.pluginService.initializeCustomModal(this.customModal) | |
130 | } | |
131 | ||
132 | getToggleTitle () { | |
133 | if (this.menu.isDisplayed()) return $localize`Close the left menu` | |
134 | ||
135 | return $localize`Open the left menu` | |
136 | } | |
137 | ||
138 | isUserLoggedIn () { | |
139 | return this.authService.isLoggedIn() | |
140 | } | |
141 | ||
142 | hideBroadcastMessage () { | |
143 | peertubeLocalStorage.setItem(AppComponent.BROADCAST_MESSAGE_KEY, this.serverConfig.broadcastMessage.message) | |
144 | ||
145 | this.broadcastMessage = null | |
146 | this.screenService.isBroadcastMessageDisplayed = false | |
147 | } | |
148 | ||
149 | private initRouteEvents () { | |
150 | const eventsObs = this.router.events | |
151 | ||
152 | // Plugin hooks | |
153 | this.peertubeRouter.getNavigationEndEvents().subscribe(e => { | |
154 | this.hooks.runAction('action:router.navigation-end', 'common', { path: e.url }) | |
155 | }) | |
156 | ||
157 | // Automatically hide/display the menu | |
158 | eventsObs.pipe( | |
159 | filter((e: Event): e is GuardsCheckStart => e instanceof GuardsCheckStart), | |
160 | filter(() => this.screenService.isInSmallView() || this.screenService.isInTouchScreen()) | |
161 | ).subscribe(() => this.menu.setMenuDisplay(false)) // User clicked on a link in the menu, change the page | |
162 | ||
163 | // Handle lazy loaded module | |
164 | eventsObs.pipe( | |
165 | filter((e: Event): e is RouteConfigLoadStart => e instanceof RouteConfigLoadStart) | |
166 | ).subscribe(() => this.loadingBar.useRef().start()) | |
167 | ||
168 | eventsObs.pipe( | |
169 | filter((e: Event): e is RouteConfigLoadEnd => e instanceof RouteConfigLoadEnd) | |
170 | ).subscribe(() => this.loadingBar.useRef().complete()) | |
171 | } | |
172 | ||
173 | private async injectBroadcastMessage () { | |
174 | this.broadcastMessage = null | |
175 | this.screenService.isBroadcastMessageDisplayed = false | |
176 | ||
177 | const messageConfig = this.serverConfig.broadcastMessage | |
178 | ||
179 | if (messageConfig.enabled) { | |
180 | // Already dismissed this message? | |
181 | if (messageConfig.dismissable && localStorage.getItem(AppComponent.BROADCAST_MESSAGE_KEY) === messageConfig.message) { | |
182 | return | |
183 | } | |
184 | ||
185 | const classes: { [id in BroadcastMessageLevel]: string } = { | |
186 | info: 'alert-info', | |
187 | warning: 'alert-warning', | |
188 | error: 'alert-danger' | |
189 | } | |
190 | ||
191 | this.broadcastMessage = { | |
192 | message: await this.markdownService.unsafeMarkdownToHTML(messageConfig.message, true), | |
193 | dismissable: messageConfig.dismissable, | |
194 | class: classes[messageConfig.level] | |
195 | } | |
196 | ||
197 | this.screenService.isBroadcastMessageDisplayed = true | |
198 | } | |
199 | } | |
200 | ||
201 | private injectJS () { | |
202 | // Inject JS | |
203 | if (this.serverConfig.instance.customizations.javascript) { | |
204 | try { | |
205 | /* eslint-disable no-eval */ | |
206 | eval(this.serverConfig.instance.customizations.javascript) | |
207 | } catch (err) { | |
208 | console.error('Cannot eval custom JavaScript.', err) | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | private injectCSS () { | |
214 | const headStyle = document.querySelector('style.custom-css-style') | |
215 | if (headStyle) headStyle.parentNode.removeChild(headStyle) | |
216 | ||
217 | // We test customCSS if the admin removed the css | |
218 | if (this.customCSS || this.serverConfig.instance.customizations.css) { | |
219 | const styleTag = '<style>' + this.serverConfig.instance.customizations.css + '</style>' | |
220 | this.customCSS = this.domSanitizer.bypassSecurityTrustHtml(styleTag) | |
221 | } | |
222 | } | |
223 | ||
224 | private openModalsIfNeeded () { | |
225 | const userSub = this.authService.userInformationLoaded | |
226 | .pipe( | |
227 | delay(0), // Wait for modals creations | |
228 | map(() => this.authService.getUser()) | |
229 | ) | |
230 | ||
231 | // Admin modal | |
232 | userSub.pipe( | |
233 | filter(user => user.role === UserRole.ADMINISTRATOR) | |
234 | ).subscribe(user => this.openAdminModalsIfNeeded(user)) | |
235 | ||
236 | // Account modal | |
237 | userSub.pipe( | |
238 | filter(user => user.role !== UserRole.ADMINISTRATOR) | |
239 | ).subscribe(user => this.openAccountModalsIfNeeded(user)) | |
240 | } | |
241 | ||
242 | private openAdminModalsIfNeeded (user: User) { | |
243 | if (this.adminWelcomeModal.shouldOpen(user)) { | |
244 | return this.adminWelcomeModal.show() | |
245 | } | |
246 | ||
247 | if (!this.instanceConfigWarningModal.shouldOpenByUser(user)) return | |
248 | ||
249 | forkJoin([ | |
250 | this.serverService.getConfig().pipe(first()), | |
251 | this.instanceService.getAbout().pipe(first()) | |
252 | ]).subscribe(([ config, about ]) => { | |
253 | if (this.instanceConfigWarningModal.shouldOpen(config, about)) { | |
254 | this.instanceConfigWarningModal.show(about) | |
255 | } | |
256 | }) | |
257 | } | |
258 | ||
259 | private openAccountModalsIfNeeded (user: User) { | |
260 | if (this.accountSetupWarningModal.shouldOpen(user)) { | |
261 | this.accountSetupWarningModal.show(user) | |
262 | } | |
263 | } | |
264 | ||
265 | private initHotkeys () { | |
266 | this.hotkeysService.add([ | |
267 | new Hotkey([ '/', 's' ], (event: KeyboardEvent): boolean => { | |
268 | document.getElementById('search-video').focus() | |
269 | return false | |
270 | }, undefined, $localize`Focus the search bar`), | |
271 | ||
272 | new Hotkey('b', (event: KeyboardEvent): boolean => { | |
273 | this.menu.toggleMenu() | |
274 | return false | |
275 | }, undefined, $localize`Toggle the left menu`), | |
276 | ||
277 | new Hotkey('g o', (event: KeyboardEvent): boolean => { | |
278 | this.router.navigate([ '/videos/overview' ]) | |
279 | return false | |
280 | }, undefined, $localize`Go to the discover videos page`), | |
281 | ||
282 | new Hotkey('g t', (event: KeyboardEvent): boolean => { | |
283 | this.router.navigate([ '/videos/trending' ]) | |
284 | return false | |
285 | }, undefined, $localize`Go to the trending videos page`), | |
286 | ||
287 | new Hotkey('g r', (event: KeyboardEvent): boolean => { | |
288 | this.router.navigate([ '/videos/recently-added' ]) | |
289 | return false | |
290 | }, undefined, $localize`Go to the recently added videos page`), | |
291 | ||
292 | new Hotkey('g l', (event: KeyboardEvent): boolean => { | |
293 | this.router.navigate([ '/videos/local' ]) | |
294 | return false | |
295 | }, undefined, $localize`Go to the local videos page`), | |
296 | ||
297 | new Hotkey('g u', (event: KeyboardEvent): boolean => { | |
298 | this.router.navigate([ '/videos/upload' ]) | |
299 | return false | |
300 | }, undefined, $localize`Go to the videos upload page`) | |
301 | ]) | |
302 | } | |
303 | } |