funky-framadate-front/src/app/app.component.ts

90 lines
2.5 KiB
TypeScript
Raw Normal View History

import { DOCUMENT } from '@angular/common';
2020-04-22 12:56:18 +02:00
import { Component, Inject, OnInit, OnDestroy } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { NavigationStart, Router } from '@angular/router';
import { Observable, Subscription } from 'rxjs';
import { filter } from 'rxjs/operators';
2020-04-22 12:56:18 +02:00
import { environment } from '../environments/environment';
2020-04-22 12:56:18 +02:00
import { Theme } from './core/enums/theme.enum';
import { ThemeService } from './core/services/theme.service';
import { ConfigService } from './services/config.service';
@Component({
2020-04-21 10:50:26 +02:00
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
2020-04-22 12:56:18 +02:00
export class AppComponent implements OnInit, OnDestroy {
public appTitle: string = environment.appTitle;
public themeClass: string;
public isDebugMode = false;
private themeSubscription: Subscription;
public step: string;
2020-04-21 10:50:26 +02:00
constructor(
2020-04-22 12:56:18 +02:00
private titleService: Title,
private themeService: ThemeService,
2020-04-21 10:50:26 +02:00
public config: ConfigService,
@Inject(DOCUMENT) private document,
2020-04-22 12:56:18 +02:00
private router: Router
) {}
ngOnInit(): void {
if (!environment.production) {
this.isDebugMode = true;
this.appTitle += ' | DEV';
console.info(' ######### framadate | DEV ######### we are NOT in production env, filling with mock values');
}
this.titleService.setTitle(this.appTitle);
this.themeSubscription = this.themeService.theme.subscribe((theme: Theme) => {
switch (theme) {
case Theme.DARK:
this.themeClass = 'theme-dark-crystal';
break;
case Theme.RED:
this.themeClass = 'theme-hot-covid';
break;
default:
this.themeClass = 'theme-light-watermelon';
}
});
2020-04-21 10:50:26 +02:00
this.detectCurrentTabOnRouteChange();
2020-04-22 12:56:18 +02:00
}
2020-04-22 12:56:18 +02:00
ngOnDestroy(): void {
if (this.themeSubscription) {
this.themeSubscription.unsubscribe();
}
2020-04-21 10:50:26 +02:00
}
2020-04-22 12:56:18 +02:00
public detectCurrentTabOnRouteChange(): void {
this.router.events
2020-04-21 10:50:26 +02:00
.pipe(filter((event) => event instanceof NavigationStart))
.subscribe((event: NavigationStart) => {
this.scrollGoToTop();
this.updateCurrentTab(event);
// only if there is a poll ID
this.config.fetchPollFromRoute(event);
});
}
2019-10-01 15:43:37 +02:00
2020-04-22 12:56:18 +02:00
public scrollGoToTop(): void {
2020-04-21 10:50:26 +02:00
this.document.documentElement.scrollTop = 0;
}
2020-01-23 14:23:07 +01:00
2020-04-22 12:56:18 +02:00
public updateCurrentTab(event): void {
2020-04-21 10:50:26 +02:00
if (event.url) {
2020-04-22 12:56:18 +02:00
const tab: string[] = event.url.split('/');
this.step = tab && tab[2] ? tab[2] : 'home';
2020-04-21 10:50:26 +02:00
}
}
2020-04-22 12:56:18 +02:00
public toggleMenu(): void {
// TODO: move this logic elsewhere, probably in Navbar component
this.config.menuVisible = !this.config.menuVisible;
}
}