2022-02-28 13:38:26 +01:00
|
|
|
import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
|
2021-03-17 10:21:37 +01:00
|
|
|
import {ActivatedRoute, Router} from '@angular/router';
|
|
|
|
import {Session, User} from './utils/helper.class';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {RouterHelper} from '../utils/routerHelper.class';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {Subscriber} from "rxjs";
|
2021-01-11 15:14:44 +01:00
|
|
|
import {MenuItem} from "../sharedComponents/menu";
|
2021-03-17 10:21:37 +01:00
|
|
|
import {UserManagementService} from "../services/user-management.service";
|
2018-02-05 14:14:59 +01:00
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
@Component({
|
2020-05-06 18:31:54 +02:00
|
|
|
selector: 'user-mini',
|
|
|
|
template: `
|
2022-07-04 21:34:28 +02:00
|
|
|
<ul *ngIf="!mobileView" class="uk-navbar-nav">
|
2022-03-02 22:48:21 +01:00
|
|
|
<li class="uk-parent">
|
|
|
|
<a *ngIf="!loggedIn" (click)="logIn()">Sign in</a>
|
|
|
|
<ng-container *ngIf="loggedIn">
|
|
|
|
<a class="login uk-icon">
|
|
|
|
<svg height="60" width="60">
|
|
|
|
<circle cx="30" cy="30" r="20" stroke-width="2"></circle>
|
|
|
|
<text x="50%" y="50%" text-anchor="middle" dy=".4em" font-size="16">
|
|
|
|
{{firstLetters ? firstLetters : 'AN'}}
|
|
|
|
</text>
|
|
|
|
</svg>
|
|
|
|
</a>
|
2022-06-29 13:33:21 +02:00
|
|
|
<div class="uk-navbar-dropdown uk-dropdown" uk-dropdown="pos: bottom-right">
|
2022-03-02 22:48:21 +01:00
|
|
|
<ul class="uk-nav uk-navbar-dropdown-nav">
|
|
|
|
<ng-container *ngFor="let item of userMenuItems ">
|
|
|
|
<li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization"
|
|
|
|
[class.uk-active]="isTheActiveSubMenu(item)">
|
2022-06-29 13:33:21 +02:00
|
|
|
<a *ngIf="item.route" [routerLink]="item.route"
|
2022-03-02 22:48:21 +01:00
|
|
|
[queryParams]="item.params">{{item.title}}</a>
|
2022-06-29 13:33:21 +02:00
|
|
|
<a *ngIf="!item.route && item.url" [href]="item.url" [class.custom-external]="item.target != '_self'"
|
|
|
|
[target]="item.target">{{item.title}}</a>
|
2022-03-02 22:48:21 +01:00
|
|
|
</li>
|
|
|
|
</ng-container>
|
|
|
|
<li *ngIf="userMenuItems.length > 0" class="uk-nav-divider"></li>
|
|
|
|
<li><a (click)="logOut()">Log out</a></li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</ng-container>
|
|
|
|
</li>
|
|
|
|
</ul>
|
2022-11-28 15:25:05 +01:00
|
|
|
<ul *ngIf="mobileView" class="uk-nav uk-nav-primary uk-list uk-margin-top uk-nav-parent-icon" uk-nav>
|
|
|
|
<li [class.uk-parent]="loggedIn">
|
|
|
|
<a *ngIf="loggedIn">
|
|
|
|
{{user.fullname.toString()}}
|
|
|
|
</a>
|
2022-03-02 22:48:21 +01:00
|
|
|
<a *ngIf="!loggedIn" (click)="logIn()">Sign in</a>
|
|
|
|
<ul *ngIf="loggedIn" class="uk-nav-sub">
|
2020-05-06 18:31:54 +02:00
|
|
|
<ng-container *ngFor="let item of userMenuItems ">
|
|
|
|
<li *ngIf="item.needsAuthorization && isAuthorized || !item.needsAuthorization">
|
2022-06-29 13:33:21 +02:00
|
|
|
<a *ngIf="item.route" [routerLink]="item.route" (click)="closeCanvas()">{{item.title}}</a>
|
|
|
|
<a *ngIf="!item.route && item.url" (click)="closeCanvas()" [href]="item.url" [class.custom-external]="item.target != '_self'"
|
|
|
|
[target]="item.target">{{item.title}}</a>
|
2020-05-06 18:31:54 +02:00
|
|
|
</li>
|
|
|
|
</ng-container>
|
|
|
|
<li *ngIf="userMenuItems.length > 0" class="uk-nav-divider"></li>
|
2022-02-28 13:38:26 +01:00
|
|
|
<li><a (click)="logOut()">Log out</a></li>
|
2020-05-06 18:31:54 +02:00
|
|
|
</ul>
|
2022-03-02 22:48:21 +01:00
|
|
|
</li>
|
|
|
|
</ul>
|
2020-05-06 18:31:54 +02:00
|
|
|
`
|
2017-12-19 13:53:46 +01:00
|
|
|
})
|
2019-12-15 13:44:20 +01:00
|
|
|
export class UserMiniComponent implements OnInit, OnChanges {
|
2020-05-06 18:31:54 +02:00
|
|
|
@Input() user: User;
|
|
|
|
public loggedIn: boolean = false;
|
|
|
|
public isAuthorized: boolean = false;
|
|
|
|
@Input() public mobileView: boolean = false;
|
|
|
|
public firstLetters: string = "";
|
|
|
|
public server: boolean = true;
|
|
|
|
public routerHelper: RouterHelper = new RouterHelper();
|
|
|
|
@Input() userMenuItems;
|
|
|
|
@Input() logInUrl;
|
|
|
|
@Input() logOutUrl;
|
|
|
|
@Input() cookieDomain;
|
2022-02-28 13:38:26 +01:00
|
|
|
@Output() closeCanvasEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
|
|
|
|
private subscriptions = [];
|
2020-05-06 18:31:54 +02:00
|
|
|
|
2022-02-28 13:38:26 +01:00
|
|
|
constructor(private router: Router, private route: ActivatedRoute, private userManagementService: UserManagementService) {
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
if (typeof document !== 'undefined') {
|
|
|
|
this.server = false;
|
2019-12-15 13:44:20 +01:00
|
|
|
}
|
2021-03-17 10:21:37 +01:00
|
|
|
this.initUser();
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
if (changes.user) {
|
2021-03-17 10:21:37 +01:00
|
|
|
this.initUser();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
2022-02-28 13:38:26 +01:00
|
|
|
|
2020-05-06 18:31:54 +02:00
|
|
|
ngOnDestroy() {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.subscriptions.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 13:38:26 +01:00
|
|
|
closeCanvas() {
|
|
|
|
this.closeCanvasEmitter.emit(true);
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:21:37 +01:00
|
|
|
initUser() {
|
2020-05-06 18:31:54 +02:00
|
|
|
if (this.user) {
|
|
|
|
this.loggedIn = true;
|
|
|
|
this.parseName();
|
|
|
|
this.isAuthorized = Session.isClaimsCurator(this.user) || Session.isPortalAdministrator(this.user);
|
|
|
|
} else {
|
|
|
|
this.loggedIn = false;
|
|
|
|
this.isAuthorized = false;
|
|
|
|
this.user = null;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logOut() {
|
|
|
|
if (this.user) {
|
2021-03-17 10:21:37 +01:00
|
|
|
this.userManagementService.logout();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
this.loggedIn = false;
|
|
|
|
this.isAuthorized = false;
|
|
|
|
this.user = new User();
|
|
|
|
}
|
|
|
|
|
|
|
|
logIn() {
|
2021-03-17 10:21:37 +01:00
|
|
|
this.userManagementService.login();
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
2022-02-28 13:38:26 +01:00
|
|
|
|
2021-01-11 15:14:44 +01:00
|
|
|
isTheActiveSubMenu(menuItem: MenuItem): boolean {
|
|
|
|
let currentRoute = this.router.url.split('?')[0];
|
2022-02-28 13:38:26 +01:00
|
|
|
return menuItem.route == currentRoute || menuItem.route == (currentRoute + "/");
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
2022-02-28 13:38:26 +01:00
|
|
|
|
2020-05-06 18:31:54 +02:00
|
|
|
parseName() {
|
|
|
|
this.firstLetters = "";
|
|
|
|
if (this.user && this.user.firstname) {
|
|
|
|
this.firstLetters += this.user.firstname.substr(0, 1);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
if (this.user && this.user.lastname) {
|
|
|
|
this.firstLetters += this.user.lastname.substr(0, 1);
|
2019-12-12 15:11:01 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
if (!this.firstLetters && this.user && this.user.fullname) {
|
|
|
|
let matches = this.user.fullname.match(/\b(\w)/g);
|
|
|
|
this.firstLetters += matches.join('');
|
2019-12-12 15:11:01 +01:00
|
|
|
}
|
2020-05-06 18:31:54 +02:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|