opt-cc/static/src/app/app.component.ts

75 lines
2.2 KiB
TypeScript

import {Component, HostListener, Inject, OnInit} from '@angular/core';
import {NavigationEnd, NavigationStart, Router} from '@angular/router';
import {LoginService} from './services/app-user-service/login-service';
import {PromotionService} from './services/army-management/promotion.service';
import {AwardingService} from './services/army-management/awarding.service';
import {RouteConfig} from './app.config';
import {DOCUMENT} from "@angular/common";
declare function require(url: string);
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.css', 'style/load-indicator.css']
})
export class AppComponent implements OnInit {
config = RouteConfig;
loading = false;
scrollTopVisible = false;
scrollBtnVisibleVal = 100;
version = 'v'.concat(require('./../../../package.json').version);
constructor(public loginService: LoginService,
private promotionService: PromotionService,
private awardingService: AwardingService,
private router: Router,
@Inject(DOCUMENT) private document) {
router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.loading = true;
}
if (event instanceof NavigationEnd) {
this.loading = false;
// scroll to top on route from army overview to user detail and back
if (router.url.includes('overview')) {
this.scrollToTop()
}
}
});
}
@HostListener("window:scroll", [])
onWindowScroll() {
this.scrollTopVisible = document.body.scrollTop > this.scrollBtnVisibleVal
|| document.documentElement.scrollTop > this.scrollBtnVisibleVal;
}
ngOnInit() {
if (this.loginService.hasPermission(2)) {
const fraction = this.loginService.getCurrentUser().squad.fraction;
this.promotionService.checkUnconfirmedPromotions(fraction);
this.awardingService.checkUnprocessedAwards(fraction);
}
}
logout() {
this.loginService.logout();
this.router.navigate([RouteConfig.overviewPath]);
return false;
}
scrollToTop() {
this.document.body.scrollTop = 0; // For Safari
this.document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
}