Browse Source

feat(core): add template ref to permission directive

pull/2261/head
mehmet-erim 7 years ago
parent
commit
eedab03ad1
  1. 26
      npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts

26
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<any>,
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,

Loading…
Cancel
Save