Skip to content

Added autopopulate function to handle autopopulated inputs #114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.0.0-carbonize
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { HiderHelper } from '../form-entry/control-hiders-disablers/hider-helper
import { AlertHelper } from '../form-entry/control-alerts/alert-helpers';
import { DisablerHelper } from '../form-entry/control-hiders-disablers/disabler-helper';
import { CanCalculate } from '../form-entry/control-calculators/can-calculate';
import { CanAutopopulate } from '../form-entry/can-autopopulate/can-autopopulate';
import { ExpressionRunner } from '../form-entry/expression-runner/expression-runner';

class AfeFormControl
Expand All @@ -31,6 +32,7 @@ class AfeFormControl
CanHide,
CanDisable,
CanCalculate,
CanAutopopulate,
CanGenerateAlert,
ValueChangeListener {
private _controlRelations: ControlRelations;
Expand All @@ -44,6 +46,7 @@ class AfeFormControl
alert: string;
alerts: Alert[];
calculator: Function;
autopopulate: Function;
disablers: Disabler[];

private hiderHelper: HiderHelper = new HiderHelper();
Expand Down Expand Up @@ -100,6 +103,17 @@ class AfeFormControl
}
}

setAutopopulateFn(newAutopopulate: Function) {
this.autopopulate = newAutopopulate;
}

updateAutopopulatedValue() {
if (this.autopopulate) {
const _val = this.autopopulate.call(ExpressionRunner, {});
this.setValue(_val);
}
}

