Browse Source

feat(core): add default component input and output mapping process to replaceable template directive

#2404
pull/2522/head
mehmet-erim 7 years ago
parent
commit
3c6ceb7388
  1. 54
      npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts

54
npm/ng-packs/packages/core/src/lib/directives/replaceable-template.directive.ts

@ -3,33 +3,42 @@ import {
Directive, Directive,
Injector, Injector,
Input, Input,
OnChanges,
OnDestroy, OnDestroy,
OnInit, OnInit,
SimpleChanges,
TemplateRef, TemplateRef,
Type, Type,
ViewContainerRef, ViewContainerRef,
SimpleChanges,
} from '@angular/core'; } from '@angular/core';
import { Store } from '@ngxs/store'; import { Store } from '@ngxs/store';
import compare from 'just-compare'; import { Subscription } from 'rxjs';
import { filter } from 'rxjs/operators'; import { filter } from 'rxjs/operators';
import { ABP } from '../models/common';
import { ReplaceableComponents } from '../models/replaceable-components'; import { ReplaceableComponents } from '../models/replaceable-components';
import { ReplaceableComponentsState } from '../states/replaceable-components.state'; import { ReplaceableComponentsState } from '../states/replaceable-components.state';
import { takeUntilDestroy } from '../utils/rxjs-utils'; import { takeUntilDestroy } from '../utils/rxjs-utils';
import compare from 'just-compare';
import snq from 'snq';
@Directive({ selector: '[abpReplaceableTemplate]' }) @Directive({ selector: '[abpReplaceableTemplate]' })
export class ReplaceableTemplateDirective implements OnInit, OnDestroy { export class ReplaceableTemplateDirective implements OnInit, OnDestroy, OnChanges {
private context = {}; private context = {};
@Input('abpReplaceableTemplate') @Input('abpReplaceableTemplate')
data: { inputs: any; outputs: any; componentKey: string }; data: { inputs: any; outputs: any; componentKey: string };
providedData = { inputs: {}, outputs: {} } as { inputs: any; outputs: any; componentKey: string }; providedData = { inputs: {}, outputs: {} } as ReplaceableComponents.ReplaceableTemplateData<
any,
any
>;
externalComponent: Type<any> = null; // externalComponent must equal to null externalComponent: Type<any> = null; // externalComponent must equal to null
defaultComponentRef: any; defaultComponentRef: any;
defaultComponentSubscriptions = {} as ABP.Dictionary<Subscription>;
constructor( constructor(
private injector: Injector, private injector: Injector,
private templateRef: TemplateRef<any>, private templateRef: TemplateRef<any>,
@ -39,12 +48,12 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy {
) { ) {
this.context = { this.context = {
initTemplate: ref => { initTemplate: ref => {
Object.keys(this.defaultComponentSubscriptions).forEach(key => {
this.defaultComponentSubscriptions[key].unsubscribe();
});
this.defaultComponentSubscriptions = {} as ABP.Dictionary<Subscription>;
this.defaultComponentRef = ref; this.defaultComponentRef = ref;
this.setDefaultComponentInputs(); this.setDefaultComponentInputs();
setTimeout(() => {
ref.providerKey = 'admin';
ref.visible = true;
}, 5000);
}, },
}; };
} }
@ -81,16 +90,37 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy {
} }
ngOnChanges(changes: SimpleChanges) { ngOnChanges(changes: SimpleChanges) {
console.log(changes); if (snq(() => changes.data.currentValue.inputs) && this.defaultComponentRef) {
this.setDefaultComponentInputs();
}
} }
ngOnDestroy() {} ngOnDestroy() {}
setDefaultComponentInputs() { setDefaultComponentInputs() {
if (!this.defaultComponentRef) return; 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);
});
}
}
}
}
} }
setProvidedData() { setProvidedData() {
this.providedData = { ...this.data, inputs: {} };
if (!this.providedData.inputs) return;
Object.defineProperties(this.providedData.inputs, { Object.defineProperties(this.providedData.inputs, {
...Object.keys(this.data.inputs).reduce( ...Object.keys(this.data.inputs).reduce(
(acc, key) => ({ (acc, key) => ({
@ -110,9 +140,5 @@ export class ReplaceableTemplateDirective implements OnInit, OnDestroy {
{}, {},
), ),
}); });
this.providedData.outputs = this.data.outputs;
console.warn(this.providedData);
} }
} }

Loading…
Cancel
Save