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
    console.log('countries ->', this.countries);
25
  }
26

    
27
  sortBy(field: string) {
28

    
29
    if (field === this.isSortedBy) {
30
      this.isDescending = !this.isDescending;
31
    } else {
32
      this.isDescending = true;
33
    }
34

    
35
    this.isSortedBy = field;
36

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

    
66
  }
67
}
(4-4/12)