From b36339ff9f20309c6c3331b75cdd24e21a0b2895 Mon Sep 17 00:00:00 2001 From: Bogdan Cilibiu Date: Wed, 4 Jul 2018 09:02:30 +0300 Subject: [PATCH] ifExperimental else --- src/app/directives/experimental.directive.ts | 46 +++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/src/app/directives/experimental.directive.ts b/src/app/directives/experimental.directive.ts index 3546f8c50..09c30bed9 100644 --- a/src/app/directives/experimental.directive.ts +++ b/src/app/directives/experimental.directive.ts @@ -23,7 +23,7 @@ * along with Alfresco. If not, see . */ -import { Directive, TemplateRef, ViewContainerRef, Input } from '@angular/core'; +import { Directive, TemplateRef, ViewContainerRef, Input, EmbeddedViewRef } from '@angular/core'; import { AppConfigService, StorageService } from '@alfresco/adf-core'; import { environment } from '../../environments/environment'; @@ -32,6 +32,10 @@ import { environment } from '../../environments/environment'; selector: '[ifExperimental]' }) export class ExperimentalDirective { + private elseTemplateRef: TemplateRef; + private elseViewRef: EmbeddedViewRef; + private shouldRender: boolean; + constructor( private templateRef: TemplateRef, private viewContainerRef: ViewContainerRef, @@ -43,19 +47,49 @@ export class ExperimentalDirective { const key = `experimental.${featureKey}`; const override = this.storage.getItem(key); + if (override === 'true') { - this.viewContainerRef.createEmbeddedView(this.templateRef); - return; + this.shouldRender = true; } if (!environment.production) { const value = this.config.get(key); if (value === true || value === 'true') { - this.viewContainerRef.createEmbeddedView(this.templateRef); - return; + this.shouldRender = true; } } - this.viewContainerRef.clear(); + if (override !== 'true' && environment.production) { + this.shouldRender = false; + } + + this.updateView(); + } + + @Input() set ifExperimentalElse(templateRef: TemplateRef) { + this.elseTemplateRef = templateRef; + this.elseViewRef = null; + this.updateView(); + } + + private updateView() { + if (this.shouldRender) { + this.viewContainerRef.clear(); + this.elseViewRef = null; + + if (this.templateRef) { + this.viewContainerRef.createEmbeddedView(this.templateRef); + } + } else { + if (this.elseViewRef) { + return; + } + + this.viewContainerRef.clear(); + + if (this.elseTemplateRef) { + this.elseViewRef = this.viewContainerRef.createEmbeddedView(this.elseTemplateRef); + } + } } }