2020-07-14 14:41:09 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
Output,
|
|
|
|
EventEmitter,
|
|
|
|
OnInit,
|
|
|
|
OnChanges,
|
|
|
|
SimpleChanges
|
|
|
|
} from '@angular/core';
|
2022-01-20 10:19:44 +01:00
|
|
|
import {Filter, Value} from './searchHelperClasses.class';
|
2020-11-11 15:43:13 +01:00
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
2021-05-19 09:39:08 +02:00
|
|
|
import {SearchFields} from "../../utils/properties/searchFields";
|
2022-03-15 22:31:10 +01:00
|
|
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
2022-04-07 17:17:52 +02:00
|
|
|
import {Option} from "../../sharedComponents/input/input.component";
|
2017-12-19 13:53:46 +01:00
|
|
|
@Component({
|
2022-01-20 10:19:44 +01:00
|
|
|
selector: 'search-filter',
|
|
|
|
templateUrl: 'searchFilter.component.html'
|
2017-12-19 13:53:46 +01:00
|
|
|
})
|
|
|
|
|
2022-01-20 10:19:44 +01:00
|
|
|
export class SearchFilterComponent implements OnInit, OnChanges {
|
|
|
|
|
|
|
|
@Input() filter: Filter;
|
|
|
|
@Input() showResultCount: boolean = true;
|
|
|
|
@Input() isDisabled: boolean = false;
|
|
|
|
@Input() addShowMore: boolean = true;
|
2020-02-17 15:19:14 +01:00
|
|
|
@Input() showMoreInline: boolean = true;
|
2020-04-28 12:07:53 +02:00
|
|
|
@Input() filterValuesNum: number = 6;
|
2022-01-20 10:19:44 +01:00
|
|
|
public showAll: boolean = false;
|
|
|
|
public _maxCharacters: number = 28;
|
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
@Output() toggleModal = new EventEmitter();
|
2022-01-20 10:19:44 +01:00
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
@Output() modalChange = new EventEmitter();
|
|
|
|
@Output() onFilterChange = new EventEmitter();
|
|
|
|
keyword = "";
|
2022-03-16 17:54:22 +01:00
|
|
|
sortBy: "name" | "num" = "name";
|
2022-04-07 17:17:52 +02:00
|
|
|
sortByOptions: Option[] = [{label: 'Results number', value: 'num'}, {label: 'Name', value: 'name'}];
|
2020-10-20 11:11:15 +02:00
|
|
|
queryParams = {};
|
2021-05-19 09:39:08 +02:00
|
|
|
paramPosition = 0;
|
2022-01-20 10:19:44 +01:00
|
|
|
@Input() actionRoute: boolean = false;
|
2020-10-20 11:11:15 +02:00
|
|
|
@Input() quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string };
|
2020-11-11 15:43:13 +01:00
|
|
|
sub;
|
2022-01-20 10:19:44 +01:00
|
|
|
public isOpen: boolean = false;
|
2022-03-16 17:54:22 +01:00
|
|
|
sortedValues;
|
2022-01-20 10:19:44 +01:00
|
|
|
constructor(private _router: Router, private route: ActivatedRoute) {
|
|
|
|
}
|
|
|
|
|
2020-11-11 15:43:13 +01:00
|
|
|
ngOnDestroy() {
|
2022-01-20 10:19:44 +01:00
|
|
|
if (this.sub) {
|
2020-11-11 15:43:13 +01:00
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
ngOnInit() {
|
2022-03-16 17:54:22 +01:00
|
|
|
if(this.filterValuesNum == 0){
|
|
|
|
this.isOpen = true;
|
|
|
|
this.sortBy = "num";
|
|
|
|
}else{
|
|
|
|
this.isOpen = false;
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
this.sub = this.route.queryParams.subscribe(params => {
|
|
|
|
this.queryParams = Object.assign({}, params);
|
|
|
|
this.paramPosition = SearchFields.getParameterOrder(this.filter.filterId, this.getEntries(params));
|
|
|
|
});
|
2022-09-30 13:51:00 +02:00
|
|
|
// this.filter.values = this.filter.values.filter(value => !value.name.toLowerCase().includes('unknown') && !value.name.toLowerCase().includes('not available'));
|
|
|
|
// this.sort();
|
|
|
|
// if (this.filter.filterType == "radio") {
|
|
|
|
// this.filter.radioValue = "";
|
|
|
|
// this.filter.values.forEach(value => {
|
|
|
|
// if (value.selected) {
|
|
|
|
// this.filter.radioValue = value.id
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
2022-01-20 10:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if (changes.filter) {
|
2020-05-05 16:09:18 +02:00
|
|
|
this.filter.values = this.filter.values.filter(value => !value.name.toLowerCase().includes('unknown') && !value.name.toLowerCase().includes('not available'));
|
2022-01-20 10:19:44 +01:00
|
|
|
if (this.filter.filterType == "radio") {
|
2020-07-15 16:11:49 +02:00
|
|
|
this.filter.radioValue = "";
|
2020-07-14 17:18:14 +02:00
|
|
|
this.filter.values.forEach(value => {
|
2022-01-20 10:19:44 +01:00
|
|
|
if (value.selected) {
|
2020-07-14 17:18:14 +02:00
|
|
|
this.filter.radioValue = value.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-09-30 13:51:00 +02:00
|
|
|
this.sort();
|
2020-05-05 16:09:18 +02:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
}
|
2020-07-14 14:41:09 +02:00
|
|
|
|
2022-01-20 10:19:44 +01:00
|
|
|
public _formatTitle(title, length) {
|
|
|
|
return (((title + " (" + length + ")").length > this._maxCharacters) ? (title.substring(0, (this._maxCharacters - (" (" + length + ")").length - ('...').length)) + "...") : title) + " (" + (length > 95 ? "100" : length) + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
public _formatName(value) {
|
|
|
|
//let maxLineLength = 24;
|
|
|
|
let maxLineLength = 35;
|
|
|
|
|
|
|
|
//1 space after checkbox
|
|
|
|
//3 space before number + parenthesis
|
|
|
|
if (!this.showResultCount && value.name.length + 1 > maxLineLength) {
|
|
|
|
return value.name.substr(0, maxLineLength - 3 - 1) + '...';
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
if (value.name.length + value.number.toLocaleString().length + 1 + 3 > maxLineLength) {
|
|
|
|
return value.name.substr(0, (maxLineLength - 3 - 4 - value.number.toLocaleString().length)) + '...';
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
return value.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
filterKeywords(value) {
|
|
|
|
if (this.keyword.length > 0) {
|
|
|
|
if (value.toLowerCase().indexOf(this.keyword.toLowerCase()) == -1) {
|
|
|
|
return false;
|
2020-02-24 17:27:01 +01:00
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
filterChange(selected: boolean) {
|
|
|
|
if (selected) {
|
|
|
|
this.filter.countSelectedValues++;
|
|
|
|
} else {
|
|
|
|
this.filter.countSelectedValues--;
|
2019-07-15 18:11:07 +02:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
uniqueFilterChange(value: Value) {
|
|
|
|
let tmp = value.selected;
|
|
|
|
value.selected = !tmp;
|
|
|
|
|
|
|
|
if (value.selected) {
|
|
|
|
this.filter.countSelectedValues = 1;
|
|
|
|
} else {
|
2020-02-17 15:19:14 +01:00
|
|
|
this.filter.countSelectedValues = 0;
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
this.filter.values.forEach(value => {
|
|
|
|
value.selected = (value.id == this.filter.radioValue);
|
|
|
|
});
|
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFilter() {
|
|
|
|
for (var i = 0; i < this.filter.values.length; i++) {
|
|
|
|
this.filter.values[i].selected = false;
|
|
|
|
}
|
|
|
|
this.filter.countSelectedValues = 0;
|
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
|
|
|
this.filter.radioValue = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
getSelectedAndTopValues(filter, topNum: number = 6): any {
|
2020-02-24 17:27:01 +01:00
|
|
|
var values = [];
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
for (let i = 0; i < topNum; i++) {
|
|
|
|
if (filter.values.length <= i) {
|
2020-02-24 17:27:01 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
values.push(filter.values[i]);
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
if (filter.countSelectedValues > 0) {
|
|
|
|
for (let i = topNum; i < filter.values.length; i++) {
|
|
|
|
if (filter.values[i].selected) {
|
2020-02-24 17:27:01 +01:00
|
|
|
values.push(filter.values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
2020-02-24 17:27:01 +01:00
|
|
|
return values;
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
getSelectedValues(filter, sortBy: string = "num"): any {
|
|
|
|
var selected = [];
|
|
|
|
if (filter.countSelectedValues > 0) {
|
|
|
|
for (var i = 0; i < filter.values.length; i++) {
|
|
|
|
if (filter.values[i].selected) {
|
|
|
|
selected.push(filter.values[i]);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
if (sortBy == "name") {
|
|
|
|
|
|
|
|
selected.sort((n1, n2) => {
|
|
|
|
if (n1.name > n2.name) {
|
|
|
|
return 1;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
if (n1.name < n2.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
return selected;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
getNotSelectedValues(filter, sortBy: string = "num"): any {
|
|
|
|
var notSselected = [];
|
|
|
|
for (var i = 0; i < filter.values.length; i++) {
|
|
|
|
if (!filter.values[i].selected) {
|
|
|
|
notSselected.push(filter.values[i]);
|
2020-02-24 17:27:01 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
if (sortBy == "name") {
|
|
|
|
|
|
|
|
notSselected.sort((n1, n2) => {
|
|
|
|
if (n1.name > n2.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (n1.name < n2.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return notSselected;
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:44:59 +02:00
|
|
|
sort() {
|
|
|
|
let sorted: Value[] = this.filter.values.slice();
|
2022-01-20 10:19:44 +01:00
|
|
|
if (this.sortBy == "name") {
|
|
|
|
sorted.sort((n1, n2) => {
|
2022-03-16 14:33:05 +01:00
|
|
|
if (this.filter.filterId === "sdg") {
|
|
|
|
return HelperFunctions.sortSDGs(n1.name.toLowerCase(), n2.name.toLowerCase());
|
|
|
|
} else {
|
|
|
|
if (n1.name.toLowerCase() > n2.name.toLowerCase()) {
|
|
|
|
return 1;
|
2018-07-19 12:52:29 +02:00
|
|
|
}
|
|
|
|
if (n1.name < n2.name) {
|
2022-03-16 14:33:05 +01:00
|
|
|
return -1;
|
2018-07-19 12:52:29 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2022-01-20 10:19:44 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-03-16 17:54:22 +01:00
|
|
|
this.sortedValues = Object.assign(sorted);
|
2022-03-16 18:18:26 +01:00
|
|
|
|
2022-01-20 10:19:44 +01:00
|
|
|
}
|
|
|
|
|
2022-04-17 00:42:10 +02:00
|
|
|
toggle(event) {
|
2020-02-17 15:19:14 +01:00
|
|
|
this.isOpen = !this.isOpen;
|
2022-03-16 17:54:22 +01:00
|
|
|
event.stopPropagation();
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
disabled(value) {
|
|
|
|
return this.isDisabled || (this.showResultCount && value === 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFilterName(value) {
|
|
|
|
let name = value.name + " (" + value.number.format() + ")";
|
2019-07-22 16:15:23 +02:00
|
|
|
return name;
|
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
getRoute() {
|
|
|
|
return this._router.url.split("?")[0];
|
2020-10-20 11:11:15 +02:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
getParams(filter: Filter, value: Value) {
|
|
|
|
let params = Object.assign({}, this.queryParams);
|
|
|
|
/* let qf=false;
|
|
|
|
if(this.quickFilter && this.quickFilter.filterId == filter.filterId && this.quickFilter.selected && value.id == this.quickFilter.value){
|
|
|
|
params['qf']="false";
|
|
|
|
qf=true;
|
|
|
|
}*/
|
|
|
|
if (params[filter.filterId] && this.checkIfValueIndexOf(params[filter.filterId].split(','), value.id) == -1 /*&& !qf*/) {
|
|
|
|
//has other values of this filter --> add this value
|
|
|
|
params[filter.filterId] = params[filter.filterId] + ',' + '"' + encodeURIComponent(value.id) + '"';
|
|
|
|
} else if (params[filter.filterId] && this.checkIfValueIndexOf(params[filter.filterId].split(','), value.id) != -1) {
|
|
|
|
// has this filter and the value -- > remove it
|
|
|
|
|
|
|
|
let values = params[filter.filterId].split(',');
|
|
|
|
values.splice(this.checkIfValueIndexOf(values, value.id), 1);
|
|
|
|
params[filter.filterId] = values.join(',');
|
|
|
|
if (values.length == 0) {
|
|
|
|
delete params[filter.filterId];
|
2020-11-25 18:12:31 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
} else /*if(!qf)*/{
|
|
|
|
//has no filter, no value --> add the value
|
|
|
|
//check the order
|
|
|
|
let keyValues = this.getEntries(params);
|
|
|
|
keyValues.splice(this.paramPosition, 0, [filter.filterId, '"' + encodeURIComponent(value.id) + '"']); // insert key value at paramPosition.
|
|
|
|
params = keyValues.reduce((o, key) => Object.assign(o, {[key[0]]: key[1]}), {});
|
|
|
|
// params[filter.filterId] = '"' + encodeURIComponent(value.id) + '"' ;
|
|
|
|
}
|
|
|
|
delete params['page'];
|
|
|
|
return params;
|
2020-10-20 11:11:15 +02:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
|
|
|
checkIfValueIndexOf(array, value) {
|
|
|
|
let encodedValue = encodeURIComponent(value);
|
|
|
|
if (array.indexOf(encodedValue) != -1) {
|
|
|
|
return array.indexOf(encodedValue);
|
|
|
|
}
|
|
|
|
if (array.indexOf('"' + encodedValue + '"') != -1) {
|
|
|
|
return array.indexOf('"' + encodedValue + '"');
|
|
|
|
}
|
|
|
|
return -1;
|
2020-11-12 10:51:34 +01:00
|
|
|
}
|
2022-01-20 10:19:44 +01:00
|
|
|
|
2021-05-19 09:39:08 +02:00
|
|
|
getEntries(obj) {
|
|
|
|
if (!Object.entries) {
|
2022-01-20 10:19:44 +01:00
|
|
|
|
2021-05-19 09:39:08 +02:00
|
|
|
var ownProps = Object.keys(obj),
|
|
|
|
i = ownProps.length,
|
|
|
|
resArray = new Array(i); // preallocate the Array
|
|
|
|
while (i--)
|
|
|
|
resArray[i] = [ownProps[i], obj[ownProps[i]]];
|
|
|
|
return resArray;
|
2022-01-20 10:19:44 +01:00
|
|
|
} else {
|
2021-05-19 09:39:08 +02:00
|
|
|
return Object.entries(obj);
|
|
|
|
}
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|