Skip to content

Commit aa0721f

Browse files
committed
refactor(query-builder): dragdrop logic for keyboard drag
1 parent e33df44 commit aa0721f

File tree

2 files changed

+194
-125
lines changed

2 files changed

+194
-125
lines changed

projects/igniteui-angular/src/lib/query-builder/query-builder-tree.component.ts

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
12541254
}
12551255

12561256
//Make a copy of the drag chip and place it in the DOM north or south of the drop chip
1257-
private renderDropGhostChip(appendToElement: HTMLElement, appendUnder: boolean, keyboardMode?: boolean, overrideDropUnder?: boolean): void {
1257+
private renderDropGhostChip(appendToElement: HTMLElement, appendUnder: boolean, keyboardMode?: boolean): void {
12581258
const dragCopy = this.createDropGhost(keyboardMode);
12591259

12601260
//Append the ghost
@@ -1274,8 +1274,6 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
12741274
appendToElement.parentNode.insertBefore(this.dropGhostChipNode, appendToElement.nextElementSibling);
12751275
}
12761276

1277-
this.dropUnder = overrideDropUnder == null ? this.dropUnder : overrideDropUnder;
1278-
12791277
//Put focus on the drag icon of the ghost while performing keyboard drag
12801278
if (this.isKeyboardDrag) {
12811279
((this.dropGhostChipNode as HTMLElement).querySelector('.igx-drag-indicator') as HTMLElement).focus();
@@ -1396,52 +1394,27 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
13961394
this.keyDragOffsetIndex = newKeyIndexOffset;
13971395
const indexOffset = ~~(this.keyDragOffsetIndex / 2);
13981396

1399-
//If not the last drop zone (the +Condition button)
1400-
if (index + indexOffset <= this.dropZonesList.length - 2) {
1401-
const groupsTillCurrent = this.dropZonesList.filter((x, ix) => ix < index + indexOffset && x.className.indexOf('igx-filter-tree__expression-context-menu') !== -1).length;
1402-
1397+
if (index + indexOffset <= this.expressionsList.length - 1) {
14031398
let under = this.keyDragOffsetIndex < 0 ? this.keyDragOffsetIndex % 2 == 0 ? true : false : this.keyDragOffsetIndex % 2 == 0 ? false : true;
1404-
let overrideDropUnder;
14051399

1406-
//if the current drop zone is a condition chip
14071400
if (this.dropZonesList[index + indexOffset].className.indexOf('igx-filter-tree__expression-context-menu') === -1) {
14081401
this.targetElement = this.dropZonesList[index + indexOffset]
1409-
this.targetExpressionItem = this.expressionsList[index + indexOffset - groupsTillCurrent];
1402+
this.targetExpressionItem = this.expressionsList[index + indexOffset];
14101403
} else {
14111404
//if the current drop zone is a group root (AND/OR)
14121405
if (index + indexOffset === 0) {
14131406
//If the root group's AND/OR
14141407
this.targetElement = this.dropZonesList[0]
14151408
this.targetExpressionItem = this.rootGroup.children[0];
14161409
under = true;
1417-
overrideDropUnder = false;
14181410
} else if (under) {
14191411
//If under AND/OR
14201412
this.targetElement = this.dropZonesList[index + indexOffset]
1421-
this.targetExpressionItem = this.expressionsList[index + indexOffset - groupsTillCurrent];
1422-
overrideDropUnder = false;
1413+
this.targetExpressionItem = this.expressionsList[index + indexOffset + 1];
14231414
} else {
14241415
//if over AND/OR
14251416
this.targetElement = this.dropZonesList[index + indexOffset].parentElement.parentElement;
1426-
const targetEx = this.expressionsList[index + indexOffset - groupsTillCurrent];
1427-
this.targetExpressionItem = targetEx.parent ? targetEx.parent : targetEx;
1428-
}
1429-
1430-
//Calculation of proper targetExpression in case of multiple nested groups
1431-
if (index + indexOffset !== 0) {
1432-
let parentCount = 0;
1433-
let item = this.targetExpressionItem;
1434-
do {
1435-
parentCount++;
1436-
item = item.parent;
1437-
}
1438-
while (item.parent);
1439-
1440-
if (key.code == 'ArrowDown') parentCount--;
1441-
1442-
for (let index = 0; index < parentCount - groupsTillCurrent; index++) {
1443-
if (this.targetExpressionItem.parent) this.targetExpressionItem = this.targetExpressionItem.parent;
1444-
}
1417+
this.targetExpressionItem = this.expressionsList[index + indexOffset];
14451418
}
14461419

14471420
//If should drop under AND/OR => drop over first chip in that AND/OR's group
@@ -1451,13 +1424,12 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
14511424
under = false;
14521425
}
14531426
}
1454-
14551427
const before = this.getPreviousChip(this.dropGhostElement);
14561428
const after = this.getNextChip(this.dropGhostElement);
14571429

1458-
this.renderDropGhostChip(this.targetElement, under, true, overrideDropUnder);
1430+
this.renderDropGhostChip(this.targetElement, under, true);
14591431

1460-
//If it's the first arrow hit OR drop ghost is not displayed OR hasn't actually moved => move one more step in the same direction
1432+
//If it's the first arrow hit OR drop ghost is not displayed OR hasn't actually moved, move one more step in the same direction
14611433
if (this.keyDragFirstMove ||
14621434
!this.dropGhostElement ||
14631435
(this.getPreviousChip(this.dropGhostElement) === before && this.getNextChip(this.dropGhostElement) === after)) {
@@ -1510,10 +1482,11 @@ export class IgxQueryBuilderTreeComponent implements AfterViewInit, OnDestroy {
15101482
return nextElement;
15111483
}
15121484

1513-
//Get all expressions from the tree flatten out as a list
1485+
//Get all expressions from the tree flatten out as a list, including the expression groups
15141486
private getListedExpressions(group: ExpressionGroupItem): ExpressionItem[] {
15151487
const expressions: ExpressionItem[] = [];
15161488

1489+
expressions.push(group);
15171490
group.children.forEach(child => {
15181491
if (child instanceof ExpressionGroupItem) {
15191492
expressions.push(...this.getListedExpressions(child));

0 commit comments

Comments
 (0)