@@ -14,9 +14,9 @@ export default () => ({
1414 let condition = this . processCondition ( [ "Year" , "0" , [ 0 ] ] , add_to_id ) ;
1515 this . conditionMap [ condition . id ] = condition ;
1616
17- let parentGroup = this . conditionMap [ add_to_id ] ;
17+ if ( add_to_id ) {
18+ let parentGroup = this . conditionMap [ add_to_id ] ;
1819
19- if ( parentGroup ) {
2020 if ( parentGroup . children . length ) {
2121 parentGroup . children [ parentGroup . children . length - 1 ] . operator = "&&"
2222 }
@@ -30,13 +30,31 @@ export default () => ({
3030 }
3131 } ,
3232
33- addGroup ( add_to_id ) {
33+ deleteElement ( element_id ) {
34+ let element = this . conditionMap [ element_id ] ;
35+ let siblings = this . conditionMap [ element . parent_id ] ?. children
36+ ?? this . sortable_data ;
37+
38+ let element_index = siblings . indexOf ( element ) ;
39+
40+ if ( siblings . length > 1 && element_index === siblings . length - 1 ) {
41+ siblings [ element_index - 1 ] . operator = false
42+ }
43+
44+ siblings . splice ( element_index , 1 ) ;
45+
46+ if ( element . parent_id && siblings . length == 0 ) {
47+ this . deleteElement ( element . parent_id ) ;
48+ }
49+ } ,
50+
51+ addGroup ( add_to_id = null ) {
3452 let group = this . processGroup ( [ "" , [ ] ] , add_to_id , 0 ) ;
3553 this . conditionMap [ group . id ] = group ;
3654
37- let parentGroup = this . conditionMap [ add_to_id ] ;
55+ if ( add_to_id ) {
56+ let parentGroup = this . conditionMap [ add_to_id ] ;
3857
39- if ( parentGroup ) {
4058 if ( parentGroup . children . length ) {
4159 parentGroup . children [ parentGroup . children . length - 1 ] . operator = "&&"
4260 }
@@ -48,6 +66,8 @@ export default () => ({
4866
4967 this . sortable_data . push ( group ) ;
5068 }
69+
70+ this . addCondition ( group . id ) ;
5171 } ,
5272
5373 set sortableData ( value ) {
@@ -544,7 +564,7 @@ export default () => ({
544564 ${ condition_types }
545565 </select>
546566 ${ this . renderConditionOptions ( condition ) }
547- <div @click="delete( ${ condition . id } )" class="cursor-pointer order-last ml-2 mr-1.5"><i class="fa fa-trash"></i></div>
567+ <div @click="deleteElement(' ${ condition . id } ' )" class="cursor-pointer order-last ml-2 mr-1.5"><i class="fa fa-trash"></i></div>
548568 </div>
549569 ${ condition . operator && ( ! parent || parent . type !== "num" ) ? this . renderOperator ( condition ) : "" }
550570 </li>` ;
0 commit comments