2020-04-11 04:06:27 +02:00
|
|
|
import { Component, OnInit, ViewChild, Input, Type } from '@angular/core';
|
2020-03-30 19:44:54 +02:00
|
|
|
import Folder from '../../folder'
|
|
|
|
import { FolderService } from 'src/app/services/folder.service';
|
|
|
|
import { DonutChartComponent } from '../donut-chart/donut-chart.component';
|
|
|
|
import { DeviceService } from 'src/app/services/device.service';
|
|
|
|
import Device from 'src/app/device';
|
2020-04-02 02:43:54 +02:00
|
|
|
import { StType } from '../../type';
|
|
|
|
import { FilterService } from 'src/app/services/filter.service';
|
2020-04-11 04:06:27 +02:00
|
|
|
import { Observable } from 'rxjs';
|
2020-03-30 19:44:54 +02:00
|
|
|
|
|
|
|
|
2020-04-03 16:08:56 +02:00
|
|
|
export interface ChartItemState {
|
|
|
|
label: string,
|
|
|
|
count: number,
|
|
|
|
color: string,
|
|
|
|
selected: boolean,
|
|
|
|
}
|
2020-03-30 19:44:54 +02:00
|
|
|
@Component({
|
|
|
|
selector: 'app-chart',
|
|
|
|
templateUrl: './chart.component.html',
|
|
|
|
styleUrls: ['./chart.component.scss']
|
|
|
|
})
|
|
|
|
|
|
|
|
export class ChartComponent implements OnInit {
|
|
|
|
@ViewChild(DonutChartComponent) donutChart: DonutChartComponent;
|
2020-04-02 02:43:54 +02:00
|
|
|
@Input() type: StType;
|
2020-03-30 19:44:54 +02:00
|
|
|
title: string;
|
|
|
|
chartID: string;
|
2020-04-03 16:08:56 +02:00
|
|
|
states: ChartItemState[] = [];
|
|
|
|
|
2020-04-11 04:06:27 +02:00
|
|
|
private observer: Observable<any>;
|
2020-04-03 16:08:56 +02:00
|
|
|
private activeChartState: ChartItemState;
|
2020-03-30 19:44:54 +02:00
|
|
|
|
2020-04-02 02:43:54 +02:00
|
|
|
constructor(
|
|
|
|
private folderService: FolderService,
|
2020-04-11 04:06:27 +02:00
|
|
|
private deviceService: DeviceService,
|
2020-04-02 02:43:54 +02:00
|
|
|
private filterService: FilterService,
|
|
|
|
) { }
|
|
|
|
|
2020-04-03 16:08:56 +02:00
|
|
|
onItemSelect(s: ChartItemState) {
|
2020-04-02 02:43:54 +02:00
|
|
|
// Send chart item state to filter
|
2020-04-03 16:08:56 +02:00
|
|
|
this.filterService.changeFilter({ type: this.type, text: s.label });
|
|
|
|
|
|
|
|
// Deselect all other items
|
|
|
|
this.states.forEach(s => {
|
|
|
|
s.selected = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Select item only
|
|
|
|
if (s !== this.activeChartState) {
|
|
|
|
s.selected = true;
|
|
|
|
this.activeChartState = s;
|
|
|
|
} else {
|
|
|
|
this.activeChartState = null;
|
|
|
|
this.filterService.changeFilter({ type: this.type, text: "" })
|
|
|
|
}
|
2020-04-02 02:43:54 +02:00
|
|
|
}
|
2020-03-30 19:44:54 +02:00
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
switch (this.type) {
|
2020-04-02 02:43:54 +02:00
|
|
|
case StType.Folder:
|
2020-03-30 19:44:54 +02:00
|
|
|
this.title = "Folders";
|
|
|
|
this.chartID = 'foldersChart';
|
2020-04-11 04:06:27 +02:00
|
|
|
this.observer = this.folderService.folderAdded$;
|
2020-03-30 19:44:54 +02:00
|
|
|
break;
|
2020-04-02 02:43:54 +02:00
|
|
|
case StType.Device:
|
2020-03-30 19:44:54 +02:00
|
|
|
this.title = "Devices";
|
|
|
|
this.chartID = 'devicesChart';
|
2020-04-11 04:06:27 +02:00
|
|
|
this.observer = this.deviceService.deviceAdded$;
|
2020-03-30 19:44:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
let totalCount: number = 0;
|
2020-04-11 04:06:27 +02:00
|
|
|
this.observer.subscribe(
|
2020-04-01 01:24:36 +02:00
|
|
|
t => {
|
2020-03-30 19:44:54 +02:00
|
|
|
// Count the number of folders and set chart
|
|
|
|
totalCount++;
|
|
|
|
this.donutChart.count = totalCount;
|
|
|
|
|
|
|
|
// Get StateType and convert to string
|
2020-04-01 01:24:36 +02:00
|
|
|
const stateType = t.stateType;
|
|
|
|
const state = t.state;
|
2020-03-30 19:44:54 +02:00
|
|
|
let color;
|
|
|
|
switch (this.type) {
|
2020-04-02 02:43:54 +02:00
|
|
|
case StType.Folder:
|
2020-04-01 01:24:36 +02:00
|
|
|
color = Folder.stateTypeToColor(t.stateType);
|
2020-03-30 19:44:54 +02:00
|
|
|
break;
|
2020-04-02 02:43:54 +02:00
|
|
|
case StType.Device:
|
2020-03-30 19:44:54 +02:00
|
|
|
color = Device.stateTypeToColor(stateType);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if state exists
|
|
|
|
let found: boolean = false;
|
|
|
|
this.states.forEach(s => {
|
|
|
|
if (s.label === state) {
|
|
|
|
s.count = s.count + 1;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!found) {
|
2020-04-03 16:08:56 +02:00
|
|
|
this.states.push({ label: state, count: 1, color: color, selected: false });
|
2020-03-30 19:44:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.donutChart.updateData(this.states);
|
|
|
|
},
|
|
|
|
err => console.error('Observer got an error: ' + err),
|
|
|
|
() => {
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|