feat: use RoutesService in RoutesComponent

pull/4377/head
Arman Ozak 5 years ago
parent 9c0fe4c8e1
commit 962e636640

@ -1,6 +1,6 @@
<ul class="navbar-nav"> <ul class="navbar-nav">
<ng-container <ng-container
*ngFor="let route of visibleRoutes$ | async; trackBy: trackByFn" *ngFor="let route of routes.visible$ | async; trackBy: trackByFn"
[ngTemplateOutlet]="route?.children?.length ? dropdownLink : defaultLink" [ngTemplateOutlet]="route?.children?.length ? dropdownLink : defaultLink"
[ngTemplateOutletContext]="{ $implicit: route }" [ngTemplateOutletContext]="{ $implicit: route }"
> >
@ -8,7 +8,7 @@
<ng-template #defaultLink let-route> <ng-template #defaultLink let-route>
<li class="nav-item" *abpPermission="route.requiredPolicy"> <li class="nav-item" *abpPermission="route.requiredPolicy">
<a class="nav-link" [routerLink]="[route.url]" <a class="nav-link" [routerLink]="[route.path]"
><i *ngIf="route.iconClass" [ngClass]="route.iconClass"></i> ><i *ngIf="route.iconClass" [ngClass]="route.iconClass"></i>
{{ route.name | abpLocalization }}</a {{ route.name | abpLocalization }}</a
> >
@ -44,27 +44,23 @@
(click)="$event.preventDefault(); $event.stopPropagation()" (click)="$event.preventDefault(); $event.stopPropagation()"
[class.d-block]="smallScreen && navbarRootDropdown.expand" [class.d-block]="smallScreen && navbarRootDropdown.expand"
> >
<ng-template <ng-container *ngTemplateOutlet="forTemplate; context: { $implicit: route }"></ng-container>
#forTemplate
ngFor
[ngForOf]="route.children"
[ngForTrackBy]="trackByFn"
[ngForTemplate]="childWrapper"
></ng-template>
</div> </div>
</li> </li>
</ng-template> </ng-template>
<ng-template #childWrapper let-child> <ng-template #forTemplate let-route>
<ng-template <ng-container *ngFor="let child of route.children">
[ngTemplateOutlet]="child?.children?.length ? dropdownChild : defaultChild" <ng-template
[ngTemplateOutletContext]="{ $implicit: child }" [ngTemplateOutlet]="child.children?.length ? dropdownChild : defaultChild"
></ng-template> [ngTemplateOutletContext]="{ $implicit: child }"
></ng-template>
</ng-container>
</ng-template> </ng-template>
<ng-template #defaultChild let-child> <ng-template #defaultChild let-child>
<div class="dropdown-submenu" *abpPermission="child.requiredPolicy"> <div class="dropdown-submenu" *abpPermission="child.requiredPolicy">
<a class="dropdown-item" [routerLink]="[child.url]"> <a class="dropdown-item" [routerLink]="[child.path]">
<i *ngIf="child.iconClass" [ngClass]="child.iconClass"></i> <i *ngIf="child.iconClass" [ngClass]="child.iconClass"></i>
{{ child.name | abpLocalization }}</a {{ child.name | abpLocalization }}</a
> >
@ -97,12 +93,7 @@
class="dropdown-menu border-0 shadow-sm" class="dropdown-menu border-0 shadow-sm"
[class.d-block]="smallScreen && dropdownSubmenu.isOpen()" [class.d-block]="smallScreen && dropdownSubmenu.isOpen()"
> >
<ng-template <ng-container *ngTemplateOutlet="forTemplate; context: { $implicit: child }"></ng-container>
ngFor
[ngForOf]="child.children"
[ngForTrackBy]="trackByFn"
[ngForTemplate]="childWrapper"
></ng-template>
</div> </div>
</div> </div>
</ng-template> </ng-template>

@ -1,37 +1,14 @@
import { ABP, ConfigState } from '@abp/ng.core'; import { ABP, RoutesService, TreeNode } from '@abp/ng.core';
import { Component, Input, Renderer2, TrackByFunction } from '@angular/core'; import { Component, Input, TrackByFunction } from '@angular/core';
import { Select } from '@ngxs/store';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Component({ @Component({
selector: 'abp-routes', selector: 'abp-routes',
templateUrl: 'routes.component.html', templateUrl: 'routes.component.html',
}) })
export class RoutesComponent { export class RoutesComponent {
@Select(ConfigState.getOne('routes')) @Input() smallScreen: boolean;
routes$: Observable<ABP.FullRoute[]>;
@Input() trackByFn: TrackByFunction<TreeNode<ABP.Route>> = (_, item) => item.name;
smallScreen: boolean;
get visibleRoutes$(): Observable<ABP.FullRoute[]> { constructor(public readonly routes: RoutesService) {}
return this.routes$.pipe(map(routes => getVisibleRoutes(routes)));
}
trackByFn: TrackByFunction<ABP.FullRoute> = (_, item) => item.name;
constructor(private renderer: Renderer2) {}
}
function getVisibleRoutes(routes: ABP.FullRoute[]) {
return routes.reduce((acc, val) => {
if (val.invisible) return acc;
if (val.children && val.children.length) {
val.children = getVisibleRoutes(val.children);
}
return [...acc, val];
}, []);
} }

Loading…
Cancel
Save