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 { ABP, ConfigState, RoutesService, takeUntilDestroy, TreeNode } from '@abp/ng.core'; |
||||
import { Component, Input, TrackByFunction } from '@angular/core'; |
import { Component, Input, OnDestroy, TrackByFunction } from '@angular/core'; |
||||
import { Store } from '@ngxs/store'; |
import { Store } from '@ngxs/store'; |
||||
|
import compare from 'just-compare'; |
||||
|
import { distinctUntilChanged, skip } 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 implements OnDestroy { |
||||
@Input() smallScreen: boolean; |
@Input() smallScreen: boolean; |
||||
|
|
||||
trackByFn: TrackByFunction<TreeNode<ABP.Route>> = (_, item) => item.name; |
isShow = true; |
||||
|
|
||||
constructor(public readonly routes: RoutesService, private store: Store) {} |
|
||||
|
|
||||
findRequiredPolicy(route: TreeNode<ABP.Route>): boolean { |
|
||||
if (route.requiredPolicy || !route.children?.length) return true; |
|
||||
|
|
||||
const policies = getPolicies(route.children); |
trackByFn: TrackByFunction<TreeNode<ABP.Route>> = (_, item) => item.name; |
||||
|
|
||||
let isVisible = true; |
|
||||
for (let i = 0; i < policies.length; i++) { |
|
||||
if (!this.store.selectSnapshot(ConfigState.getGrantedPolicy(policies[i]))) { |
|
||||
isVisible = false; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
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