1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/app.component.ts
2018-03-30 15:30:30 -04:00

72 lines
2.1 KiB
TypeScript

import {Component, OnDestroy, OnInit, ViewContainerRef} from '@angular/core';
import {AuthenticationService} from './model/network/authentication.service';
import {UserDTO} from '../../common/entities/UserDTO';
import {Router} from '@angular/router';
import {Config} from '../../common/config/public/Config';
import {Title} from '@angular/platform-browser';
import {NotificationService} from './model/notification.service';
import {ShareService} from './gallery/share.service';
import 'hammerjs';
@Component({
selector: 'app-pi-gallery2',
template: `<router-outlet></router-outlet>`,
})
export class AppComponent implements OnInit, OnDestroy {
private subscription = null;
constructor(private _router: Router,
private _authenticationService: AuthenticationService,
private _shareService: ShareService,
private _title: Title, vcr: ViewContainerRef,
notificationService: NotificationService) {
notificationService.setRootViewContainerRef(vcr);
}
async ngOnInit() {
this._title.setTitle(Config.Client.applicationTitle);
await this._shareService.wait();
this.subscription = this._authenticationService.user.subscribe((user: UserDTO) => {
if (this._authenticationService.isAuthenticated()) {
if (this.isLoginPage()) {
return this.toGallery();
}
} else {
if (!this.isLoginPage()) {
return this.toLogin();
}
}
});
}
ngOnDestroy() {
if (this.subscription != null) {
this.subscription.unsubscribe();
}
}
private isLoginPage() {
return this._router.isActive('login', true) || this._router.isActive('shareLogin', false);
}
private toLogin() {
if (this._shareService.isSharing()) {
return this._router.navigate(['shareLogin'], {queryParams: {sk: this._shareService.getSharingKey()}});
} else {
return this._router.navigate(['login']);
}
}
private toGallery() {
if (this._shareService.isSharing()) {
return this._router.navigate(['share', this._shareService.getSharingKey()]);
} else {
return this._router.navigate(['gallery', '']);
}
}
}