Merge pull request #8274 from abpframework/feat/page-improvement

feat: improve page render strategy interface
pull/8283/head
Muhammed Altuğ 5 years ago committed by GitHub
commit 096f3cc5ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,12 +13,13 @@ import {
SimpleChanges, SimpleChanges,
SimpleChange, SimpleChange,
} from '@angular/core'; } from '@angular/core';
import { Observable, Subscription, of } from 'rxjs';
export interface PageRenderStrategy { export interface PageRenderStrategy {
shouldRender(type: string); shouldRender(type?: string): boolean | Observable<boolean>;
onInit?(type: string, injector: Injector, context?: any); onInit?(type?: string, injector?: Injector, context?: any): void;
onDestroy?(type: string, injector?: Injector, context?: any); onDestroy?(type?: string, injector?: Injector, context?: any): void;
onContextUpdate?(change: SimpleChange); onContextUpdate?(change?: SimpleChange): void;
} }
export const PAGE_RENDER_STRATEGY = new InjectionToken<PageRenderStrategy>('PAGE_RENDER_STRATEGY'); export const PAGE_RENDER_STRATEGY = new InjectionToken<PageRenderStrategy>('PAGE_RENDER_STRATEGY');
@ -27,11 +28,15 @@ export const PAGE_RENDER_STRATEGY = new InjectionToken<PageRenderStrategy>('PAGE
export class PagePartDirective implements OnInit, OnDestroy, OnChanges { export class PagePartDirective implements OnInit, OnDestroy, OnChanges {
hasRendered = false; hasRendered = false;
type: string; type: string;
subscription: Subscription;
@Input('abpPagePartContext') context: any;
@Input() set abpPagePart(type: string) { @Input() set abpPagePart(type: string) {
this.type = type; this.type = type;
const shouldRender = this.shouldRender(type); this.createRenderStream(type);
}
render = (shouldRender: boolean) => {
if (shouldRender && !this.hasRendered) { if (shouldRender && !this.hasRendered) {
this.viewContainer.createEmbeddedView(this.templateRef); this.viewContainer.createEmbeddedView(this.templateRef);
this.hasRendered = true; this.hasRendered = true;
@ -39,9 +44,7 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges {
this.viewContainer.clear(); this.viewContainer.clear();
this.hasRendered = false; this.hasRendered = false;
} }
} };
@Input('abpPagePartContext') context: any;
constructor( constructor(
private templateRef: TemplateRef<any>, private templateRef: TemplateRef<any>,
@ -63,6 +66,8 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges {
} }
ngOnDestroy() { ngOnDestroy() {
this.clearSubscription();
if (this.renderLogic?.onDestroy) { if (this.renderLogic?.onDestroy) {
this.renderLogic.onDestroy(this.type, this.injector, this.context); this.renderLogic.onDestroy(this.type, this.injector, this.context);
} }
@ -70,8 +75,21 @@ export class PagePartDirective implements OnInit, OnDestroy, OnChanges {
shouldRender(type: string) { shouldRender(type: string) {
if (this.renderLogic) { if (this.renderLogic) {
return this.renderLogic.shouldRender(type); const willRender = this.renderLogic.shouldRender(type);
return willRender instanceof Observable ? willRender : of(willRender);
}
return of(true);
}
protected createRenderStream(type: string) {
this.clearSubscription();
this.subscription = this.shouldRender(type).subscribe(this.render);
}
protected clearSubscription() {
if (this.subscription) {
this.subscription.unsubscribe();
} }
return true;
} }
} }

Loading…
Cancel
Save