From eedab03ad1634097bebf21542071933768b9e5e5 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Tue, 26 Nov 2019 17:05:34 +0300 Subject: [PATCH] feat(core): add template ref to permission directive --- .../lib/directives/permission.directive.ts | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts b/npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts index 68a5ae6f75..1047d6ba30 100644 --- a/npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts +++ b/npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts @@ -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 { ConfigState } from '../states'; import { takeUntilDestroy } from '../utils'; @@ -9,7 +19,13 @@ import { takeUntilDestroy } from '../utils'; export class PermissionDirective implements OnInit, OnDestroy { @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, + private vcRef: ViewContainerRef, + ) {} ngOnInit() { if (this.condition) { @@ -17,7 +33,11 @@ export class PermissionDirective implements OnInit, OnDestroy { .select(ConfigState.getGrantedPolicy(this.condition)) .pipe(takeUntilDestroy(this)) .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.elRef.nativeElement as HTMLElement).parentElement, this.elRef.nativeElement,