Format AppMenuitemComponent

This commit is contained in:
aditya.chandel
2024-12-27 11:39:00 -07:00
parent 3f13917191
commit 8aeb361052
@@ -1,19 +1,20 @@
import { ChangeDetectorRef, Component, Host, HostBinding, Input, OnDestroy, OnInit } from '@angular/core'; import {ChangeDetectorRef, Component, Host, HostBinding, Input, OnDestroy, OnInit} from '@angular/core';
import {NavigationEnd, Router, RouterLink, RouterLinkActive} from '@angular/router'; import {NavigationEnd, Router, RouterLink, RouterLinkActive} from '@angular/router';
import { animate, state, style, transition, trigger } from '@angular/animations'; import {animate, state, style, transition, trigger} from '@angular/animations';
import { Subscription } from 'rxjs'; import {Subscription} from 'rxjs';
import { filter } from 'rxjs/operators'; import {filter} from 'rxjs/operators';
import { MenuService } from './app.menu.service'; import {MenuService} from './app.menu.service';
import { LayoutService } from './service/app.layout.service'; import {LayoutService} from './service/app.layout.service';
import {NgClass, NgForOf, NgIf} from '@angular/common'; import {NgClass, NgForOf, NgIf} from '@angular/common';
import {Ripple} from 'primeng/ripple'; import {Ripple} from 'primeng/ripple';
@Component({ @Component({
// eslint-disable-next-line @angular-eslint/component-selector
selector: '[app-menuitem]', selector: '[app-menuitem]',
template: ` template: `
<ng-container> <ng-container>
<div *ngIf="root && item.visible !== false" class="layout-menuitem-root-text">{{ item.label }}</div> <div *ngIf="root && item.visible !== false" class="layout-menuitem-root-text">{{ item.label }}</div>
<a *ngIf="(!item.routerLink || item.items) && item.visible !== false" [attr.href]="item.url" <a *ngIf="(!item.routerLink || item.items) && item.visible !== false" [attr.href]="item.url"
(click)="itemClick($event)" (click)="itemClick($event)"
[ngClass]="item.class" [attr.target]="item.target" tabindex="0" pRipple> [ngClass]="item.class" [attr.target]="item.target" tabindex="0" pRipple>
@@ -21,6 +22,7 @@ import {Ripple} from 'primeng/ripple';
<span class="layout-menuitem-text">{{ item.label }}</span> <span class="layout-menuitem-text">{{ item.label }}</span>
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i> <i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
</a> </a>
<a *ngIf="(item.routerLink && !item.items) && item.visible !== false" (click)="itemClick($event)" <a *ngIf="(item.routerLink && !item.items) && item.visible !== false" (click)="itemClick($event)"
[ngClass]="item.class" [ngClass]="item.class"
[routerLink]="item.routerLink" routerLinkActive="active-route" [routerLink]="item.routerLink" routerLinkActive="active-route"
@@ -63,95 +65,89 @@ import {Ripple} from 'primeng/ripple';
] ]
}) })
export class AppMenuitemComponent implements OnInit, OnDestroy { export class AppMenuitemComponent implements OnInit, OnDestroy {
@Input() item: any;
@Input() index!: number;
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean;
@Input() parentKey!: string;
@Input() item: any; active = false;
menuSourceSubscription: Subscription;
menuResetSubscription: Subscription;
key: string = "";
@Input() index!: number; constructor(public router: Router, private menuService: MenuService) {
this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => {
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean; Promise.resolve(null).then(() => {
if (value.routeEvent) {
@Input() parentKey!: string; this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false;
} else {
active = false; if (value.key !== this.key && !value.key.startsWith(this.key + '-')) {
menuSourceSubscription: Subscription;
menuResetSubscription: Subscription;
key: string = "";
constructor(public layoutService: LayoutService, private cd: ChangeDetectorRef, public router: Router, private menuService: MenuService) {
this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => {
Promise.resolve(null).then(() => {
if (value.routeEvent) {
this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false;
}
else {
if (value.key !== this.key && !value.key.startsWith(this.key + '-')) {
this.active = false;
}
}
});
});
this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
this.active = false; this.active = false;
}); }
}
});
});
this.router.events.pipe(filter(event => event instanceof NavigationEnd)) this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
.subscribe(params => { this.active = false;
if (this.item.routerLink) { });
this.updateActiveStateFromRoute();
}
});
}
ngOnInit() {
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
this.router.events.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(params => {
if (this.item.routerLink) { if (this.item.routerLink) {
this.updateActiveStateFromRoute(); this.updateActiveStateFromRoute();
} }
});
}
ngOnInit() {
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
if (this.item.routerLink) {
this.updateActiveStateFromRoute();
} }
}
updateActiveStateFromRoute() { updateActiveStateFromRoute() {
let activeRoute = this.router.isActive(this.item.routerLink[0], { paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }); let activeRoute = this.router.isActive(this.item.routerLink[0], {
paths: 'exact',
if (activeRoute) { queryParams: 'ignored',
this.menuService.onMenuStateChange({ key: this.key, routeEvent: true }); matrixParams: 'ignored',
} fragment: 'ignored'
});
if (activeRoute) {
this.menuService.onMenuStateChange({key: this.key, routeEvent: true});
} }
}
itemClick(event: Event) { itemClick(event: Event) {
if (this.item.disabled) { if (this.item.disabled) {
event.preventDefault(); event.preventDefault();
return; return;
}
if (this.item.command) {
this.item.command({ originalEvent: event, item: this.item });
}
if (this.item.items) {
this.active = !this.active;
}
this.menuService.onMenuStateChange({ key: this.key });
} }
if (this.item.command) {
get submenuAnimation() { this.item.command({originalEvent: event, item: this.item});
return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed');
} }
if (this.item.items) {
@HostBinding('class.active-menuitem') this.active = !this.active;
get activeClass() {
return this.active && !this.root;
} }
this.menuService.onMenuStateChange({key: this.key});
}
ngOnDestroy() { get submenuAnimation() {
if (this.menuSourceSubscription) { return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed');
this.menuSourceSubscription.unsubscribe(); }
}
if (this.menuResetSubscription) { @HostBinding('class.active-menuitem')
this.menuResetSubscription.unsubscribe(); get activeClass() {
} return this.active && !this.root;
}
ngOnDestroy() {
if (this.menuSourceSubscription) {
this.menuSourceSubscription.unsubscribe();
} }
if (this.menuResetSubscription) {
this.menuResetSubscription.unsubscribe();
}
}
} }