@@ -52,10 +52,10 @@ export class DropGroupDirective implements OnInit {
52
52
} else {
53
53
switch ( event . container . id ) {
54
54
case 'Exclusion' :
55
- this . moveCriterionInExclusion ( event . previousIndex , event . currentIndex ) ;
55
+ this . moveCriterionInExclusion ( droppedCriterion , event . previousIndex , event . currentIndex ) ;
56
56
break ;
57
57
case 'Inclusion' :
58
- this . moveCriterionInInclusion ( event . previousIndex , event . currentIndex ) ;
58
+ this . moveCriterionInInclusion ( droppedCriterion , event . previousIndex , event . currentIndex ) ;
59
59
break ;
60
60
default :
61
61
break ;
@@ -70,12 +70,12 @@ export class DropGroupDirective implements OnInit {
70
70
}
71
71
private addToInclusion ( droppedCriterion : string , currentIndex : number ) : void {
72
72
this . criteria = this . feasibilityQuery . getInclusionCriteria ( ) ;
73
- this . criteria . splice ( currentIndex , 0 , [ droppedCriterion ] ) ;
73
+ this . addCriterionToInnerArray ( this . criteria , droppedCriterion , currentIndex ) ;
74
74
this . queryProviderService . setInclusionCriteria ( this . criteria ) ;
75
75
}
76
76
private addToExclusion ( droppedCriterion : string , currentIndex : number ) : void {
77
77
this . criteria = this . feasibilityQuery . getExclusionCriteria ( ) ;
78
- this . criteria . splice ( currentIndex , 0 , [ droppedCriterion ] ) ;
78
+ this . addCriterionToInnerArray ( this . criteria , droppedCriterion , currentIndex ) ;
79
79
this . queryProviderService . setExclusionCriteria ( this . criteria ) ;
80
80
}
81
81
@@ -95,23 +95,99 @@ export class DropGroupDirective implements OnInit {
95
95
}
96
96
97
97
private deleteCriterion ( inexclusion : string [ ] [ ] , criterionID : string ) : string [ ] [ ] {
98
- inexclusion . forEach ( ( idArray ) => {
98
+ inexclusion . every ( ( idArray ) => {
99
99
const index = idArray . indexOf ( criterionID ) ;
100
100
if ( index > - 1 ) {
101
101
idArray . splice ( index , 1 ) ;
102
+ return false ;
102
103
}
104
+ return true ;
103
105
} ) ;
104
106
inexclusion = inexclusion . filter ( ( item ) => item . length > 0 ) ;
105
107
return inexclusion ;
106
108
}
107
- private moveCriterionInInclusion ( previousIndex : number , currentIndex : number ) : void {
109
+ private moveCriterionInInclusion (
110
+ criterionID : string ,
111
+ previousIndex : number ,
112
+ currentIndex : number
113
+ ) : void {
108
114
this . criteria = this . feasibilityQuery . getInclusionCriteria ( ) ;
109
- moveItemInArray ( this . criteria , previousIndex , currentIndex ) ;
115
+ this . moveCriterion ( criterionID , previousIndex , currentIndex ) ;
110
116
this . queryProviderService . setInclusionCriteria ( this . criteria ) ;
111
117
}
112
- private moveCriterionInExclusion ( previousIndex : number , currentIndex : number ) : void {
118
+ private moveCriterionInExclusion (
119
+ criterionID : string ,
120
+ previousIndex : number ,
121
+ currentIndex : number
122
+ ) : void {
113
123
this . criteria = this . feasibilityQuery . getExclusionCriteria ( ) ;
114
- moveItemInArray ( this . criteria , previousIndex , currentIndex ) ;
124
+ this . moveCriterion ( criterionID , previousIndex , currentIndex ) ;
115
125
this . queryProviderService . setExclusionCriteria ( this . criteria ) ;
116
126
}
127
+
128
+ private moveCriterion ( criterionID : string , previousIndex : number , currentIndex : number ) : void {
129
+ const positionPrev = this . getPosition ( this . criteria , previousIndex ) ;
130
+ const positionCurr = this . getPosition ( this . criteria , currentIndex ) ;
131
+ let position = positionCurr ;
132
+ if ( previousIndex < currentIndex ) {
133
+ if ( positionPrev [ 0 ] < positionCurr [ 0 ] ) {
134
+ position = [ positionCurr [ 0 ] + 1 , positionCurr [ 1 ] ] ;
135
+ } else {
136
+ position = [ positionCurr [ 0 ] , positionCurr [ 1 ] + 1 ] ;
137
+ }
138
+ this . addCriterionToPosition ( this . criteria , criterionID , position ) ;
139
+ this . criteria = this . deleteCriterion ( this . criteria , criterionID ) ;
140
+ } else {
141
+ const addToInnerArray = positionPrev [ 1 ] > 0 || positionCurr [ 1 ] > 0 ;
142
+ this . criteria = this . deleteCriterion ( this . criteria , criterionID ) ;
143
+ this . addCriterionToPosition ( this . criteria , criterionID , positionCurr , addToInnerArray ) ;
144
+ }
145
+ }
146
+ private addCriterionToPosition (
147
+ criteria : string [ ] [ ] ,
148
+ criterionID : string ,
149
+ position : [ number , number ] ,
150
+ addToInnerArray ?: boolean
151
+ ) : void {
152
+ if ( position [ 0 ] >= criteria . length ) {
153
+ this . criteria . push ( [ criterionID ] ) ;
154
+ } else {
155
+ if ( criteria [ position [ 0 ] ] ?. length > 1 || addToInnerArray ) {
156
+ this . criteria [ position [ 0 ] ] . splice ( position [ 1 ] , 0 , criterionID ) ;
157
+ } else {
158
+ this . criteria . splice ( position [ 0 ] , 0 , [ criterionID ] ) ;
159
+ }
160
+ }
161
+ }
162
+
163
+ private addCriterionToInnerArray (
164
+ criteria : string [ ] [ ] ,
165
+ criterionID : string ,
166
+ currentIndex : number
167
+ ) : void {
168
+ const position = this . getPosition ( criteria , currentIndex ) ;
169
+ if ( currentIndex >= criteria . length ) {
170
+ this . criteria . push ( [ criterionID ] ) ;
171
+ } else {
172
+ if ( criteria [ position [ 0 ] ] ?. length > 1 ) {
173
+ this . criteria [ 0 ] . splice ( position [ 1 ] , 0 , criterionID ) ;
174
+ } else {
175
+ this . criteria . splice ( position [ 0 ] , 0 , [ criterionID ] ) ;
176
+ }
177
+ }
178
+ }
179
+
180
+ private getPosition ( criteria : string [ ] [ ] , currentIndex : number ) : [ number , number ] {
181
+ let position : [ number , number ] = [ 0 , 0 ] ;
182
+ let count = 0 ;
183
+ criteria . forEach ( ( outer , outerIndex ) => {
184
+ outer . forEach ( ( inner , innerIndex ) => {
185
+ if ( count === currentIndex ) {
186
+ position = [ outerIndex , innerIndex ] ;
187
+ }
188
+ count ++ ;
189
+ } ) ;
190
+ } ) ;
191
+ return position ;
192
+ }
117
193
}
0 commit comments