Project

General

Profile

1
import { Component, Input, OnChanges } from '@angular/core';
2
import { CountryTableData } from '../../domain/overview-map-data';
3

    
4
@Component({
5
  selector: 'app-countries-table',
6
  templateUrl: './countries-table.component.html',
7
})
8

    
9
export class CountriesTableComponent implements OnChanges {
10

    
11
  @Input() isPercentage: boolean;
12
  @Input() countries: CountryTableData[];
13

    
14
  @Input() view: string;
15
  @Input() entity: string;
16

    
17
  isSortedBy: string;
18
  isDescending: boolean = true;
19

    
20
  constructor() {}
21

    
22
  ngOnChanges() {
23
    this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
24
  }
25

    
26
  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
    if (field === 'pid' || field === 'licence' || field === 'gold' || field === 'green') {
37
      if (this.isDescending) {
38
        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
        this.countries.sort((a, b) => b[field] - a[field]);
53
      } else {
54
        this.countries.sort((a, b) => a[field] - b[field]);
55
      }
56
    } else {
57
      if (this.isDescending) {
58
        this.countries.sort((a, b) => (a['name'] < b['name']) ? 1 : -1);
59
      } else {
60
        this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
61
      }
62
    }
63

    
64
  }
65
}
(4-4/12)