import {Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild} from '@angular/core'; import {ActivatedRoute, Router} from '@angular/router'; import {Session, User} from './utils/helper.class'; import {RouterHelper} from '../utils/routerHelper.class'; import {Subscriber} from "rxjs"; import {MenuItem} from "../sharedComponents/menu"; import {UserManagementService} from "../services/user-management.service"; import { NotificationConfiguration, NotificationsSidebarComponent } from "../notifications/notifications-sidebar/notifications-sidebar.component"; declare var UIkit; @Component({ selector: 'user-mini', template: ` ` }) export class UserMiniComponent implements OnInit, OnChanges { @Input() user: User; public loggedIn: boolean = false; public isAuthorized: boolean = false; @Input() public mobileView: boolean = false; public firstLetters: string = ""; public routerHelper: RouterHelper = new RouterHelper(); @Input() userMenuItems; @Input() notificationConfiguration: NotificationConfiguration; @ViewChild('notificationsSidebar') notificationsSidebar: NotificationsSidebarComponent; public showNotifications = false; private subscriptions = []; constructor(private router: Router, private route: ActivatedRoute, private userManagementService: UserManagementService) { } ngOnInit() { this.initUser(); } ngOnChanges(changes: SimpleChanges): void { if (changes.user) { this.initUser(); } } ngOnDestroy() { this.subscriptions.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } closeCanvas(canvas) { UIkit.offcanvas(canvas).hide(); } initUser() { 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; } } logOut() { if (this.user) { this.userManagementService.logout(); } } logIn() { this.userManagementService.login(); } isTheActiveSubMenu(menuItem: MenuItem): boolean { let currentRoute = this.router.url.split('?')[0]; return menuItem.route == currentRoute || menuItem.route == (currentRoute + "/"); } parseName() { this.firstLetters = ""; if (this.user && this.user.firstname) { this.firstLetters += this.user.firstname.substr(0, 1); } if (this.user && this.user.lastname) { this.firstLetters += this.user.lastname.substr(0, 1); } if (!this.firstLetters && this.user && this.user.fullname) { let matches = this.user.fullname.match(/\b(\w)/g); this.firstLetters += matches.join(''); } } toggleNotifications() { this.showNotifications = !this.showNotifications; } }