@ -1,37 +1,41 @@
import { OnInit , Directive , OnDestroy , Input , ViewContainerRef , TemplateRef , inject } from '@angular/core' ;
import { Directive , OnDestroy , ViewContainerRef , TemplateRef , inject , input , eff ect } from '@angular/core' ;
import { EMPTY , from , Observable , of , Subscription } from 'rxjs' ;
import { EMPTY , from , Observable , of , Subscription } from 'rxjs' ;
type VisibleInput = boolean | Promise < boolean > | Observable < boolean > | undefined | null ;
@Directive ( {
@Directive ( {
selector : '[abpVisible]' ,
selector : '[abpVisible]' ,
} )
} )
export class AbpVisibleDirective implements OnDestroy , OnInit {
export class AbpVisibleDirective implements OnDestroy {
private viewContainerRef = inject ( ViewContainerRef ) ;
private viewContainerRef = inject ( ViewContainerRef ) ;
private templateRef = inject < TemplateRef < unknown > > ( TemplateRef ) ;
private templateRef = inject < TemplateRef < unknown > > ( TemplateRef ) ;
conditionSubscription : Subscription | undefined ;
private conditionSubscription : Subscription | undefined ;
isVisible : boolean | undefined ;
private isVisible : boolean | undefined ;
private condition$ : Observable < boolean > = of ( false ) ;
@Input ( ) set abpVisible (
readonly abpVisible = input < VisibleInput > ( ) ;
value : boolean | Promise < boolean > | Observable < boolean > | undefined | null ,
) {
this . condition $ = checkType ( value ) ;
this . subscribeToCondition ( ) ;
}
private condition$ : Observable < boolean > = of ( false ) ;
constructor ( ) {
ngOnInit ( ) : void {
effect ( ( ) = > {
this . updateVisibility ( ) ;
const value = this . abpVisible ( ) ;
this . condition $ = checkType ( value ) ;
this . subscribeToCondition ( ) ;
} ) ;
}
}
ngOnDestroy ( ) : void {
ngOnDestroy ( ) : void {
this . conditionSubscription ? . unsubscribe ( ) ;
this . conditionSubscription ? . unsubscribe ( ) ;
}
}
private subscribeToCondition() {
private subscribeToCondition() {
this . conditionSubscription ? . unsubscribe ( ) ;
this . conditionSubscription = this . condition $ . subscribe ( value = > {
this . conditionSubscription = this . condition $ . subscribe ( value = > {
this . isVisible = value ;
this . isVisible = value ;
this . updateVisibility ( ) ;
this . updateVisibility ( ) ;
} ) ;
} ) ;
}
}
private updateVisibility() {
private updateVisibility() {
this . viewContainerRef . clear ( ) ;
this . viewContainerRef . clear ( ) ;
// it should be false not falsy
// it should be false not falsy
@ -42,7 +46,7 @@ export class AbpVisibleDirective implements OnDestroy, OnInit {
}
}
}
}
function checkType ( value : boolean | Promise < boolean > | Observable < boolean > | undefined | null ) {
function checkType ( value : VisibleInput ) {
if ( value instanceof Promise ) {
if ( value instanceof Promise ) {
return from ( value ) ;
return from ( value ) ;
} else if ( value instanceof Observable ) {
} else if ( value instanceof Observable ) {