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

84 lines
2.7 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';
import {DomSanitizer} from '@angular/platform-browser';
import {MatIconRegistry} from '@angular/material';
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,
private iconRegistry: MatIconRegistry,
private sanitizer: DomSanitizer,
@Inject(DOCUMENT) private document) {
this.iconRegistry.addSvgIcon('add',
sanitizer.bypassSecurityTrustResourceUrl('../assets/icon/twotone-add_circle-24px.svg'));
this.iconRegistry.addSvgIcon('add-user',
sanitizer.bypassSecurityTrustResourceUrl('../assets/icon/twotone-person_add-24px.svg'));
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
}
}