Project

General

Profile

1
import { Pipe, PipeTransform} from '@angular/core';
2
import { Filter, Value} from '../../searchPages/searchUtils/searchHelperClasses.class';
3
import { SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
4
import { ErrorCodes} from '../properties/openaireProperties';
5

    
6
@Pipe({
7
  name: 'contentProvidersDatatable'
8
})
9
export class ContentProvidersDatatablePipe implements PipeTransform  {
10

    
11
  transform(array: any[], args: any[]): any {
12
    if(array.length > 0) {
13
      let searchUtils: SearchUtilsClass = args[0];
14
      let filters:Filter[] = args[1];
15

    
16

    
17
      var errorCodes:ErrorCodes = new ErrorCodes();
18
      searchUtils.status = errorCodes.LOADING;
19

    
20
      var result = array.filter(row=>this.filterAll(row, searchUtils.keyword.toLowerCase(), filters));
21

    
22
      searchUtils.totalResults = result.length;
23

    
24
      var errorCodes:ErrorCodes = new ErrorCodes();
25
      searchUtils.status = errorCodes.DONE;
26
      if(searchUtils.totalResults == 0 ){
27
        searchUtils.status = errorCodes.NONE;
28
      }
29
      return result;
30
    }
31
    return [];
32
  }
33

    
34
  filterAll(row: any, query: string, filters:Filter[]) {
35
    let returnValue: boolean = false;
36

    
37
    if(query) {
38
      if(row.title.name.toLowerCase().indexOf(query) > -1) {
39
        returnValue = true;
40
      }
41

    
42
      if(row.type.toLowerCase().indexOf(query) > -1) {
43
        returnValue = true;
44
      }
45

    
46
      if(row.countries && row.countries.length > 0) {
47
        for(let country of row.countries) {
48
          if(country.toLowerCase().indexOf(query) > -1) {
49
            returnValue = true;
50
            break;
51
          }
52
        }
53
      }
54

    
55
      if(row.compatibility && row.compatibility.toLowerCase().indexOf(query) > -1) {
56
        returnValue = true;
57
      }
58

    
59
      if(row.organizations && row.organizations.length > 0) {
60
        for(let organization of row.organizations) {
61
          if(organization.name.toLowerCase().indexOf(query) > -1) {
62
            returnValue = true;
63
            break;
64
          }
65
        }
66
      }
67

    
68
      if(!returnValue) {
69
        return false;
70
      }
71
    }
72

    
73
    for (let filter of filters){
74
      if(filter.countSelectedValues > 0){
75
        for (let value of filter.values){
76
          if(value.selected == true){
77

    
78
            // make it generic in future commit
79
            let field:string = "";
80
            if(filter.title == "Type") {
81
              field = "type";
82
            } else if(filter.title == "Compatibility Level") {
83
              field = "compatibility";
84
            }
85

    
86
            if(row[field] == value.name) {
87
              returnValue = true;
88
              if(filter.filterOperator == "or") {
89
                break;
90
              }
91
            } else {
92
                if(filter.filterOperator == "and") {
93
                  return false;
94
                }
95
                returnValue = false;
96
            }
97
          }
98
        }
99
        if(!returnValue) {
100
          return false;
101
        }
102
      }
103
    }
104

    
105
    return true;
106
  }
107
}
(2-2/3)