WIP: Add options to menu items
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
<ng-container>
|
||||||
|
|
||||||
|
<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"
|
||||||
|
(click)="itemClick($event)"
|
||||||
|
[ngClass]="item.class" [attr.target]="item.target" tabindex="0" pRipple>
|
||||||
|
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
|
||||||
|
<span class="layout-menuitem-text">
|
||||||
|
{{ item.label }}
|
||||||
|
</span>
|
||||||
|
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<div class="flex flex-row items-center justify-between">
|
||||||
|
<a *ngIf="(item.routerLink && !item.items) && item.visible !== false" (click)="itemClick($event)"
|
||||||
|
[ngClass]="item.class"
|
||||||
|
[routerLink]="item.routerLink" routerLinkActive="active-route"
|
||||||
|
[routerLinkActiveOptions]="item.routerLinkActiveOptions||{ paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }"
|
||||||
|
[fragment]="item.fragment" [queryParamsHandling]="item.queryParamsHandling"
|
||||||
|
[preserveFragment]="item.preserveFragment"
|
||||||
|
[skipLocationChange]="item.skipLocationChange" [replaceUrl]="item.replaceUrl" [state]="item.state"
|
||||||
|
[queryParams]="item.queryParams"
|
||||||
|
[attr.target]="item.target" tabindex="0" pRipple>
|
||||||
|
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
|
||||||
|
<span class="layout-menuitem-text w-full">
|
||||||
|
{{ item.label }}
|
||||||
|
</span>
|
||||||
|
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
|
||||||
|
</a>
|
||||||
|
<span *ngIf="(item.routerLink && !item.items) && item.visible !== false" class="book-count">
|
||||||
|
<p-button
|
||||||
|
(mouseover)="hovered = true" (mouseleave)="hovered = false"
|
||||||
|
[text]="true"
|
||||||
|
[outlined]="false"
|
||||||
|
[rounded]="true"
|
||||||
|
[label]="hovered ? '' : (item.bookCount$ | async)?.toString() || ''"
|
||||||
|
[icon]="hovered ? 'pi pi-ellipsis-v' : ''"
|
||||||
|
class="font-bold">
|
||||||
|
</p-button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ul *ngIf="item.items && item.visible !== false" [@children]="submenuAnimation">
|
||||||
|
<ng-template ngFor let-child let-i="index" [ngForOf]="item.items">
|
||||||
|
<li app-menuitem [item]="child" [index]="i" [parentKey]="key" [class]="child.badgeClass"></li>
|
||||||
|
</ng-template>
|
||||||
|
</ul>
|
||||||
|
</ng-container>
|
||||||
@@ -6,155 +6,120 @@ import {filter} from 'rxjs/operators';
|
|||||||
import {MenuService} from './service/app.menu.service';
|
import {MenuService} from './service/app.menu.service';
|
||||||
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
|
import {AsyncPipe, NgClass, NgForOf, NgIf} from '@angular/common';
|
||||||
import {Ripple} from 'primeng/ripple';
|
import {Ripple} from 'primeng/ripple';
|
||||||
|
import {Button} from 'primeng/button';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: '[app-menuitem]',
|
selector: '[app-menuitem]',
|
||||||
template: `
|
templateUrl: './app.menuitem.component.html',
|
||||||
<ng-container>
|
styleUrl: './app.menuitem.component.scss',
|
||||||
|
imports: [
|
||||||
<div *ngIf="root && item.visible !== false" class="layout-menuitem-root-text">{{ item.label }}</div>
|
RouterLink,
|
||||||
|
RouterLinkActive,
|
||||||
<a *ngIf="(!item.routerLink || item.items) && item.visible !== false" [attr.href]="item.url"
|
NgClass,
|
||||||
(click)="itemClick($event)"
|
NgForOf,
|
||||||
[ngClass]="item.class" [attr.target]="item.target" tabindex="0" pRipple>
|
Ripple,
|
||||||
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
|
NgIf,
|
||||||
<span class="layout-menuitem-text">
|
AsyncPipe,
|
||||||
{{ item.label }}
|
Button
|
||||||
</span>
|
],
|
||||||
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
|
animations: [
|
||||||
</a>
|
trigger('children', [
|
||||||
|
state('collapsed', style({
|
||||||
<a *ngIf="(item.routerLink && !item.items) && item.visible !== false" (click)="itemClick($event)"
|
height: '0'
|
||||||
[ngClass]="item.class"
|
})),
|
||||||
[routerLink]="item.routerLink" routerLinkActive="active-route"
|
state('expanded', style({
|
||||||
[routerLinkActiveOptions]="item.routerLinkActiveOptions||{ paths: 'exact', queryParams: 'ignored', matrixParams: 'ignored', fragment: 'ignored' }"
|
height: '*'
|
||||||
[fragment]="item.fragment" [queryParamsHandling]="item.queryParamsHandling"
|
})),
|
||||||
[preserveFragment]="item.preserveFragment"
|
transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)'))
|
||||||
[skipLocationChange]="item.skipLocationChange" [replaceUrl]="item.replaceUrl" [state]="item.state"
|
])
|
||||||
[queryParams]="item.queryParams"
|
]
|
||||||
[attr.target]="item.target" tabindex="0" pRipple>
|
|
||||||
<i [ngClass]="item.icon" class="layout-menuitem-icon"></i>
|
|
||||||
<span class="layout-menuitem-text w-full">
|
|
||||||
{{ item.label }}
|
|
||||||
<span *ngIf="item.bookCount$ | async as bookCount">
|
|
||||||
{{ bookCount }}
|
|
||||||
</span>
|
|
||||||
</span>
|
|
||||||
<i class="pi pi-fw pi-angle-down layout-submenu-toggler" *ngIf="item.items"></i>
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<ul *ngIf="item.items && item.visible !== false" [@children]="submenuAnimation">
|
|
||||||
<ng-template ngFor let-child let-i="index" [ngForOf]="item.items">
|
|
||||||
<li app-menuitem [item]="child" [index]="i" [parentKey]="key" [class]="child.badgeClass"></li>
|
|
||||||
</ng-template>
|
|
||||||
</ul>
|
|
||||||
</ng-container>
|
|
||||||
`,
|
|
||||||
imports: [
|
|
||||||
RouterLink,
|
|
||||||
RouterLinkActive,
|
|
||||||
NgClass,
|
|
||||||
NgForOf,
|
|
||||||
Ripple,
|
|
||||||
NgIf,
|
|
||||||
AsyncPipe
|
|
||||||
],
|
|
||||||
animations: [
|
|
||||||
trigger('children', [
|
|
||||||
state('collapsed', style({
|
|
||||||
height: '0'
|
|
||||||
})),
|
|
||||||
state('expanded', style({
|
|
||||||
height: '*'
|
|
||||||
})),
|
|
||||||
transition('collapsed <=> expanded', animate('400ms cubic-bezier(0.86, 0, 0.07, 1)'))
|
|
||||||
])
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
export class AppMenuitemComponent implements OnInit, OnDestroy {
|
export class AppMenuitemComponent implements OnInit, OnDestroy {
|
||||||
@Input() item: any;
|
@Input() item: any;
|
||||||
@Input() index!: number;
|
@Input() index!: number;
|
||||||
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean;
|
@Input() @HostBinding('class.layout-root-menuitem') root!: boolean;
|
||||||
@Input() parentKey!: string;
|
@Input() parentKey!: string;
|
||||||
|
|
||||||
active = false;
|
hovered = false;
|
||||||
menuSourceSubscription: Subscription;
|
|
||||||
menuResetSubscription: Subscription;
|
|
||||||
key: string = "";
|
|
||||||
|
|
||||||
constructor(public router: Router, private menuService: MenuService) {
|
active = false;
|
||||||
this.menuSourceSubscription = this.menuService.menuSource$.subscribe(value => {
|
menuSourceSubscription: Subscription;
|
||||||
Promise.resolve(null).then(() => {
|
menuResetSubscription: Subscription;
|
||||||
if (value.routeEvent) {
|
key: string = "";
|
||||||
this.active = (value.key === this.key || value.key.startsWith(this.key + '-')) ? true : false;
|
|
||||||
} else {
|
constructor(public router: Router, private menuService: MenuService) {
|
||||||
if (value.key !== this.key && !value.key.startsWith(this.key + '-')) {
|
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.menuResetSubscription = this.menuService.resetSource$.subscribe(() => {
|
this.router.events.pipe(filter(event => event instanceof NavigationEnd))
|
||||||
this.active = false;
|
.subscribe(() => {
|
||||||
});
|
if (this.item.routerLink) {
|
||||||
|
this.updateActiveStateFromRoute();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
this.router.events.pipe(filter(event => event instanceof NavigationEnd))
|
ngOnInit() {
|
||||||
.subscribe(() => {
|
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
|
||||||
if (this.item.routerLink) {
|
if (this.item.routerLink) {
|
||||||
this.updateActiveStateFromRoute();
|
this.updateActiveStateFromRoute();
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ngOnInit() {
|
updateActiveStateFromRoute() {
|
||||||
this.key = this.parentKey ? this.parentKey + '-' + this.index : String(this.index);
|
const activeRoute = this.router.isActive(this.item.routerLink[0], {
|
||||||
if (this.item.routerLink) {
|
paths: 'exact',
|
||||||
this.updateActiveStateFromRoute();
|
queryParams: 'ignored',
|
||||||
|
matrixParams: 'ignored',
|
||||||
|
fragment: 'ignored'
|
||||||
|
});
|
||||||
|
if (activeRoute) {
|
||||||
|
this.menuService.onMenuStateChange({key: this.key, routeEvent: true});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
updateActiveStateFromRoute() {
|
itemClick(event: Event) {
|
||||||
const activeRoute = this.router.isActive(this.item.routerLink[0], {
|
if (this.item.disabled) {
|
||||||
paths: 'exact',
|
event.preventDefault();
|
||||||
queryParams: 'ignored',
|
return;
|
||||||
matrixParams: 'ignored',
|
}
|
||||||
fragment: 'ignored'
|
if (this.item.command) {
|
||||||
});
|
this.item.command({originalEvent: event, item: this.item});
|
||||||
if (activeRoute) {
|
}
|
||||||
this.menuService.onMenuStateChange({key: this.key, routeEvent: true});
|
if (this.item.items) {
|
||||||
|
this.active = !this.active;
|
||||||
|
}
|
||||||
|
this.menuService.onMenuStateChange({key: this.key});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
itemClick(event: Event) {
|
get submenuAnimation() {
|
||||||
if (this.item.disabled) {
|
return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed');
|
||||||
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});
|
|
||||||
}
|
|
||||||
|
|
||||||
get submenuAnimation() {
|
@HostBinding('class.active-menuitem')
|
||||||
return this.root ? 'expanded' : (this.active ? 'expanded' : 'collapsed');
|
get activeClass() {
|
||||||
}
|
return this.active && !this.root;
|
||||||
|
|
||||||
@HostBinding('class.active-menuitem')
|
|
||||||
get activeClass() {
|
|
||||||
return this.active && !this.root;
|
|
||||||
}
|
|
||||||
|
|
||||||
ngOnDestroy() {
|
|
||||||
if (this.menuSourceSubscription) {
|
|
||||||
this.menuSourceSubscription.unsubscribe();
|
|
||||||
}
|
}
|
||||||
if (this.menuResetSubscription) {
|
|
||||||
this.menuResetSubscription.unsubscribe();
|
ngOnDestroy() {
|
||||||
|
if (this.menuSourceSubscription) {
|
||||||
|
this.menuSourceSubscription.unsubscribe();
|
||||||
|
}
|
||||||
|
if (this.menuResetSubscription) {
|
||||||
|
this.menuResetSubscription.unsubscribe();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user