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

76 lines
2.1 KiB
TypeScript
Raw Normal View History

2019-01-28 03:58:56 +08:00
import {Component, OnDestroy, OnInit} from '@angular/core';
2018-03-31 03:30:30 +08:00
import {AuthenticationService} from './model/network/authentication.service';
import {Router} from '@angular/router';
import {Config} from '../../common/config/public/Config';
import {Title} from '@angular/platform-browser';
import {ShareService} from './ui/gallery/share.service';
2018-03-31 03:30:30 +08:00
import 'hammerjs';
2018-11-29 06:49:33 +08:00
import {Subscription} from 'rxjs';
2020-01-08 05:17:54 +08:00
import {QueryParams} from '../../common/QueryParams';
2017-07-20 04:40:27 +08:00
2016-03-12 21:57:22 +08:00
@Component({
2018-03-31 03:30:30 +08:00
selector: 'app-pi-gallery2',
2020-01-08 05:17:54 +08:00
template: `
<router-outlet></router-outlet>`
2016-03-12 21:57:22 +08:00
})
2017-07-20 04:40:27 +08:00
export class AppComponent implements OnInit, OnDestroy {
2018-11-29 06:49:33 +08:00
private subscription: Subscription = null;
2016-03-13 18:28:29 +08:00
constructor(private router: Router,
private authenticationService: AuthenticationService,
private shareService: ShareService,
private title: Title) {
}
2016-03-13 01:11:19 +08:00
async ngOnInit(): Promise<void> {
this.title.setTitle(Config.Client.applicationTitle);
await this.shareService.wait();
this.subscription = this.authenticationService.user.subscribe(() => {
if (this.authenticationService.isAuthenticated()) {
2017-07-09 18:03:17 +08:00
if (this.isLoginPage()) {
return this.toGallery();
}
} else {
2017-07-09 18:03:17 +08:00
if (!this.isLoginPage()) {
return this.toLogin();
}
}
2016-05-17 05:15:03 +08:00
});
2016-05-09 23:04:56 +08:00
2017-07-20 04:40:27 +08:00
}
ngOnDestroy(): void {
2017-07-20 04:40:27 +08:00
if (this.subscription != null) {
this.subscription.unsubscribe();
}
}
2017-06-22 03:16:04 +08:00
private isLoginPage(): boolean {
return this.router.isActive('login', true) || this.router.isActive('shareLogin', false);
2017-07-09 18:03:17 +08:00
}
private async toLogin(): Promise<void> {
if (this.shareService.isSharing()) {
2020-01-08 05:17:54 +08:00
const q: any = {};
q[QueryParams.gallery.sharingKey_query] = this.shareService.getSharingKey();
await this.router.navigate(['shareLogin'], {queryParams: q});
return;
2017-07-09 18:03:17 +08:00
} else {
await this.router.navigate(['login']);
return;
2017-07-09 18:03:17 +08:00
}
}
2017-06-22 03:16:04 +08:00
private async toGallery(): Promise<void> {
if (this.shareService.isSharing()) {
await this.router.navigate(['share', this.shareService.getSharingKey()]);
return;
2017-07-09 18:03:17 +08:00
} else {
await this.router.navigate(['gallery', '']);
return;
2017-07-09 18:03:17 +08:00
}
}
}