|
1 | 1 | import { Component } from '@angular/core'; |
2 | | -import { ClickEvent } from 'devextreme/ui/button'; |
| 2 | +import { |
| 3 | + Employee, |
| 4 | + DragChangeEvent, |
| 5 | + ReorderEvent, |
| 6 | + SelectionChangedEvent, |
| 7 | +} from './app.types'; |
| 8 | +import { EmployeesService } from './employees.service'; |
3 | 9 |
|
4 | 10 | @Component({ |
5 | 11 | selector: 'app-root', |
6 | 12 | templateUrl: './app.component.html', |
7 | 13 | styleUrls: ['./app.component.scss'], |
8 | 14 | }) |
9 | 15 | export class AppComponent { |
10 | | - title = 'Angular'; |
| 16 | + employees: Employee[] = []; |
11 | 17 |
|
12 | | - counter = 0; |
| 18 | + selectedEmployee: Employee | null = null; |
13 | 19 |
|
14 | | - buttonText = 'Click count: 0'; |
| 20 | + expanded = true; |
15 | 21 |
|
16 | | - onClick(e: ClickEvent): void { |
17 | | - this.counter++; |
18 | | - this.buttonText = `Click count: ${this.counter}`; |
| 22 | + expandedRowKeys: number[] = []; |
| 23 | + |
| 24 | + constructor(private readonly employeesService: EmployeesService) { |
| 25 | + this.employees = this.employeesService.getEmployees(); |
| 26 | + this.selectEmployee = this.selectEmployee.bind(this); |
| 27 | + this.onReorder = this.onReorder.bind(this); |
| 28 | + this.onDragChange = this.onDragChange.bind(this); |
| 29 | + } |
| 30 | + |
| 31 | + selectEmployee(e: SelectionChangedEvent): void { |
| 32 | + e.component.byKey(e.currentSelectedRowKeys[0]).then((employee: Employee) => { |
| 33 | + if (employee) { |
| 34 | + this.selectedEmployee = employee; |
| 35 | + } |
| 36 | + }).catch(() => { |
| 37 | + // Handle error silently |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + onDragChange(e: DragChangeEvent): void { |
| 42 | + const visibleRows = e.component.getVisibleRows(); |
| 43 | + const sourceNode = e.component.getNodeByKey(e.itemData.ID); |
| 44 | + let targetNode = visibleRows[e.toIndex].node; |
| 45 | + |
| 46 | + while (targetNode?.data) { |
| 47 | + if (targetNode.data.ID === sourceNode.data.ID) { |
| 48 | + e.cancel = true; |
| 49 | + break; |
| 50 | + } |
| 51 | + const parentNode = targetNode.parent; |
| 52 | + if (!parentNode) { |
| 53 | + break; |
| 54 | + } |
| 55 | + targetNode = parentNode; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + onReorder(e: ReorderEvent): void { |
| 60 | + const visibleRows = e.component.getVisibleRows(); |
| 61 | + const sourceData = e.itemData; |
| 62 | + const targetData = visibleRows[e.toIndex].data; |
| 63 | + |
| 64 | + if (e.dropInsideItem) { |
| 65 | + e.itemData.HeadID = targetData.ID; |
| 66 | + e.component.refresh().catch(() => { |
| 67 | + // Handle error silently |
| 68 | + }); |
| 69 | + } else { |
| 70 | + let targetIndex = this.employees.indexOf(targetData); |
| 71 | + |
| 72 | + if (sourceData.HeadID !== targetData.HeadID) { |
| 73 | + sourceData.HeadID = targetData.HeadID; |
| 74 | + if (e.toIndex > e.fromIndex) { |
| 75 | + targetIndex += 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + this.employeesService.reorderEmployees(sourceData, targetIndex); |
| 80 | + this.employees = this.employeesService.getEmployees(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + toggleExpansion(): void { |
| 85 | + this.expanded = !this.expanded; |
| 86 | + this.expandedRowKeys = []; |
19 | 87 | } |
20 | 88 | } |
0 commit comments