import {Component, EventEmitter, Output} from '@angular/core'; import {MatIconModule} from "@angular/material/icon"; import {MatButtonModule} from "@angular/material/button"; import {MatTreeFlatDataSource, MatTreeFlattener, MatTreeModule} from "@angular/material/tree"; import {FlatTreeControl} from "@angular/cdk/tree"; import {RecursiveTaskgroupInfo, TaskgroupService, TaskOverviewInfo, TaskShortInfo} from "../../../api"; import {TaskCreationEmitterInfo, TaskOverviewComponent} from "../task-overview/task-overview.component"; /** Flat node with expandable and level information */ interface ExampleFlatNode { expandable: boolean; level: number; name: string activeTasks: number; hasOverdueTask: boolean; tasks: TaskOverviewInfo[]; taskgroupID: number } export interface TaskOverviewData { tasks: TaskOverviewInfo[], taskgroupID: number } @Component({ selector: 'app-taskgroup-overview', templateUrl: './taskgroup-overview.component.html', styleUrls: ['./taskgroup-overview.component.css'] }) export class TaskgroupOverviewComponent { @Output('taskgroupSelected') taskgroupSelected: EventEmitter = new EventEmitter(); private _transformer = (node: RecursiveTaskgroupInfo, level: number) => { return { expandable: !!node.childTaskgroups && node.childTaskgroups.length > 0, name: node.taskgroupName, level: level, activeTasks: node.amountActiveTasks, hasOverdueTask: node.hasOverdueTask, tasks: node.activeTasks, taskgroupID: node.taskgroupID }; }; treeControl = new FlatTreeControl( node => node.level, node => node.expandable, ); treeFlattener = new MatTreeFlattener( this._transformer, node => node.level, node => node.expandable, node => node.childTaskgroups, ); dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener); constructor(private taskgroupService: TaskgroupService) { } ngOnInit() { this.taskgroupService.taskgroupsTreeGet().subscribe({ next: resp => { this.dataSource.data = resp; } }) } hasChild = (_: number, node: ExampleFlatNode) => node.expandable; onSelectTaskgroup(tasks: TaskOverviewInfo[], taskgroupID: number) { this.taskgroupSelected.emit({ tasks: tasks, taskgroupID: taskgroupID }) } finishTask(taskID: number) { let taskgroupQueue : RecursiveTaskgroupInfo[] = this.dataSource.data.slice(); while(taskgroupQueue.length > 0) { const currentTaskgroup = taskgroupQueue.pop()!; const searchedTask = currentTaskgroup.activeTasks.find(searchedTask => searchedTask.taskID === taskID); if(searchedTask == undefined) { currentTaskgroup.childTaskgroups.forEach(childTask => { taskgroupQueue.push(childTask); }) } else { currentTaskgroup.activeTasks = currentTaskgroup.activeTasks.filter(taskFilter => taskFilter.taskID !== searchedTask.taskID); } } } private findTaskgroup(taskgroupID: number) { let taskgroupQueue : RecursiveTaskgroupInfo[] = this.dataSource.data.slice(); while(taskgroupQueue.length > 0) { const currentTaskgroup = taskgroupQueue.pop()!; if(currentTaskgroup.taskgroupID === taskgroupID) { return currentTaskgroup; } else { currentTaskgroup.childTaskgroups.forEach(childgroup => taskgroupQueue.push(childgroup)); } } return undefined } onCreateTask(taskCreationInfo: TaskCreationEmitterInfo) { const taskgroup: RecursiveTaskgroupInfo | undefined = this.findTaskgroup(taskCreationInfo.taskgroupID); if(taskgroup != undefined) { taskgroup.activeTasks.push(taskCreationInfo.task); } } }