Skip to content

Commit

Permalink
fix: avoid many fn calling problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmet-erim committed Jul 29, 2020
1 parent e512923 commit e1d20c7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<div class="form-group" [abpPermission]="prop.permission" [ngSwitch]="getComponent(prop)">
<ng-template ngSwitchCase="input">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<input
[id]="prop.id"
[formControlName]="prop.name"
[autocomplete]="prop.autocomplete"
[type]="getType(prop)"
[abpDisabled]="prop.disabled(data)"
[readonly]="prop.readonly(data)"
[abpDisabled]="disabled"
[readonly]="readonly"
class="form-control"
/>
</ng-template>
Expand All @@ -19,38 +17,34 @@
<input
[id]="prop.id"
[formControlName]="prop.name"
[abpDisabled]="prop.disabled(data)"
[abpDisabled]="disabled"
type="checkbox"
class="custom-control-input"
/>
<label [htmlFor]="prop.id" class="custom-control-label"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>{{ prop.displayName | abpLocalization }} {{ asterisk }}</label
>
</div>
</ng-template>

<ng-template ngSwitchCase="select">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<select
[id]="prop.id"
[formControlName]="prop.name"
[abpDisabled]="prop.disabled(data)"
[abpDisabled]="disabled"
class="custom-select form-control"
>
<option
*ngFor="let option of prop.options(data) | async; trackBy: track.by('value')"
*ngFor="let option of options$ | async; trackBy: track.by('value')"
[ngValue]="option.value"
>{{ option.key }}</option
>
</select>
</ng-template>

<ng-template ngSwitchCase="date">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<input
[id]="prop.id"
[formControlName]="prop.name"
Expand All @@ -64,28 +58,22 @@
</ng-template>

<ng-template ngSwitchCase="time">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<ngb-timepicker [formControlName]="prop.name"></ngb-timepicker>
</ng-template>

<ng-template ngSwitchCase="dateTime">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<abp-date-time-picker [prop]="prop"></abp-date-time-picker>
</ng-template>

<ng-template ngSwitchCase="textarea">
<label [htmlFor]="prop.id"
>{{ prop.displayName | abpLocalization }} {{ getAsterisk(prop, data) }}</label
>
<label [htmlFor]="prop.id">{{ prop.displayName | abpLocalization }} {{ asterisk }}</label>
<textarea
[id]="prop.id"
[formControlName]="prop.name"
[abpDisabled]="prop.disabled(data)"
[readonly]="prop.readonly(data)"
[abpDisabled]="disabled"
[readonly]="readonly"
class="form-control"
></textarea>
</ng-template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { TrackByService } from '@abp/ng.core';
import { ABP, TrackByService } from '@abp/ng.core';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Input,
Optional,
SimpleChanges,
SkipSelf,
OnChanges,
} from '@angular/core';
import { ControlContainer, Validators } from '@angular/forms';
import { ControlContainer, Validators, ValidatorFn } from '@angular/forms';
import { NgbDateAdapter, NgbTimeAdapter } from '@ng-bootstrap/ng-bootstrap';
import { Observable, of } from 'rxjs';
import { DateAdapter } from '../../adapters/date.adapter';
import { TimeAdapter } from '../../adapters/time.adapter';
import { ePropType } from '../../enums/props.enum';
Expand All @@ -30,15 +33,23 @@ import { selfFactory } from '../../utils/factory.util';
{ provide: NgbTimeAdapter, useClass: TimeAdapter },
],
})
export class ExtensibleFormPropComponent {
export class ExtensibleFormPropComponent implements OnChanges {
@Input() data: PropData;

@Input() prop: FormProp;

options$: Observable<ABP.Option<any>[]> = of([]);

validators: ValidatorFn[] = [];

readonly: boolean;

disabled: boolean;

constructor(public readonly cdRef: ChangeDetectorRef, public readonly track: TrackByService) {}

getAsterisk(prop: FormProp, data: PropData): string {
return prop.validators(data).some(validator => validator === Validators.required) ? '*' : '';
get asterisk(): string {
return this.validators.some(validator => validator === Validators.required) ? '*' : '';
}

getComponent(prop: FormProp): string {
Expand Down Expand Up @@ -77,4 +88,16 @@ export class ExtensibleFormPropComponent {
return 'hidden';
}
}

ngOnChanges({ prop }: SimpleChanges) {
const options = prop.currentValue.options;
const readonly = prop.currentValue.readonly;
const disabled = prop.currentValue.disabled;
const validators = prop.currentValue.validators;

if (options) this.options$ = options(this.data);
if (readonly) this.readonly = readonly(this.data);
if (disabled) this.disabled = disabled(this.data);
if (validators) this.validators = validators(this.data);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { TrackByService } from '@abp/ng.core';
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Inject,
Input,
OnDestroy,
Optional,
QueryList,
SkipSelf,
ViewChildren,
} from '@angular/core';
import { ControlContainer, FormGroup } from '@angular/forms';
import { Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { EXTRA_PROPERTIES_KEY } from '../../constants/extra-properties';
import { FormPropList } from '../../models/form-props';
import { ExtensionsService } from '../../services/extensions.service';
Expand All @@ -35,7 +31,7 @@ import { ExtensibleFormPropComponent } from './extensible-form-prop.component';
},
],
})
export class ExtensibleFormComponent<R = any> implements AfterViewInit, OnDestroy {
export class ExtensibleFormComponent<R = any> {
@ViewChildren(ExtensibleFormPropComponent)
formProps: QueryList<ExtensibleFormPropComponent>;

Expand All @@ -46,7 +42,6 @@ export class ExtensibleFormComponent<R = any> implements AfterViewInit, OnDestro
this.record = record;
}

private subscription = new Subscription();
extraPropertiesKey = EXTRA_PROPERTIES_KEY;
propList: FormPropList<R>;
record: R;
Expand All @@ -66,17 +61,4 @@ export class ExtensibleFormComponent<R = any> implements AfterViewInit, OnDestro
private extensions: ExtensionsService,
@Inject(EXTENSIONS_IDENTIFIER) private identifier: string,
) {}

ngAfterViewInit() {
this.subscription.add(
this.form.statusChanges.pipe(debounceTime(0)).subscribe(() => {
this.formProps.forEach(prop => prop.cdRef.markForCheck());
this.cdRef.detectChanges();
}),
);
}

ngOnDestroy() {
this.subscription.unsubscribe();
}
}

0 comments on commit e1d20c7

Please sign in to comment.