|
|
@ -1,4 +1,14 @@ |
|
|
import { Directive, ElementRef, Input, OnDestroy, OnInit, Optional, Renderer2 } from '@angular/core'; |
|
|
import { |
|
|
|
|
|
Directive, |
|
|
|
|
|
ElementRef, |
|
|
|
|
|
Input, |
|
|
|
|
|
OnDestroy, |
|
|
|
|
|
OnInit, |
|
|
|
|
|
Renderer2, |
|
|
|
|
|
ViewContainerRef, |
|
|
|
|
|
TemplateRef, |
|
|
|
|
|
Optional, |
|
|
|
|
|
} from '@angular/core'; |
|
|
import { Store } from '@ngxs/store'; |
|
|
import { Store } from '@ngxs/store'; |
|
|
import { ConfigState } from '../states'; |
|
|
import { ConfigState } from '../states'; |
|
|
import { takeUntilDestroy } from '../utils'; |
|
|
import { takeUntilDestroy } from '../utils'; |
|
|
@ -9,7 +19,13 @@ import { takeUntilDestroy } from '../utils'; |
|
|
export class PermissionDirective implements OnInit, OnDestroy { |
|
|
export class PermissionDirective implements OnInit, OnDestroy { |
|
|
@Input('abpPermission') condition: string; |
|
|
@Input('abpPermission') condition: string; |
|
|
|
|
|
|
|
|
constructor(@Optional() private elRef: ElementRef, private renderer: Renderer2, private store: Store) {} |
|
|
constructor( |
|
|
|
|
|
private elRef: ElementRef, |
|
|
|
|
|
private renderer: Renderer2, |
|
|
|
|
|
private store: Store, |
|
|
|
|
|
@Optional() private templateRef: TemplateRef<any>, |
|
|
|
|
|
private vcRef: ViewContainerRef, |
|
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
ngOnInit() { |
|
|
ngOnInit() { |
|
|
if (this.condition) { |
|
|
if (this.condition) { |
|
|
@ -17,7 +33,11 @@ export class PermissionDirective implements OnInit, OnDestroy { |
|
|
.select(ConfigState.getGrantedPolicy(this.condition)) |
|
|
.select(ConfigState.getGrantedPolicy(this.condition)) |
|
|
.pipe(takeUntilDestroy(this)) |
|
|
.pipe(takeUntilDestroy(this)) |
|
|
.subscribe(isGranted => { |
|
|
.subscribe(isGranted => { |
|
|
if (!isGranted) { |
|
|
if (this.templateRef && isGranted) { |
|
|
|
|
|
this.vcRef.createEmbeddedView(this.templateRef); |
|
|
|
|
|
} else if (this.templateRef && !isGranted) { |
|
|
|
|
|
this.vcRef.clear(); |
|
|
|
|
|
} else if (!isGranted && !this.templateRef) { |
|
|
this.renderer.removeChild( |
|
|
this.renderer.removeChild( |
|
|
(this.elRef.nativeElement as HTMLElement).parentElement, |
|
|
(this.elRef.nativeElement as HTMLElement).parentElement, |
|
|
this.elRef.nativeElement, |
|
|
this.elRef.nativeElement, |
|
|
|