Skip to content

Commit 30dae40

Browse files
committed
refactor: working conditions
1 parent 9a4f901 commit 30dae40

6 files changed

Lines changed: 68 additions & 9 deletions

File tree

source/js/fields/field/checkbox/checkbox.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class Checkbox implements CheckboxInterface {
66
private checkboxValidator: ConditionValidatorInterface,
77
private conditionsHandler: ConditionsHandlerInterface
88
) {
9-
109
}
1110

1211
public init(conditionBuilder: ConditionBuilderInterface): void {
1312
this.conditionsHandler.init(this, conditionBuilder);
13+
this.checkboxValidator.init(this);
1414
}
1515

1616
public getName(): string {
@@ -29,6 +29,10 @@ class Checkbox implements CheckboxInterface {
2929
return this.choices;
3030
}
3131

32+
public getSelectedChoices(): string[] {
33+
return [...this.getChoices()].filter(choice => choice.checked).map(choice => choice.value);
34+
}
35+
3236
public getField(): HTMLElement {
3337
return this.field;
3438
}

source/js/fields/field/checkbox/checkboxConditionValidator.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
class CheckboxConditionValidator implements ConditionValidatorInterface {
2+
private parent: CheckboxInterface|null = null;
3+
public init(parent: CheckboxInterface) {
4+
this.parent = parent;
5+
}
6+
27
public validate(condition: Condition): boolean {
3-
4-
return false;
8+
const selected = this.parent?.getSelectedChoices() ?? [];
9+
10+
switch (condition.operator) {
11+
case '==':
12+
case '=':
13+
case '===':
14+
return selected.includes(condition.value);
15+
case '!=':
16+
case '!==':
17+
return !selected.includes(condition.value);
18+
case '==empty':
19+
return selected.length === 0;
20+
case '!=empty':
21+
return selected.length > 0;
22+
case '>':
23+
return selected.some(selectedValue => Number(selectedValue) > Number(condition.value));
24+
case '<':
25+
return selected.some(selectedValue => Number(selectedValue) < Number(condition.value));
26+
default:
27+
return false;
28+
}
529
}
630
}
731

source/js/fields/field/checkbox/checkboxConditionsHandler.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { TilesHelper } from "../../../../../dist/js-init.0f85b13b34f29a8d87e4";
2+
13
class CheckboxConditionsHandler implements ConditionsHandlerInterface {
24
private fieldsObject: FieldsObject = {};
35
private parent: CheckboxInterface | null = null;
46
private conditions: ConditionInterface[] = [];
7+
private isDisabled: boolean = false;
58

69
constructor(private unstructuredConditions: any) {
710
}
@@ -12,12 +15,32 @@ class CheckboxConditionsHandler implements ConditionsHandlerInterface {
1215
this.setValueChangeListener();
1316
}
1417

15-
public validate(): boolean {
16-
this.getConditions().forEach((condition) => {
17-
condition.validate();
18-
})
18+
private updateDisabled(disabled: boolean): void {
19+
if (this.isDisabled !== disabled) {
20+
this.isDisabled = disabled;
21+
22+
this.parent?.getChoices().forEach((checkbox, index) => {
23+
if (index === 0) {
24+
checkbox.dispatchEvent(new Event('change'));
25+
this.parent?.getField().classList.toggle('u-display--none', disabled)
26+
}
27+
28+
checkbox.disabled = disabled;
29+
});
30+
}
31+
}
32+
33+
public validate(): void {
34+
console.log(this.parent?.getName())
35+
let isValid: boolean = false;
36+
for (const condition of this.getConditions()) {
37+
if (condition.validate()) {
38+
isValid = true;
39+
break;
40+
}
41+
}
1942

20-
return false;
43+
this.updateDisabled(!isValid);
2144
}
2245

2346
public getConditions(): ConditionInterface[] {

source/js/fields/field/nullField/nullField.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class NullField implements FieldInterface {
1212

1313
public init(conditionBuilder: ConditionBuilderInterface): void {
1414
this.conditionsHandler.init(this, conditionBuilder);
15+
this.nullFieldConditionValidator.init(this);
1516
}
1617

1718
public getField(): HTMLElement {

source/js/fields/field/nullField/nullFieldConditionValidator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class NullFieldConditionValidator implements ConditionValidatorInterface {
2+
public init(parent: FieldInterface): void {
3+
// No implementation needed for NullField
4+
}
5+
26
public validate(condition: Condition): boolean {
37

48
return false;

source/js/fields/fieldsInterface.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ interface FieldsInterface {
55
interface FieldInterface {
66
init(conditionBuilder: ConditionBuilderInterface): void;
77
getName(): string;
8+
getField(): HTMLElement;
89
getConditionsHandler(): ConditionsHandlerInterface;
910
getConditionValidator(): ConditionValidatorInterface;
1011
}
1112

1213
interface CheckboxInterface extends FieldInterface {
1314
getChoices(): NodeListOf<HTMLInputElement>;
15+
getSelectedChoices(): string[];
1416
}
1517

1618
interface FieldBuilderInterface {
@@ -21,11 +23,12 @@ interface FieldBuilderInterface {
2123
interface ConditionsHandlerInterface {
2224
init(parent: FieldInterface, conditionsBuilder: ConditionBuilderInterface): void;
2325
getConditions(): ConditionInterface[];
24-
validate(): boolean;
26+
validate(): void;
2527
addValueChangeListener(field: FieldInterface): void;
2628
}
2729

2830
interface ConditionValidatorInterface {
31+
init(parent: FieldInterface): void;
2932
validate(condition: Condition): boolean;
3033
}
3134

0 commit comments

Comments
 (0)