Skip to content

Commit bc91f29

Browse files
authored
Merge pull request #446 from medizininformatik-initiative/419-sorting-of-criteria-in-the-cohort-selection-is-not-possible
first draft for fixing drag&drop issues
2 parents 2d4b31d + 2ad3d7c commit bc91f29

File tree

1 file changed

+85
-9
lines changed

1 file changed

+85
-9
lines changed

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

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ export class DropGroupDirective implements OnInit {
5252
} else {
5353
switch (event.container.id) {
5454
case 'Exclusion':
55-
this.moveCriterionInExclusion(event.previousIndex, event.currentIndex);
55+
this.moveCriterionInExclusion(droppedCriterion, event.previousIndex, event.currentIndex);
5656
break;
5757
case 'Inclusion':
58-
this.moveCriterionInInclusion(event.previousIndex, event.currentIndex);
58+
this.moveCriterionInInclusion(droppedCriterion, event.previousIndex, event.currentIndex);
5959
break;
6060
default:
6161
break;
@@ -70,12 +70,12 @@ export class DropGroupDirective implements OnInit {
7070
}
7171
private addToInclusion(droppedCriterion: string, currentIndex: number): void {
7272
this.criteria = this.feasibilityQuery.getInclusionCriteria();
73-
this.criteria.splice(currentIndex, 0, [droppedCriterion]);
73+
this.addCriterionToInnerArray(this.criteria, droppedCriterion, currentIndex);
7474
this.queryProviderService.setInclusionCriteria(this.criteria);
7575
}
7676
private addToExclusion(droppedCriterion: string, currentIndex: number): void {
7777
this.criteria = this.feasibilityQuery.getExclusionCriteria();
78-
this.criteria.splice(currentIndex, 0, [droppedCriterion]);
78+
this.addCriterionToInnerArray(this.criteria, droppedCriterion, currentIndex);
7979
this.queryProviderService.setExclusionCriteria(this.criteria);
8080
}
8181

@@ -95,23 +95,99 @@ 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;
106108
}
107-
private moveCriterionInInclusion(previousIndex: number, currentIndex: number): void {
109+
private moveCriterionInInclusion(
110+
criterionID: string,
111+
previousIndex: number,
112+
currentIndex: number
113+
): void {
108114
this.criteria = this.feasibilityQuery.getInclusionCriteria();
109-
moveItemInArray(this.criteria, previousIndex, currentIndex);
115+
this.moveCriterion(criterionID, previousIndex, currentIndex);
110116
this.queryProviderService.setInclusionCriteria(this.criteria);
111117
}
112-
private moveCriterionInExclusion(previousIndex: number, currentIndex: number): void {
118+
private moveCriterionInExclusion(
119+
criterionID: string,
120+
previousIndex: number,
121+
currentIndex: number
122+
): void {
113123
this.criteria = this.feasibilityQuery.getExclusionCriteria();
114-
moveItemInArray(this.criteria, previousIndex, currentIndex);
124+
this.moveCriterion(criterionID, previousIndex, currentIndex);
115125
this.queryProviderService.setExclusionCriteria(this.criteria);
116126
}
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+
}
117193
}

0 commit comments

Comments
 (0)