1+ class RadioConditionsHandler implements ConditionsHandlerInterface {
2+ private fieldsObject : FieldsObject = { } ;
3+ private parent : RadioInterface | null = null ;
4+ private conditions : ConditionInterface [ ] = [ ] ;
5+ private isDisabled : boolean = false ;
6+
7+ constructor ( private unstructuredConditions : any ) {
8+ }
9+
10+ public init ( parent : RadioInterface , conditionsBuilder : ConditionBuilderInterface ) : void {
11+ this . parent = parent ;
12+ console . log ( this . unstructuredConditions ) ;
13+ this . conditions = conditionsBuilder . build ( this . unstructuredConditions ) ;
14+ this . setValueChangeListener ( ) ;
15+ }
16+
17+ private updateDisabled ( disabled : boolean ) : void {
18+ if ( this . isDisabled !== disabled ) {
19+ this . isDisabled = disabled ;
20+
21+ this . parent ?. getChoices ( ) . forEach ( ( checkbox , index ) => {
22+ if ( index === 0 ) {
23+ this . parent ?. getField ( ) . classList . toggle ( 'u-display--none' , disabled )
24+ }
25+
26+ checkbox . disabled = disabled ;
27+ } ) ;
28+
29+ this . dispatchUpdateEvent ( ) ;
30+ }
31+ }
32+
33+ public validate ( ) : void {
34+ let isValid : boolean = false ;
35+ for ( const condition of this . getConditions ( ) ) {
36+ if ( condition . validate ( ) ) {
37+ isValid = true ;
38+ break ;
39+ }
40+ }
41+
42+ this . updateDisabled ( ! isValid ) ;
43+ }
44+
45+ public dispatchUpdateEvent ( ) : void {
46+ const choice = this . parent ?. getChoices ( ) [ 0 ] ;
47+
48+ if ( choice ) {
49+ choice . dispatchEvent ( new Event ( 'change' ) ) ;
50+ }
51+ }
52+
53+ public getIsDisabled ( ) : boolean {
54+ return this . isDisabled ;
55+ }
56+
57+ public getConditions ( ) : ConditionInterface [ ] {
58+ return this . conditions ;
59+ }
60+
61+ public addValueChangeListener ( field : FieldInterface ) : void {
62+ this . fieldsObject [ field . getName ( ) ] = field ;
63+ }
64+
65+ private setValueChangeListener ( ) : void {
66+ this . parent ?. getChoices ( ) . forEach ( ( radio ) => {
67+ radio . addEventListener ( 'change' , ( ) => {
68+ for ( const fieldName in this . fieldsObject ) {
69+ this . fieldsObject [ fieldName ] . getConditionsHandler ( ) . validate ( ) ;
70+ }
71+ } ) ;
72+ } ) ;
73+ }
74+ }
75+
76+ export default RadioConditionsHandler ;
0 commit comments