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' || field === 'cc_licence' || field === 'abstract'
37
    || field === 'funders_collab' || field === 'projects_collab' || field === 'authors_collab') {
38
      if (this.isDescending) {
39
        if (this.isPercentage) {
40
          this.countries.sort((a, b) => b[field].percentage - a[field].percentage);
41
        } else {
42
          this.countries.sort((a, b) => b[field].oa - a[field].oa);
43
        }
44
      } else {
45
        if (this.isPercentage) {
46
          this.countries.sort((a, b) => a[field].percentage - b[field].percentage);
47
        } else {
48
          this.countries.sort((a, b) => a[field].oa - b[field].oa);
49
        }
50
      }
51
    } else if (field !== 'country') {
52
      if (this.isDescending) {
53
        this.countries.sort((a, b) => b[field] - a[field]);
54
      } else {
55
        this.countries.sort((a, b) => a[field] - b[field]);
56
      }
57
    } else {
58
      if (this.isDescending) {
59
        this.countries.sort((a, b) => (a['name'] < b['name']) ? 1 : -1);
60
      } else {
61
        this.countries.sort((a, b) => (a['name'] > b['name']) ? 1 : -1);
62
      }
63
    }
64

    
65
  }
66
}
(4-4/12)