openaire-library/dashboard/sharedComponents/sidebar/sideBar.component.ts

88 lines
2.6 KiB
TypeScript
Raw Normal View History

import {AfterViewInit, Component, ElementRef, Input, ViewChild} from '@angular/core';
import {MenuItem} from "../../../sharedComponents/menu";
import {ActivatedRoute, Router} from "@angular/router";
import {DomSanitizer} from "@angular/platform-browser";
import {properties} from "../../../../../environments/environment";
import {LayoutService} from "./layout.service";
declare var UIkit;
@Component({
selector: 'dashboard-sidebar',
templateUrl: 'sideBar.component.html'
})
export class SideBarComponent implements AfterViewInit {
@Input() items: MenuItem[] = [];
@Input() activeItem: string = '';
@Input() activeSubItem: string = '';
@Input() specialMenuItem: MenuItem = null;
2022-03-17 12:28:25 +01:00
@Input() queryParamsHandling;
@ViewChild("nav") nav: ElementRef
public properties = properties;
constructor(private route: ActivatedRoute, private router: Router, private sanitizer: DomSanitizer, private layoutService: LayoutService) {}
ngAfterViewInit() {
if(this.nav) {
setTimeout(() => {
if(this.items[this.activeIndex].items.length > 0) {
UIkit.nav(this.nav.nativeElement).toggle(this.activeIndex, true);
}
});
}
}
2022-03-28 10:40:21 +02:00
get currentRoute() {
return {
route: this.router.url.split('?')[0].split('#')[0],
fragment: this.route.snapshot.fragment
}
}
2022-03-17 12:28:25 +01:00
get activeIndex(): number {
return this.items?this.items.findIndex(item => this.isTheActiveMenuItem(item)):0;
}
getItemRoute(item: MenuItem) {
if(this.activeSubItem && item.items.length > 0) {
let subItem = item.items.find(subItem => subItem._id === this.activeSubItem);
return subItem?subItem.route:(item.route?item.route:null);
} else {
return item.route?item.route:null;
}
}
isTheActiveMenuItem(item: MenuItem, subItem: MenuItem = null): boolean {
if (this.activeItem || this.activeSubItem) {
return (!subItem && this.activeItem === item._id) ||
(subItem && this.activeItem === item._id && this.activeSubItem === subItem._id);
} else {
if (subItem) {
2022-03-28 10:40:21 +02:00
return MenuItem.isTheActiveMenu(subItem, this.currentRoute);
}
2022-03-28 10:40:21 +02:00
return MenuItem.isTheActiveMenu(item,this.currentRoute);
}
}
isTheActiveUrl(menuItemURL): boolean {
return (menuItemURL == this.router.url.split('?')[0])
}
satinizeHTML(html) {
return this.sanitizer.bypassSecurityTrustHtml(html);
}
public get isSmallScreen() {
return this.layoutService.isSmallScreen;
}
public get open() {
return this.layoutService.open;
}
public toggleOpen(event: MouseEvent) {
event.preventDefault();
this.layoutService.setOpen(!this.open);
}
}