-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.component.ts
More file actions
86 lines (73 loc) · 2.49 KB
/
Copy pathapp.component.ts
File metadata and controls
86 lines (73 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { EmployeesService, type Employee } from './employees.service';
import { DxTreeListModule, type DxTreeListTypes } from 'devextreme-angular/ui/tree-list';
import { DxButtonModule } from 'devextreme-angular/ui/button';
@Component({
selector: 'app-root',
imports: [DxTreeListModule, DxButtonModule],
templateUrl: './app.component.html',
changeDetection: ChangeDetectionStrategy.Eager,
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
employees: Employee[] = [];
selectedEmployee: Employee | null = null;
expanded = true;
expandedRowKeys: number[] = [];
constructor(private readonly employeesService: EmployeesService) {
this.employees = this.employeesService.getEmployees();
this.selectEmployee = this.selectEmployee.bind(this);
this.onReorder = this.onReorder.bind(this);
this.onDragChange = this.onDragChange.bind(this);
}
selectEmployee(e: DxTreeListTypes.SelectionChangedEvent): void {
e.component.byKey(e.currentSelectedRowKeys[0]).then((employee: Employee) => {
if (employee) {
this.selectedEmployee = employee;
}
}).catch(() => {
// Handle error silently
});
}
onDragChange(e: any): void {
const visibleRows = e.component.getVisibleRows();
const sourceNode = e.component.getNodeByKey(e.itemData.ID);
let targetNode = visibleRows[e.toIndex].node;
while (targetNode?.data) {
if (targetNode.data.ID === sourceNode.data.ID) {
e.cancel = true;
break;
}
const parentNode = targetNode.parent;
if (!parentNode) {
break;
}
targetNode = parentNode;
}
}
onReorder(e: any): void {
const visibleRows = e.component.getVisibleRows();
const sourceData = e.itemData;
const targetData = visibleRows[e.toIndex].data;
if (e.dropInsideItem) {
e.itemData.HeadID = targetData.ID;
e.component.refresh().catch(() => {
// Handle error silently
});
} else {
let targetIndex = this.employees.indexOf(targetData);
if (sourceData.HeadID !== targetData.HeadID) {
sourceData.HeadID = targetData.HeadID;
if (e.toIndex > e.fromIndex) {
targetIndex += 1;
}
}
this.employeesService.reorderEmployees(sourceData, targetIndex);
this.employees = this.employeesService.getEmployees();
}
}
toggleExpansion(): void {
this.expanded = !this.expanded;
this.expandedRowKeys = [];
}
}