mirror of https://github.com/abpframework/abp.git
2 changed files with 20 additions and 35 deletions
@ -1,45 +1,29 @@ |
|||
import { ABP, RoutesService, TreeNode, LocalizationService, ConfigState } from '@abp/ng.core'; |
|||
import { Component, Input, TrackByFunction } from '@angular/core'; |
|||
import { ABP, ConfigState, RoutesService, takeUntilDestroy, TreeNode } from '@abp/ng.core'; |
|||
import { Component, Input, OnDestroy, TrackByFunction } from '@angular/core'; |
|||
import { Store } from '@ngxs/store'; |
|||
import compare from 'just-compare'; |
|||
import { distinctUntilChanged, skip } from 'rxjs/operators'; |
|||
|
|||
@Component({ |
|||
selector: 'abp-routes', |
|||
templateUrl: 'routes.component.html', |
|||
}) |
|||
export class RoutesComponent { |
|||
export class RoutesComponent implements OnDestroy { |
|||
@Input() smallScreen: boolean; |
|||
|
|||
trackByFn: TrackByFunction<TreeNode<ABP.Route>> = (_, item) => item.name; |
|||
|
|||
constructor(public readonly routes: RoutesService, private store: Store) {} |
|||
|
|||
findRequiredPolicy(route: TreeNode<ABP.Route>): boolean { |
|||
if (route.requiredPolicy || !route.children?.length) return true; |
|||
isShow = true; |
|||
|
|||
const policies = getPolicies(route.children); |
|||
|
|||
let isVisible = true; |
|||
for (let i = 0; i < policies.length; i++) { |
|||
if (!this.store.selectSnapshot(ConfigState.getGrantedPolicy(policies[i]))) { |
|||
isVisible = false; |
|||
break; |
|||
} |
|||
} |
|||
trackByFn: TrackByFunction<TreeNode<ABP.Route>> = (_, item) => item.name; |
|||
|
|||
return isVisible; |
|||
constructor(public readonly routes: RoutesService, private store: Store) { |
|||
this.store |
|||
.select(ConfigState.getDeep('auth.grantedPolicies')) |
|||
.pipe(distinctUntilChanged(compare), skip(1), takeUntilDestroy(this)) |
|||
.subscribe(() => { |
|||
this.isShow = false; |
|||
setTimeout(() => (this.isShow = true), 0); |
|||
}); |
|||
} |
|||
} |
|||
|
|||
function getPolicies(routes: TreeNode<ABP.Route>[]): string[] { |
|||
return routes.reduce((acc, val) => { |
|||
if (val.requiredPolicy) acc.push(val.requiredPolicy); |
|||
|
|||
if (val.children?.length) { |
|||
const childPolicies = getPolicies(val.children); |
|||
|
|||
if (childPolicies?.length) acc.push(...childPolicies); |
|||
} |
|||
|
|||
return acc; |
|||
}, []); |
|||
ngOnDestroy() {} |
|||
} |
|||
|
|||
Loading…
Reference in new issue