Skip to content

Commit b609d5f

Browse files
committed
refactor: refactoring builders
1 parent d538d62 commit b609d5f

5 files changed

Lines changed: 51 additions & 32 deletions

File tree

source/js/conditions/conditionBuilder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import NullCondition from "./condition/nullCondition";
33
import OrCondition from "./condition/orCondition";
44

55
class ConditionBuilder implements ConditionBuilderInterface {
6+
constructor(private fields: FieldsObject) {
7+
8+
}
9+
610
public build(conditions: any): ConditionInterface[] {
711
if (!Array.isArray(conditions) || conditions.length === 0) {
812
return [new NullCondition()];
@@ -15,8 +19,13 @@ class ConditionBuilder implements ConditionBuilderInterface {
1519
}
1620

1721
if (conditionSet.length === 1) {
22+
conditionSet[0].class = this.fields[conditionSet[0].field] ?? null;
1823
conditionsList.push(new OrCondition(conditionSet[0]));
1924
} else {
25+
conditionSet.forEach((condition: Condition) => {
26+
condition.class = this.fields[condition.field] ?? null;
27+
});
28+
2029
conditionsList.push(new AndCondition(conditionSet));
2130
}
2231
});

source/js/fields/fieldBuilder.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import Checkbox from "./field/checkbox";
2-
import NullField from "./field/nullField";
1+
import Checkbox from "./field/checkbox/checkbox";
2+
import CheckboxConditionsHandler from "./field/checkbox/checkboxConditionsHandler";
3+
import NullFieldConditionsHandler from "./field/nullField/nullFieldConditionsHandler";
4+
import NullField from "./field/nullField/nullField";
5+
import CheckboxValidator from "../conditions/validate/checkboxValidator";
36

47
class FieldBuilder implements FieldBuilderInterface {
58
private name: string = 'data-js-field-name';
69
private condition: string = 'data-js-conditional-logic';
710

8-
constructor(private conditionBuilder: ConditionBuilderInterface) {
9-
}
10-
1111
public build(field: HTMLElement, type: string): FieldInterface {
1212
if (!this.validateRequiredAttributes(field)) {
1313
console.error('Field name and conditional is required');
@@ -27,36 +27,38 @@ class FieldBuilder implements FieldBuilderInterface {
2727
field,
2828
type,
2929
this.getFieldName(field),
30-
this.getFieldCondition(field)
30+
new NullFieldConditionsHandler(field, this.getFieldCondition(field))
3131
);
3232
}
3333

3434
public buildCheckbox(field: HTMLElement): FieldInterface {
35-
const choices = field.querySelectorAll('input[type="checkbox"]');
36-
35+
const choices = field.querySelectorAll('input[type="checkbox"]') as NodeListOf<HTMLInputElement>;
3736

3837
return new Checkbox(
3938
field,
40-
choices as NodeListOf<HTMLInputElement>,
39+
choices,
4140
this.getFieldName(field),
42-
this.getFieldCondition(field)
41+
new CheckboxValidator(),
42+
new CheckboxConditionsHandler(this.getFieldCondition(field))
4343
);
4444
}
4545

4646
private getFieldName(field: HTMLElement): string {
4747
return field.getAttribute('data-js-field-name') as string;
4848
}
4949

50-
private getFieldCondition(field: HTMLElement): ConditionInterface[] {
50+
private getFieldCondition(field: HTMLElement): any {
5151
let condition = 0;
5252

53+
5354
try {
5455
condition = JSON.parse(field.getAttribute('data-js-conditional-logic') as string);
5556
} catch (error) {
5657
condition = 0;
5758
}
5859

59-
return this.conditionBuilder.build(condition);
60+
// return this.conditionBuilder.build(condition);
61+
return condition;
6062
}
6163

6264
private validateRequiredAttributes(field: HTMLElement): boolean {

source/js/fields/fields.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
class Fields implements FieldsInterface {
2-
private fields: FieldsObject = {};
3-
4-
constructor(private form: HTMLFormElement, private fieldsArray: FieldInterface[]) {
2+
constructor(private form: HTMLFormElement, private fields: FieldsObject) {
53
}
64

75
public init(): void {
8-
this.fieldsArray.forEach((field) => {
9-
this.fields[field.getName()] = field;
10-
});
11-
126
this.setupConditionals();
137
}
148

159
private setupConditionals(): void {
1610
for (const fieldName in this.fields) {
17-
this.fields[fieldName].getConditions().forEach((condition) => {
11+
this.fields[fieldName].getConditionsHandler().getConditions().forEach((condition) => {
1812
condition.getConditionFieldNames().forEach((conditionFieldName) => {
1913
if (this.fields[conditionFieldName]) {
20-
this.fields[conditionFieldName].addValueChangeListener(this.fields[fieldName]);
14+
this.fields[conditionFieldName].getConditionsHandler().addValueChangeListener(this.fields[fieldName]);
2115
}
2216
});
2317
});

source/js/fields/fieldsInterface.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@ interface FieldsInterface {
33
}
44

55
interface FieldInterface {
6+
init(conditionBuilder: ConditionBuilderInterface): void;
67
getName(): string;
7-
getConditions(): ConditionInterface[];
8-
addValueChangeListener(field: FieldInterface): void;
9-
validateConditionals(): boolean;
8+
getConditionsHandler(): ConditionsHandlerInterface;
9+
}
10+
11+
interface CheckboxInterface extends FieldInterface {
12+
getChoices(): NodeListOf<HTMLInputElement>;
1013
}
1114

1215
interface FieldBuilderInterface {
1316
build(field: HTMLElement, type: string): FieldInterface;
1417
buildCheckbox(field: HTMLElement): FieldInterface;
1518
}
1619

20+
interface ConditionsHandlerInterface {
21+
init(parent: FieldInterface, conditionsBuilder: ConditionBuilderInterface): void;
22+
getConditions(): ConditionInterface[];
23+
validate(): boolean;
24+
addValueChangeListener(field: FieldInterface): void;
25+
}
26+
1727
type FieldsObject = {
1828
[key: string]: FieldInterface;
1929
}

source/js/init.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ class Form {
2525
}
2626

2727
private setupConditionalLogic() {
28-
const builder = new FieldBuilder(
29-
new ConditionBuilder()
30-
);
28+
const builder = new FieldBuilder();
29+
let fieldsObject: FieldsObject = {};
3130

32-
let fields: FieldInterface[] = [];
33-
34-
this.form.querySelectorAll('[data-js-field]').forEach(field => {
35-
fields.push(builder.build(field as HTMLElement, field.getAttribute('data-js-field') ?? ''));
31+
this.form.querySelectorAll('[data-js-field]').forEach(element => {
32+
const field = builder.build(element as HTMLElement, element.getAttribute('data-js-field') ?? '');
33+
fieldsObject[field.getName()] = field;
3634
});
3735

38-
new Fields(this.form, fields).init();
36+
const conditionBuilder = new ConditionBuilder(fieldsObject);
37+
38+
for (const fieldName in fieldsObject) {
39+
fieldsObject[fieldName].init(conditionBuilder);
40+
}
41+
42+
new Fields(this.form, fieldsObject).init();
3943
}
4044

4145
private setupSteps() {

0 commit comments

Comments
 (0)