fix(sidebar): highlight nav item for detail pages, not just their list route
This commit is contained in:
@@ -67,8 +67,7 @@
|
||||
class="nav-icon-button"
|
||||
[routerLink]="item.disabled ? null : item.route"
|
||||
[queryParams]="item.queryParams || null"
|
||||
routerLinkActive="active"
|
||||
[routerLinkActiveOptions]="navLinkActiveOptions"
|
||||
[class.active]="isItemActive(item)"
|
||||
[class.disabled]="item.disabled"
|
||||
[matTooltip]="item.label"
|
||||
matTooltipPosition="right"
|
||||
@@ -80,8 +79,7 @@
|
||||
mat-list-item
|
||||
[routerLink]="item.disabled ? null : item.route"
|
||||
[queryParams]="item.queryParams || null"
|
||||
routerLinkActive="active"
|
||||
[routerLinkActiveOptions]="navLinkActiveOptions"
|
||||
[class.active]="isItemActive(item)"
|
||||
[class.disabled]="item.disabled">
|
||||
<mat-icon matListItemIcon>{{ item.icon }}</mat-icon>
|
||||
<span matListItemTitle>{{ item.label }}</span>
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Component, computed, inject, input, output } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { IsActiveMatchOptions, Router, RouterModule } from '@angular/router';
|
||||
import { NavigationEnd, Router, RouterModule } from '@angular/router';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { filter, map } from 'rxjs';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatListModule } from '@angular/material/list';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
@@ -42,6 +44,7 @@ type NavItem = {
|
||||
adminOnly?: boolean;
|
||||
superAdminOnly?: boolean;
|
||||
managerOrAdminOnly?: boolean;
|
||||
detailRoutePrefixes?: string[];
|
||||
};
|
||||
|
||||
type NavItemGroup = {
|
||||
@@ -75,12 +78,26 @@ export class AppMenuComponent {
|
||||
collapsed = input(false);
|
||||
canCollapse = input(false);
|
||||
toggleCollapsed = output<void>();
|
||||
readonly navLinkActiveOptions: IsActiveMatchOptions = {
|
||||
paths: 'subset',
|
||||
queryParams: 'ignored',
|
||||
matrixParams: 'ignored',
|
||||
fragment: 'ignored',
|
||||
};
|
||||
|
||||
private readonly router = inject(Router);
|
||||
|
||||
private readonly currentPath = toSignal(
|
||||
this.router.events.pipe(
|
||||
filter((event): event is NavigationEnd => event instanceof NavigationEnd),
|
||||
map((event) => event.urlAfterRedirects.split('?')[0]),
|
||||
),
|
||||
{ initialValue: this.router.url.split('?')[0] },
|
||||
);
|
||||
|
||||
isItemActive(item: NavItem): boolean {
|
||||
const path = this.currentPath();
|
||||
if (path === item.route || path.startsWith(`${item.route}/`)) {
|
||||
return true;
|
||||
}
|
||||
return (item.detailRoutePrefixes ?? []).some((prefix) =>
|
||||
path.startsWith(prefix),
|
||||
);
|
||||
}
|
||||
|
||||
readonly navItems = computed<NavItem[]>(() => {
|
||||
const items: NavItem[] = [
|
||||
@@ -99,6 +116,7 @@ export class AppMenuComponent {
|
||||
this.credentialService.organizationName(),
|
||||
),
|
||||
disabled: false,
|
||||
detailRoutePrefixes: ['/person/'],
|
||||
},
|
||||
{
|
||||
label: 'Bookmarks',
|
||||
@@ -124,6 +142,7 @@ export class AppMenuComponent {
|
||||
sort_by: 'works_count',
|
||||
sorting_order: SortDirection.Desc,
|
||||
},
|
||||
detailRoutePrefixes: ['/institution/'],
|
||||
},
|
||||
{
|
||||
label: 'Publikationen',
|
||||
@@ -137,6 +156,7 @@ export class AppMenuComponent {
|
||||
[workQueryParamKeys.sortBy]: WorkSortBy.CitedByCount,
|
||||
[workQueryParamKeys.sortDirection]: SortDirection.Desc,
|
||||
},
|
||||
detailRoutePrefixes: ['/publication/'],
|
||||
},
|
||||
{
|
||||
label: 'Themen',
|
||||
@@ -150,6 +170,7 @@ export class AppMenuComponent {
|
||||
[topicQueryParamKeys.sortBy]: TopicSortBy.CitedByCount,
|
||||
[topicQueryParamKeys.sortDirection]: SortDirection.Desc,
|
||||
},
|
||||
detailRoutePrefixes: ['/topic/'],
|
||||
},
|
||||
{
|
||||
label: 'Projekte',
|
||||
@@ -170,6 +191,7 @@ export class AppMenuComponent {
|
||||
icon: PATENT_ICON,
|
||||
route: '/patents',
|
||||
disabled: false,
|
||||
detailRoutePrefixes: ['/patent/'],
|
||||
},
|
||||
{
|
||||
label: 'Keywords',
|
||||
@@ -230,10 +252,7 @@ export class AppMenuComponent {
|
||||
];
|
||||
});
|
||||
|
||||
constructor(
|
||||
public credentialService: CredentialService,
|
||||
private readonly router: Router,
|
||||
) {}
|
||||
constructor(public credentialService: CredentialService) {}
|
||||
|
||||
isNavigating = computed(() => !!this.router.currentNavigation());
|
||||
readonly themeService = inject(ThemeService);
|
||||
|
||||
Reference in New Issue
Block a user