Skip to content

Commit 2ad3d7c

Browse files
committed
fix drag&drop of moved criteria
1 parent ca78418 commit 2ad3d7c

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

src/app/shared/directives/drop-group/drop-group.directive.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ export class DropGroupDirective implements OnInit {
9595
}
9696

9797
private deleteCriterion(inexclusion: string[][], criterionID: string): string[][] {
98-
inexclusion.forEach((idArray) => {
98+
inexclusion.every((idArray) => {
9999
const index = idArray.indexOf(criterionID);
100100
if (index > -1) {
101101
idArray.splice(index, 1);
102+
return false;
102103
}
104+
return true;
103105
});
104106
inexclusion = inexclusion.filter((item) => item.length > 0);
105107
return inexclusion;
@@ -110,8 +112,7 @@ export class DropGroupDirective implements OnInit {
110112
currentIndex: number
111113
): void {
112114
this.criteria = this.feasibilityQuery.getInclusionCriteria();
113-
this.criteria = this.deleteCriterion(this.criteria, criterionID);
114-
this.addCriterionToInnerArray(this.criteria, criterionID, currentIndex);
115+
this.moveCriterion(criterionID, previousIndex, currentIndex);
115116
this.queryProviderService.setInclusionCriteria(this.criteria);
116117
}
117118
private moveCriterionInExclusion(
@@ -120,11 +121,45 @@ export class DropGroupDirective implements OnInit {
120121
currentIndex: number
121122
): void {
122123
this.criteria = this.feasibilityQuery.getExclusionCriteria();
123-
this.criteria = this.deleteCriterion(this.criteria, criterionID);
124-
this.addCriterionToInnerArray(this.criteria, criterionID, currentIndex);
124+
this.moveCriterion(criterionID, previousIndex, currentIndex);
125125
this.queryProviderService.setExclusionCriteria(this.criteria);
126126
}
127127

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+
128163
private addCriterionToInnerArray(
129164
criteria: string[][],
130165
criterionID: string,

0 commit comments

Comments
 (0)