Project

General

Profile

1
import {Component, Input, OnChanges, OnInit, SimpleChanges} from "@angular/core";
2
import {SideBarService} from "../library/sharedComponents/sidebar/sideBar.service";
3
import {Indicator, Stakeholder} from "../utils/entities/stakeholder";
4
import {IndicatorUtils} from "../utils/indicator-utils";
5

    
6
@Component({
7
  selector: 'indicators',
8
  templateUrl: './indicators.component.html'
9
})
10
export class IndicatorsComponent implements OnInit, OnChanges {
11

    
12
  @Input()
13
  public topicIndex: number = 0;
14
  @Input()
15
  public categoryIndex: number = 0;
16
  @Input()
17
  public subcategoryIndex: number = 0;
18
  @Input()
19
  public stakeholder: Stakeholder = null;
20
  public indicatorUtils: IndicatorUtils = new IndicatorUtils();
21
  /**
22
   * All charts and numbers
23
   */
24
  public charts: Indicator[] = [];
25
  public numbers: Indicator[] = [];
26
  /**
27
   * Displayed chart and numbers base on Top filters
28
   */
29
  public displayCharts: Indicator[] = [];
30
  public displayNumbers: Indicator[] = [];
31
  /**
32
   * Top filters
33
   */
34
  public chartType: string = 'all';
35
  public privacy: string = 'all';
36
  public status: string = 'all';
37
  public keyword: string = null;
38
  /**
39
   * Grid or List View
40
   */
41
  public grid: boolean = true;
42

    
43
  constructor(private sideBarService: SideBarService) {}
44

    
45
  ngOnInit(): void {
46
  }
47

    
48
  ngOnChanges(changes: SimpleChanges): void {
49
    if(this.canEdit) {
50
      this.charts = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
51
        subCategories[this.subcategoryIndex].charts;
52
      this.displayCharts = this.filterChartType(this.filterPrivacy(
53
        this.filterStatus(this.filterByKeyword(this.charts, this.keyword), this.status),
54
        this.privacy),
55
        this.chartType
56
      );
57
      this.numbers = this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].
58
        subCategories[this.subcategoryIndex].numbers;
59
      this.displayNumbers = this.filterPrivacy(this.filterStatus(
60
        this.filterByKeyword(this.numbers, this.keyword),
61
        this.status),
62
        this.privacy);
63
    }
64
  }
65

    
66
  onChartTypeChange(value) {
67
    this.displayCharts = this.filterChartType(this.charts, value);
68
  }
69

    
70
  onPrivacyChange(value) {
71
    this.displayCharts = this.filterPrivacy(this.charts, value);
72
    this.displayNumbers = this.filterPrivacy(this.numbers, value);
73
  }
74

    
75
  onStatusChange(value) {
76
    this.displayCharts = this.filterStatus(this.charts, value);
77
    this.displayNumbers = this.filterStatus(this.numbers, value);
78
  }
79

    
80
  onKeywordChange(value) {
81
    this.displayCharts = this.filterByKeyword(this.charts, value);
82
    this.displayNumbers = this.filterByKeyword(this.numbers, value);
83
  }
84

    
85
  filterChartType(indicators: Indicator[], value): Indicator[] {
86
    if (value === 'all') {
87
      return indicators;
88
    } else {
89
      return indicators.filter(indicator =>
90
        indicator.indicatorPaths.filter(indicatorPath => indicatorPath.type === value).length > 0);
91
    }
92
  }
93

    
94
  filterPrivacy(indicators: Indicator[], value): Indicator[] {
95
    if (value === 'all') {
96
      return indicators;
97
    } else {
98
      return indicators.filter(indicator => indicator.isPublic === (value === 'public'));
99
    }
100
  }
101

    
102
  filterStatus(indicators: Indicator[], value): Indicator[] {
103
    if (value === 'all') {
104
      return indicators;
105
    } else {
106
      return indicators.filter(indicator => indicator.isActive === (value === 'active'));
107
    }
108
  }
109

    
110
  filterByKeyword(indicators: Indicator[], value): Indicator[] {
111
    if (value === null || value === '') {
112
      return indicators;
113
    } else {
114
      return indicators.filter(indicator => indicator.name.includes(value) || indicator.description.includes(value));
115
    }
116
  }
117

    
118
  get open(): boolean {
119
    return this.sideBarService.open;
120
  }
121

    
122
  get canEdit() {
123
    return this.stakeholder &&
124
      this.stakeholder.topics[this.topicIndex] &&
125
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex] &&
126
      this.stakeholder.topics[this.topicIndex].categories[this.categoryIndex].subCategories[this.subcategoryIndex];
127
  }
128

    
129
  public toggleOpen(event = null) {
130
    if (!event) {
131
      this.sideBarService.setOpen(!this.open);
132
    } else if (event && event['value'] === true) {
133
      this.sideBarService.setOpen(false);
134
    }
135
  }
136

    
137
  public changeGrid(value) {
138
    this.grid = value;
139
  }
140
}
(2-2/6)