diff --git a/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts b/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts index fa159d363..5bd7ff150 100644 --- a/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts +++ b/booklore-ui/src/app/book/component/layout/app.menuitem.component.ts @@ -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 { animate, state, style, transition, trigger } from '@angular/animations'; -import { Subscription } from 'rxjs'; -import { filter } from 'rxjs/operators'; -import { MenuService } from './app.menu.service'; -import { LayoutService } from './service/app.layout.service'; +import {animate, state, style, transition, trigger} from '@angular/animations'; +import {Subscription} from 'rxjs'; +import {filter} from 'rxjs/operators'; +import {MenuService} from './app.menu.service'; +import {LayoutService} from './service/app.layout.service'; import {NgClass, NgForOf, NgIf} from '@angular/common'; import {Ripple} from 'primeng/ripple'; @Component({ - // eslint-disable-next-line @angular-eslint/component-selector selector: '[app-menuitem]', template: ` +
{{ item.label }}
+ @@ -21,6 +22,7 @@ import {Ripple} from 'primeng/ripple'; {{ item.label }} + { - 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(() => { + constructor(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.router.events.pipe(filter(event => event instanceof NavigationEnd)) - .subscribe(params => { - if (this.item.routerLink) { - this.updateActiveStateFromRoute(); - } - }); - } - - ngOnInit() { - this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index); + this.menuResetSubscription = this.menuService.resetSource$.subscribe(() => { + this.active = false; + }); + this.router.events.pipe(filter(event => event instanceof NavigationEnd)) + .subscribe(params => { 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() { - let activeRoute = this.router.isActive(this.item.routerLink[0], { paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }); - - if (activeRoute) { - this.menuService.onMenuStateChange({ key: this.key, routeEvent: true }); - } + updateActiveStateFromRoute() { + let activeRoute = this.router.isActive(this.item.routerLink[0], { + paths: 'exact', + queryParams: 'ignored', + matrixParams: 'ignored', + fragment: 'ignored' + }); + if (activeRoute) { + this.menuService.onMenuStateChange({key: this.key, routeEvent: true}); } + } - itemClick(event: Event) { - if (this.item.disabled) { - event.preventDefault(); - 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 }); + itemClick(event: Event) { + if (this.item.disabled) { + event.preventDefault(); + return; } - - get submenuAnimation() { - return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed'); + if (this.item.command) { + this.item.command({originalEvent: event, item: this.item}); } - - @HostBinding('class.active-menuitem') - get activeClass() { - return this.active && !this.root; + if (this.item.items) { + this.active = !this.active; } + this.menuService.onMenuStateChange({key: this.key}); + } - ngOnDestroy() { - if (this.menuSourceSubscription) { - this.menuSourceSubscription.unsubscribe(); - } + get submenuAnimation() { + return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed'); + } - if (this.menuResetSubscription) { - this.menuResetSubscription.unsubscribe(); - } + @HostBinding('class.active-menuitem') + get activeClass() { + return this.active && !this.root; + } + + ngOnDestroy() { + if (this.menuSourceSubscription) { + this.menuSourceSubscription.unsubscribe(); } + if (this.menuResetSubscription) { + this.menuResetSubscription.unsubscribe(); + } + } }