clearHidingFns() {
this.hiderHelper.clearHidersForControl(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export class ControlRelation {
(this._control as any).updateCalculatedValue();
}

if ((this._control as any).updateAutopopulatedValue) {
(this._control as any).updateAutopopulatedValue();
}

this._control.updateValueAndValidity();
if ((this._control as any).updateHiddenState) {
(this._control as any).updateHiddenState();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface CanAutopopulate {
autopopulate: Function;
setAutopopulateFn(newAutopopulate: Function);
updateAutopopulatedValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export class ExpressionRunner {
expression: string,
control: AfeFormArray | AfeFormGroup | AfeFormControl,
helper: any,
autopopulate: any,
dataDependencies: any,
form?: Form
): Runnable {
Expand All @@ -35,6 +36,7 @@ export class ExpressionRunner {
runner.setControlQuestion(control, form, scope);
runner.getControlRelationValueString(control, scope);
runner.getHelperMethods(helper, scope);
runner.getAutopopulateMethods(autopopulate, scope);
runner.getDataDependencies(dataDependencies, scope);
if (form) {
// console.error('Form defined', form);
Expand Down Expand Up @@ -161,7 +163,7 @@ export class ExpressionRunner {
) {
control.controlRelations.relations.forEach((relation) => {
const related = relation.relatedTo as any;
const question = form.searchNodeByQuestionId(related.uuid)[0]?.question
const question = form?.searchNodeByQuestionId(related.uuid)[0]?.question
?.extras;
scope['FORM'][related.uuid] = question;
});
Expand Down Expand Up @@ -217,6 +219,14 @@ export class ExpressionRunner {
}
}

private getAutopopulateMethods(obj: any, scope?: any) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
scope[key] = obj[key];
}
}
}

private getDataDependencies(obj: any, scope?: any) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
Expand Down
2 changes: 2 additions & 0 deletions projects/ngx-formentry/src/form-entry/form-entry.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { HidersDisablersFactory } from './form-factory/hiders-disablers.factory'
import { AlertsFactory } from './form-factory/show-messages.factory';
import { ExpressionRunner } from './expression-runner/expression-runner';
import { JsExpressionHelper } from './helpers/js-expression-helper';
import { JsExpressionAutopopulate } from './helpers/autopopulate-expression-helper';
import { FormSchemaCompiler } from './services/form-schema-compiler.service';
import { FormFactory } from './form-factory/form.factory';
import { QuestionFactory } from './form-factory/question.factory';
Expand Down Expand Up @@ -83,6 +84,7 @@ import { CustomComponentWrapperModule } from '../components/custom-component-wra
AlertsFactory,
ExpressionRunner,
JsExpressionHelper,
JsExpressionAutopopulate,
HistoricalFieldHelperService,
FormSchemaCompiler,
FormFactory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export class ControlRelationsFactory {
| AfeFormArray).addValueChangeListener((value) => {
if ((rootControl as any).updateCalculatedValue) {
(rootControl as any).updateCalculatedValue();
} else if ((rootControl as any).updateAutopopulatedValue) {
(rootControl as any).updateAutopopulatedValue();
}

rootControl.updateValueAndValidity();
Expand Down Expand Up @@ -292,6 +294,13 @@ export class ControlRelationsFactory {
questionBase.calculateExpression.indexOf(id) !== -1
) {
hasRelation = true;
} else if (
!hasRelation &&
questionBase.autopopulateExpression &&
questionBase.autopopulateExpression.length > 0 &&
questionBase.autopopulateExpression.indexOf(id) !== -1
) {
hasRelation = true;
}

return hasRelation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
Runnable
} from '../expression-runner/expression-runner';
import { JsExpressionHelper } from '../helpers/js-expression-helper';

import { JsExpressionAutopopulate } from '../helpers/autopopulate-expression-helper';
@Injectable()
export class FormControlService {
controls = [];
Expand Down Expand Up @@ -145,6 +145,12 @@ export class FormControlService {
form,
form.dataSourcesContainer.dataSources
);
this.wireAutopopulator(
question,
control,
form,
form.dataSourcesContainer.dataSources
);

if (parentControl instanceof AfeFormGroup) {
parentControl.setControl(question.key, control);
Expand Down Expand Up @@ -211,4 +217,27 @@ export class FormControlService {
control.setCalculatorFn(runnable.run);
}
}
// autopopulate values based on previous inputs
private wireAutopopulator(
question: QuestionBase,
control: AfeFormControl,
form: Form,
dataSource?: any
) {
if (
question.autopopulateExpression &&
question.autopopulateExpression !== ''
) {
const autopopulate: JsExpressionAutopopulate = new JsExpressionAutopopulate();
const runner: ExpressionRunner = new ExpressionRunner();
const runnable: Runnable = runner.getRunnable(
question.autopopulateExpression,
control,
autopopulate.autopopulateFunctions,
dataSource,
form
);
control.setAutopopulateFn(runnable.run);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../../abstract-controls-extension';
import { QuestionBase } from '../question-models/question-base';
import { JsExpressionHelper } from '../helpers/js-expression-helper';
import { JsExpressionAutopopulate } from '../helpers/autopopulate-expression-helper';
import { Form } from './form';
// Add ability to display all fields for debugging
import { DebugModeService } from './../services/debug-mode.service';
Expand All @@ -23,6 +24,7 @@ export class HidersDisablersFactory {
constructor(
private expressionRunner: ExpressionRunner,
private expressionHelper: JsExpressionHelper,
private expressionAutopopulate: JsExpressionAutopopulate,
private _debugModeService: DebugModeService
) {}

Expand All @@ -35,6 +37,7 @@ export class HidersDisablersFactory {
question.disable as string,
control,
this.expressionHelper.helperFunctions,
this.expressionAutopopulate.autopopulateFunctions,
{},
form
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -108,6 +109,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -137,6 +139,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -165,6 +168,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -194,6 +198,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -224,6 +229,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -260,6 +266,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -292,6 +299,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -320,6 +328,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand All @@ -344,6 +353,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand All @@ -367,6 +377,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand All @@ -390,6 +401,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -417,6 +429,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -453,6 +466,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand All @@ -478,6 +492,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -553,6 +568,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -583,6 +599,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -621,6 +638,7 @@ export class QuestionFactory {
this.addAlertProperty(schemaQuestion, question);
this.addHistoricalExpressions(schemaQuestion, question);
this.addCalculatorProperty(schemaQuestion, question);
this.addAutopoulateProperty(schemaQuestion, question);
return question;
}

Expand Down Expand Up @@ -964,6 +982,16 @@ export class QuestionFactory {
}
}

addAutopoulateProperty(schemaQuestion: any, question: QuestionBase): any {
if (
schemaQuestion.questionOptions &&
typeof schemaQuestion.questionOptions.autopopulate === 'object'
) {
question.autopopulateExpression =
schemaQuestion.questionOptions.autopopulate.autopopulateExpression;
}
}

addAlertProperty(schemaQuestion: any, question: QuestionBase): any {
if (schemaQuestion.alert) {
question.alert = schemaQuestion.alert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import {
import { QuestionBase } from '../question-models/question-base';
import { JsExpressionHelper } from '../helpers/js-expression-helper';
import { Form } from './form';
import { JsExpressionAutopopulate } from '../helpers/autopopulate-expression-helper';

@Injectable()
export class AlertsFactory {
constructor(
private expressionRunner: ExpressionRunner,
private expressionHelper: JsExpressionHelper
private expressionHelper: JsExpressionHelper,
private expressionAutopopulate: JsExpressionAutopopulate,
) {}
getJsExpressionshowAlert(
question: QuestionBase,
Expand All @@ -30,6 +32,7 @@ export class AlertsFactory {
question.alert.alertWhenExpression,
control,
this.expressionHelper.helperFunctions,
this.expressionAutopopulate.autopopulateFunctions,
{},
form
);
Expand Down
Loading