Project

General

Profile

1 58825 stefania.m
import { Component, Input, OnChanges } from '@angular/core';
2
import { CountryTableData } from '../../domain/overview-map-data';
3 57592 stefania.m
4
@Component({
5
  selector: 'app-countries-table',
6
  templateUrl: './countries-table.component.html',
7
})
8
9 57893 stefania.m
export class CountriesTableComponent implements OnChanges {
10 57592 stefania.m
11
  @Input() isPercentage: boolean;
12 58825 stefania.m
  @Input() countries: CountryTableData[];
13 57592 stefania.m
14 58946 stefania.m
  @Input() view: string;
15
  @Input() entity: string;
16
17 57592 stefania.m
  isSortedBy: string;
18
  isDescending: boolean = true;
19
20
  constructor() {}
21
22 57893 stefania.m
  ngOnChanges() {
23 58825 stefania.m
    this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
24 57893 stefania.m
  }
25
26 57592 stefania.m
  sortBy(field: string) {
27
28
    if (field === this.isSortedBy) {
29
      this.isDescending = !this.isDescending;
30
    } else {
31
      this.isDescending = true;
32
    }
33
34
    this.isSortedBy = field;
35
36 58946 stefania.m
    if (field === 'pid' || field === 'licence' || field === 'gold' || field === 'green') {
37 58825 stefania.m
      if (this.isDescending) {
38 58946 stefania.m
        if (this.isPercentage) {
39
          this.countries.sort((a, b) => b[field].percentage - a[field].percentage);
40
        } else {
41
          this.countries.sort((a, b) => b[field].oa - a[field].oa);
42
        }
43
      } else {
44
        if (this.isPercentage) {
45
          this.countries.sort((a, b) => a[field].percentage - b[field].percentage);
46
        } else {
47
          this.countries.sort((a, b) => a[field].oa - b[field].oa);
48
        }
49
      }
50
    } else if (field !== 'country') {
51
      if (this.isDescending) {
52 58825 stefania.m
        this.countries.sort((a, b) => b[field] - a[field]);
53 57592 stefania.m
      } else {
54 58825 stefania.m
        this.countries.sort((a, b) => a[field] - b[field]);
55 57592 stefania.m
      }
56
    } else {
57
      if (this.isDescending) {
58 58825 stefania.m
        this.countries.sort((a, b) => (a['name'] < b['name']) ? 1 : -1);
59 57592 stefania.m
      } else {
60 58825 stefania.m
        this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
61 57592 stefania.m
      }
62
    }
63
64
  }
65
}