import { Directive , EmbeddedViewRef , Input , OnDestroy , TemplateRef , ViewContainerRef } from '@angular/core' ; import { Subscription } from 'rxjs' ; import { filter } from 'rxjs/operators' ; import { ACLService } from './acl.service' ; import { ACLCanType } from './acl.type' ; @ Directive ( { selector : '[aclIf]' , exportAs : 'aclIf' } ) export class ACLIfDirective implements OnDestroy { static ngAcceptInputType_except : boolean | string | undefined | null ; private _value ! : ACLCanType ; private _change$ : Subscription ; private _thenTemplateRef : TemplateRef < void > | null = null ; private _elseTemplateRef : TemplateRef < void > | null = null ; private _thenViewRef : EmbeddedViewRef < void > | null = null ; private _elseViewRef : EmbeddedViewRef < void > | null = null ; private _except = false ; constructor ( templateRef , private srv , private _viewContainer ) { this . _change$ = this . srv . change . pipe ( filter ( r => r != null ) ) . subscribe ( ( ) => this . _updateView ( ) ) ; this . _thenTemplateRef = templateRef ; } @ Input ( ) set aclIf ( value ) { this . _value = value ; this . _updateView ( ) ; } @ Input ( ) set aclIfThen ( templateRef ) { this . _thenTemplateRef = templateRef ; this . _thenViewRef = null ; this . _updateView ( ) ; } @ Input ( ) set aclIfElse ( templateRef ) { this . _elseTemplateRef = templateRef ; this . _elseViewRef = null ; this . _updateView ( ) ; } @ Input ( ) set except ( value ) { this . _except = value != null && `${ value }` !== 'false' ; } get except ( ) : boolean { return this . _except ; } protected _updateView ( ) { const res = this . srv . can ( this . _value ) ; if ( ( res && ! this . except ) || ( ! res && this . except ) ) { if ( ! this . _thenViewRef ) { this . _viewContainer . clear ( ) ; this . _elseViewRef = null ; if ( this . _thenTemplateRef ) { this . _thenViewRef = this . _viewContainer . createEmbeddedView ( this . _thenTemplateRef ) ; } } } else { if ( ! this . _elseViewRef ) { this . _viewContainer . clear ( ) ; this . _thenViewRef = null ; if ( this . _elseTemplateRef ) { this . _elseViewRef = this . _viewContainer . createEmbeddedView ( this . _elseTemplateRef ) ; } } } } ngOnDestroy ( ) { this . _change$ . unsubscribe ( ) ; } }