From 9e62f6334190b41892e86cdf79fab9d3c449837a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Fri, 27 Dec 2019 11:38:10 +0300 Subject: [PATCH] refactor(core): add resetDefaultComponent method to replaceable-template directive #2404 --- .../replaceable-template.directive.ts | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts b/npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts index 35dabc52a5..2ac0bc8cfc 100644 --- a/npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts +++ b/npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts @@ -50,10 +50,7 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange ) { this.context = { initTemplate: ref => { - Object.keys(this.defaultComponentSubscriptions).forEach(key => { - this.defaultComponentSubscriptions[key].unsubscribe(); - }); - this.defaultComponentSubscriptions = {} as ABP.Dictionary; + this.resetDefaultComponent(); this.defaultComponentRef = ref; this.setDefaultComponentInputs(); }, @@ -73,6 +70,9 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange .subscribe((res = {} as ReplaceableComponents.ReplaceableComponent) => { this.vcRef.clear(); this.externalComponent = res.component; + if (this.defaultComponentRef) { + this.resetDefaultComponent(); + } if (res.component) { this.setProvidedData(); @@ -102,19 +102,27 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange ngOnDestroy() {} setDefaultComponentInputs() { - if (!this.defaultComponentRef || !this.data.inputs) return; - - for (const key in this.data.inputs) { - if (this.data.inputs.hasOwnProperty(key)) { - if (!compare(this.defaultComponentRef[key], this.data.inputs[key].value)) { - this.defaultComponentRef[key] = this.data.inputs[key].value; - - if (this.data.inputs[key].twoWay && !this.defaultComponentSubscriptions[key]) { - this.defaultComponentSubscriptions[key] = this.defaultComponentRef[ - `${key}Change` - ].subscribe(value => { - this.data.outputs[`${key}Change`](value); - }); + if (!this.defaultComponentRef || (!this.data.inputs && !this.data.outputs)) return; + + if (this.data.inputs) { + for (const key in this.data.inputs) { + if (this.data.inputs.hasOwnProperty(key)) { + if (!compare(this.defaultComponentRef[key], this.data.inputs[key].value)) { + this.defaultComponentRef[key] = this.data.inputs[key].value; + } + } + } + } + + if (this.data.outputs) { + for (const key in this.data.outputs) { + if (this.data.outputs.hasOwnProperty(key)) { + if (!this.defaultComponentSubscriptions[key]) { + this.defaultComponentSubscriptions[key] = this.defaultComponentRef[key].subscribe( + value => { + this.data.outputs[key](value); + }, + ); } } } @@ -145,4 +153,12 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChange ), }); } + + resetDefaultComponent() { + Object.keys(this.defaultComponentSubscriptions).forEach(key => { + this.defaultComponentSubscriptions[key].unsubscribe(); + }); + this.defaultComponentSubscriptions = {} as ABP.Dictionary; + this.defaultComponentRef = null; + } }