openaire-library/utils/pipes/contentProvidersDatatable.p...

114 lines
3.0 KiB
TypeScript

import { Pipe, PipeTransform} from '@angular/core';
import { Filter, Value} from '../../searchPages/searchUtils/searchHelperClasses.class';
import { SearchUtilsClass } from '../../searchPages/searchUtils/searchUtils.class';
import { ErrorCodes} from '../properties/openaireProperties';
@Pipe({
name: 'contentProvidersDatatable'
})
export class ContentProvidersDatatablePipe implements PipeTransform {
transform(array: any[], args: any[]): any {
if(array.length > 0) {
let searchUtils: SearchUtilsClass = args[0];
let filters:Filter[] = args[1];
var errorCodes:ErrorCodes = new ErrorCodes();
searchUtils.status = errorCodes.LOADING;
var result = array.filter(row=>this.filterAll(row, searchUtils.keyword.toLowerCase(), filters));
let oldTotal = searchUtils.totalResults;
searchUtils.totalResults = result.length;
var errorCodes:ErrorCodes = new ErrorCodes();
searchUtils.status = errorCodes.DONE;
if(searchUtils.totalResults == 0 ){
searchUtils.status = errorCodes.NONE;
}
if(oldTotal != searchUtils.totalResults) {
args[3].detectChanges();
}
return result;
}
return [];
}
filterAll(row: any, query: string, filters:Filter[]) {
let returnValue: boolean = false;
if(query) {
if(row.title.name.toLowerCase().indexOf(query) > -1) {
returnValue = true;
}
if(row.type.toLowerCase().indexOf(query) > -1) {
returnValue = true;
}
if(row.countries && row.countries.length > 0) {
for(let country of row.countries) {
if(country.toLowerCase().indexOf(query) > -1) {
returnValue = true;
break;
}
}
}
if(row.compatibility && row.compatibility.toLowerCase().indexOf(query) > -1) {
returnValue = true;
}
if(row.organizations && row.organizations.length > 0) {
for(let organization of row.organizations) {
if(organization.name.toLowerCase().indexOf(query) > -1) {
returnValue = true;
break;
}
}
}
if(!returnValue) {
return false;
}
}
for (let filter of filters){
if(filter.countSelectedValues > 0){
for (let value of filter.values){
if(value.selected == true){
// make it generic in future commit
let field:string = "";
if(filter.title == "Type") {
field = "type";
} else if(filter.title == "Compatibility Level") {
field = "compatibility";
}
if(row[field] == value.name) {
returnValue = true;
if(filter.filterOperator == "or") {
break;
}
} else {
if(filter.filterOperator == "and") {
return false;
}
returnValue = false;
}
}
}
if(!returnValue) {
return false;
}
}
}
return true;
}
}