2025-12-04 21:07:18 +03:30
|
|
|
import { Injectable, inject, signal } from '@angular/core';
|
|
|
|
|
import { NavigationStart, Router } from '@angular/router';
|
|
|
|
|
import { MenuItem } from 'primeng/api';
|
|
|
|
|
import { filter } from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
|
export class BreadcrumbService {
|
2026-03-11 20:42:15 +03:30
|
|
|
readonly _items = signal<MenuItem[]>([]);
|
2025-12-04 21:07:18 +03:30
|
|
|
|
|
|
|
|
private router = inject(Router);
|
|
|
|
|
|
|
|
|
|
constructor() {
|
2026-03-11 20:42:15 +03:30
|
|
|
// Clear breadcrumb items only when the path changes (not on query string changes)
|
|
|
|
|
let lastPath = '';
|
|
|
|
|
this.router.events.pipe(filter((e) => e instanceof NavigationStart)).subscribe((e) => {
|
|
|
|
|
const nav = e as NavigationStart;
|
|
|
|
|
const urlPath = nav.url.split('?')[0];
|
|
|
|
|
|
|
|
|
|
if (!lastPath || (lastPath && urlPath !== lastPath)) {
|
|
|
|
|
this.clear();
|
|
|
|
|
}
|
|
|
|
|
lastPath = urlPath;
|
2025-12-04 21:07:18 +03:30
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 20:42:15 +03:30
|
|
|
setItems(items: MenuItem[]) {
|
|
|
|
|
this._items.set(this.mapItems(items));
|
2025-12-04 21:07:18 +03:30
|
|
|
}
|
|
|
|
|
|
2026-03-11 20:42:15 +03:30
|
|
|
private readonly mapItems = (items: MenuItem[]) =>
|
|
|
|
|
items.map((item) => ({
|
|
|
|
|
...item,
|
|
|
|
|
label: item.title || '',
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
addItems(items: MenuItem[]) {
|
|
|
|
|
this._items.update((value) => ({ ...value, ...this.mapItems(items) }));
|
2025-12-04 21:07:18 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
this._items.set([]);
|
|
|
|
|
}
|
|
|
|
|
}
|