From b5b1a5f6efe3e6c57d9d646f67047df321e249bc Mon Sep 17 00:00:00 2001 From: megli2 Date: Tue, 23 Apr 2024 14:30:24 +0200 Subject: [PATCH] fix moving rows --- .../action-plan/action-plan.component.html | 2 +- .../app/action-plan/action-plan.component.ts | 38 ++++++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/action-plan/action-plan.component.html b/frontend/src/app/action-plan/action-plan.component.html index 34e9bf4c05..a92d18a3fa 100644 --- a/frontend/src/app/action-plan/action-plan.component.html +++ b/frontend/src/app/action-plan/action-plan.component.html @@ -23,10 +23,10 @@ #listItem (keydown.arrowDown)="handleKeyDown($event, i)" (keydown.arrowUp)="handleKeyDown($event, i)" + (input)="this.setTexts()" (focusin)="activeItem = i" class="action-input" [value]="action.action" - (blur)="changeActionText($event, i)" [attr.data-testId]="'actionInput'" /> = new BehaviorSubject([]); +export class ActionPlanComponent { + @Input() control: BehaviorSubject = new BehaviorSubject([]); @Input() keyResultId!: number | null; activeItem: number = 0; - listSubscription!: Subscription; @ViewChildren('listItem') listItems!: QueryList; @@ -27,14 +26,6 @@ export class ActionPlanComponent implements AfterViewInit { public dialog: MatDialog, ) {} - ngAfterViewInit() { - this.listSubscription = this.listItems.changes.subscribe((_) => { - if (this.listItems.length > 0) { - this.listItems.toArray()[this.activeItem].nativeElement.focus(); - } - }); - } - handleKeyDown(event: Event, currentIndex: number) { let newIndex = currentIndex; if ((event as KeyboardEvent).key === 'ArrowDown') { @@ -47,12 +38,21 @@ export class ActionPlanComponent implements AfterViewInit { } } this.changeItemPosition(newIndex, currentIndex); - this.adjustPriorities(); + this.listItems.get(this.activeItem)?.nativeElement.focus(); } changeItemPosition(newIndex: number, currentIndex: number) { - moveItemInArray(this.control.getValue()!, currentIndex, newIndex); this.activeItem = newIndex; + let texts = Array.from(document.getElementsByClassName('action-input')).map((input: any) => input.value); + moveItemInArray(texts, currentIndex, newIndex); + let mappedArray = texts.map((text, index) => ({ action: text, priority: index, keyResultId: this.keyResultId })); + this.control.next(mappedArray); + } + + setTexts() { + let texts = Array.from(document.getElementsByClassName('action-input')).map((input: any) => input.value); + let mappedArray = texts.map((text, index) => ({ action: text, priority: index, keyResultId: this.keyResultId })); + this.control.next(mappedArray); } increaseActiveItemWithTab() { @@ -84,15 +84,9 @@ export class ActionPlanComponent implements AfterViewInit { this.activeItem = event.currentIndex; } - changeActionText(event: any, index: number) { - const actions = this.control.getValue()!; - actions[index] = { ...actions[index], action: event.target.value! }; - this.control.next(actions); - } - adjustPriorities() { const actions = this.control.getValue()!; - actions.forEach(function (action, index) { + actions.forEach(function (action: Action, index: number) { action.priority = index; }); this.control.next(actions);