Browse Source

refactor: deprecate the attribute usage of the permission directive

pull/6275/head
mehmet-erim 6 years ago
parent
commit
22cc37c28b
  1. 44
      npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts

44
npm/ng-packs/packages/core/src/lib/directives/permission.directive.ts

@ -1,10 +1,10 @@
import { import {
ChangeDetectorRef,
Directive, Directive,
ElementRef, ElementRef,
Input, Input,
OnChanges, OnChanges,
OnDestroy, OnDestroy,
OnInit,
Optional, Optional,
Renderer2, Renderer2,
SimpleChanges, SimpleChanges,
@ -17,43 +17,47 @@ import { PermissionService } from '../services/permission.service';
@Directive({ @Directive({
selector: '[abpPermission]', selector: '[abpPermission]',
}) })
export class PermissionDirective implements OnInit, OnDestroy, OnChanges { export class PermissionDirective implements OnDestroy, OnChanges {
@Input('abpPermission') condition: string; @Input('abpPermission') condition: string;
subscription: Subscription; subscription: Subscription;
constructor( constructor(
private elRef: ElementRef, private elRef: ElementRef<HTMLElement>,
private renderer: Renderer2, private renderer: Renderer2,
@Optional() private templateRef: TemplateRef<any>, @Optional() private templateRef: TemplateRef<any>,
private vcRef: ViewContainerRef, private vcRef: ViewContainerRef,
private permissionService: PermissionService, private permissionService: PermissionService,
private cdRef: ChangeDetectorRef,
) {} ) {}
private check() { private check() {
if (this.subscription) { if (this.subscription) {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
this.subscription = this.permissionService this.subscription = this.permissionService
.getGrantedPolicy$(this.condition) .getGrantedPolicy$(this.condition)
.subscribe(isGranted => { .subscribe(isGranted => {
if (this.templateRef && isGranted) { if (this.templateRef) this.initStructural(isGranted);
this.vcRef.clear(); else this.initAttribute(isGranted);
this.vcRef.createEmbeddedView(this.templateRef);
} else if (this.templateRef && !isGranted) { this.cdRef.detectChanges();
this.vcRef.clear();
} else if (!isGranted && !this.templateRef) {
this.renderer.removeChild(
(this.elRef.nativeElement as HTMLElement).parentElement,
this.elRef.nativeElement,
);
}
}); });
} }
ngOnInit() { private initStructural(isGranted: boolean) {
if (this.templateRef && !this.condition) { this.vcRef.clear();
this.vcRef.createEmbeddedView(this.templateRef);
if (isGranted) this.vcRef.createEmbeddedView(this.templateRef);
}
/**
* @deprecated Will be deleted in v5.0
*/
private initAttribute(isGranted: boolean) {
if (!isGranted) {
this.renderer.removeChild(this.elRef.nativeElement.parentElement, this.elRef.nativeElement);
} }
} }
@ -61,9 +65,7 @@ export class PermissionDirective implements OnInit, OnDestroy, OnChanges {
if (this.subscription) this.subscription.unsubscribe(); if (this.subscription) this.subscription.unsubscribe();
} }
ngOnChanges({ condition }: SimpleChanges) { ngOnChanges() {
if ((condition || { currentValue: null }).currentValue) { this.check();
this.check();
}
} }
} }

Loading…
Cancel
Save