mirror of https://github.com/abpframework/abp.git
committed by
GitHub
6 changed files with 77 additions and 9 deletions
@ -1,17 +1,18 @@ |
|||||
<ul class="navbar-nav"> |
<ul class="navbar-nav"> |
||||
<ng-container *ngFor="let item of navItems.items$ | async; trackBy: trackByFn"> |
<ng-container *ngFor="let item of navItems.items$ | async; trackBy: trackByFn"> |
||||
<ng-container *ngIf="item.visible && item.visible()"> |
<ng-container *abpVisible="!item.visible || item.visible(item)"> |
||||
<li class="nav-item d-flex align-items-center" *abpPermission="item.requiredPolicy"> |
<li class="nav-item d-flex align-items-center" *abpPermission="item.requiredPolicy"> |
||||
<ng-container |
<ng-container |
||||
*ngIf="item.component; else htmlTemplate" |
|
||||
|
*ngIf="item.component; else htmlTemplate" |
||||
[ngComponentOutlet]="item.component" |
[ngComponentOutlet]="item.component" |
||||
[ngComponentOutletInjector]="item | toInjector" |
[ngComponentOutletInjector]="item | toInjector" |
||||
></ng-container> |
></ng-container> |
||||
|
|
||||
<ng-template #htmlTemplate> |
<ng-template #htmlTemplate> |
||||
<div [innerHTML]="item.html" (click)="item.action ? item.action() : null"></div> |
<div [innerHTML]="item.html" (click)="item.action ? item.action() : null"></div> |
||||
</ng-template> |
</ng-template> |
||||
</li> |
</li> |
||||
</ng-container> |
</ng-container> |
||||
</ng-container> |
</ng-container> |
||||
</ul> |
</ul> |
||||
@ -0,0 +1,59 @@ |
|||||
|
import { OnInit, Directive, OnDestroy, Input, ViewContainerRef, TemplateRef } from '@angular/core'; |
||||
|
import { EMPTY, from, Observable, of, Subscription } from 'rxjs'; |
||||
|
|
||||
|
@Directive({ |
||||
|
selector: '[abpVisible]', |
||||
|
}) |
||||
|
export class AbpVisibleDirective implements OnDestroy, OnInit { |
||||
|
conditionSubscription: Subscription | undefined; |
||||
|
isVisible: boolean | undefined; |
||||
|
|
||||
|
@Input('abpVisible') set abpVisible( |
||||
|
value: boolean | Promise<boolean> | Observable<boolean> | undefined | null, |
||||
|
) { |
||||
|
this.condition$ = checkType(value); |
||||
|
this.subscribeToCondition(); |
||||
|
} |
||||
|
|
||||
|
private condition$: Observable<boolean> = of(false); |
||||
|
|
||||
|
constructor( |
||||
|
private viewContainerRef: ViewContainerRef, |
||||
|
private templateRef: TemplateRef<unknown>, |
||||
|
) {} |
||||
|
ngOnInit(): void { |
||||
|
this.updateVisibility(); |
||||
|
} |
||||
|
ngOnDestroy(): void { |
||||
|
this.conditionSubscription?.unsubscribe(); |
||||
|
} |
||||
|
|
||||
|
private subscribeToCondition() { |
||||
|
this.conditionSubscription = this.condition$.subscribe(value => { |
||||
|
this.isVisible = value; |
||||
|
this.updateVisibility(); |
||||
|
}); |
||||
|
} |
||||
|
private updateVisibility() { |
||||
|
this.viewContainerRef.clear(); |
||||
|
// it should be false not falsy
|
||||
|
if (this.isVisible === false) { |
||||
|
return; |
||||
|
} |
||||
|
this.viewContainerRef.createEmbeddedView(this.templateRef); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function checkType(value: boolean | Promise<boolean> | Observable<boolean> | undefined | null) { |
||||
|
if (value instanceof Promise) { |
||||
|
return from(value); |
||||
|
} else if (value instanceof Observable) { |
||||
|
return value; |
||||
|
} else if (typeof value === 'boolean') { |
||||
|
return of(value); |
||||
|
} else if (value === undefined || value === null) { |
||||
|
return of(true); |
||||
|
} else { |
||||
|
return EMPTY; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue