monitor-dashboard/src/app/topic/indicators.component.ts

141 lines
4.2 KiB
TypeScript
Raw Normal View History

import {Component, Input, OnChanges, OnInit, SimpleChanges} from "@angular/core";
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
import {Indicator, Stakeholder} from "../utils/entities/stakeholder";
import {IndicatorUtils} from "../utils/indicator-utils";
@Component({
selector: 'indicators',
templateUrl: './indicators.component.html'
})
export class IndicatorsComponent implements OnInit, OnChanges {
@Input()
public topicIndex: number = 0;
@Input()
public categoryIndex: number = 0;
@Input()
public subcategoryIndex: number = 0;
@Input()
public stakeholder: Stakeholder = null;
public indicatorUtils: IndicatorUtils = new IndicatorUtils();
/**
* All charts and numbers
*/
public charts: Indicator[] = [];
public numbers: Indicator[] = [];
/**
* Displayed chart and numbers base on Top filters
*/
public displayCharts: Indicator[] = [];
public displayNumbers: Indicator[] = [];
/**
* Top filters
*/
public chartType: string = 'all';
public privacy: string = 'all';
public status: string = 'all';
public keyword: string = null;
/**
* Grid or List View
*/
public grid: boolean = true;
constructor(private sideBarService: SideBarService) {}
ngOnInit(): void {
}
ngOnChanges(changes: SimpleChanges): void {
if(this.canEdit) {
this.charts = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
subCategories[this.subcategoryIndex].charts;
this.displayCharts = this.filterChartType(this.filterPrivacy(
this.filterStatus(this.filterByKeyword(this.charts, this.keyword), this.status),
this.privacy),
this.chartType
);
this.numbers = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
subCategories[this.subcategoryIndex].numbers;
this.displayNumbers = this.filterPrivacy(this.filterStatus(
this.filterByKeyword(this.numbers, this.keyword),
this.status),
this.privacy);
}
}
onChartTypeChange(value) {
this.displayCharts = this.filterChartType(this.charts, value);
}
onPrivacyChange(value) {
this.displayCharts = this.filterPrivacy(this.charts, value);
this.displayNumbers = this.filterPrivacy(this.numbers, value);
}
onStatusChange(value) {
this.displayCharts = this.filterStatus(this.charts, value);
this.displayNumbers = this.filterStatus(this.numbers, value);
}
onKeywordChange(value) {
this.displayCharts = this.filterByKeyword(this.charts, value);
this.displayNumbers = this.filterByKeyword(this.numbers, value);
}
filterChartType(indicators: Indicator[], value): Indicator[] {
if (value === 'all') {
return indicators;
} else {
return indicators.filter(indicator =>
indicator.indicatorPaths.filter(indicatorPath => indicatorPath.type === value).length > 0);
}
}
filterPrivacy(indicators: Indicator[], value): Indicator[] {
if (value === 'all') {
return indicators;
} else {
return indicators.filter(indicator => indicator.isPublic === (value === 'public'));
}
}
filterStatus(indicators: Indicator[], value): Indicator[] {
if (value === 'all') {
return indicators;
} else {
return indicators.filter(indicator => indicator.isActive === (value === 'active'));
}
}
filterByKeyword(indicators: Indicator[], value): Indicator[] {
if (value === null || value === '') {
return indicators;
} else {
return indicators.filter(indicator => indicator.name.includes(value) || indicator.description.includes(value));
}
}
get open(): boolean {
return this.sideBarService.open;
}
get canEdit() {
return this.stakeholder &&
this.stakeholder.topics[this.topicIndex] &&
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex];
}
public toggleOpen(event = null) {
if (!event) {
this.sideBarService.setOpen(!this.open);
} else if (event && event['value'] === true) {
this.sideBarService.setOpen(false);
}
}
public changeGrid(value) {
this.grid = value;
}
}