68067: Refactoring menu-component resolving route menu data to menu effects

This commit is contained in:
Kristof De Langhe
2020-05-26 11:54:33 +02:00
parent 79eecc7c76
commit 0b67bd54fb
8 changed files with 103 additions and 88 deletions

View File

@@ -8,7 +8,6 @@ import { GenericConstructor } from '../../core/shared/generic-constructor';
import { hasNoValue, hasValue } from '../empty.util';
import { MenuSectionComponent } from './menu-section/menu-section.component';
import { getComponentForMenu } from './menu-section.decorator';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { Subscription } from 'rxjs/internal/Subscription';
/**
@@ -70,17 +69,13 @@ export class MenuComponent implements OnInit, OnDestroy {
*/
subs: Subscription[] = [];
constructor(protected menuService: MenuService,
protected injector: Injector,
protected route: ActivatedRoute,
protected router: Router) {
constructor(protected menuService: MenuService, protected injector: Injector) {
}
/**
* Sets all instance variables to their initial values
*/
ngOnInit(): void {
this.initSections();
this.menuCollapsed = this.menuService.isMenuCollapsed(this.menuID);
this.menuPreviewCollapsed = this.menuService.isMenuPreviewCollapsed(this.menuID);
this.menuVisible = this.menuService.isMenuVisible(this.menuID);
@@ -93,49 +88,6 @@ export class MenuComponent implements OnInit, OnDestroy {
}));
}
/**
* Initialize all menu sections and add them to the menu's store
*/
initSections() {
this.subs.push(this.router.events.pipe(
filter((e): e is NavigationEnd => e instanceof NavigationEnd),
tap(() => this.menuService.resetSections(this.menuID)),
map(() => this.resolveMenuSections(this.route.root))
).subscribe((sections: MenuSection[]) => {
this.createMenu();
sections.forEach((section: MenuSection) => {
this.menuService.addSection(this.menuID, section);
});
}));
}
/**
* Initialize all static menu sections and items for this menu
* These sections will be available on any route. Route specific sections should be defined in the route's data instead.
*/
createMenu() {
// Override this method to create and add a standard set of menu sections that should be available for the current menu type on all routes
}
/**
* Resolve menu sections defined in the current route data (including parent routes)
* @param route The route to resolve data for
*/
resolveMenuSections(route: ActivatedRoute): MenuSection[] {
const data = route.snapshot.data;
const last: boolean = hasNoValue(route.firstChild);
if (hasValue(data) && hasValue(data.menu) && hasValue(data.menu[this.menuID])) {
if (!last) {
return [...data.menu[this.menuID], ...this.resolveMenuSections(route.firstChild)]
} else {
return [...data.menu[this.menuID]];
}
}
return !last ? this.resolveMenuSections(route.firstChild) : [];
}
/**
* Collapse this menu when it's currently expanded, expand it when its currently collapsed
* @param {Event} event The user event that triggered this method