2020-07-14 14:41:09 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
Input,
|
|
|
|
Output,
|
|
|
|
EventEmitter,
|
|
|
|
ViewChild,
|
|
|
|
ElementRef,
|
|
|
|
OnInit,
|
|
|
|
OnChanges,
|
|
|
|
SimpleChanges
|
|
|
|
} from '@angular/core';
|
2017-12-19 13:53:46 +01:00
|
|
|
import { Filter, Value} from './searchHelperClasses.class';
|
2020-10-20 11:11:15 +02:00
|
|
|
import {ActivatedRoute, NavigationStart, Router} from "@angular/router";
|
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
import 'rxjs/add/operator/filter';
|
2017-12-19 13:53:46 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'search-filter',
|
|
|
|
templateUrl: 'searchFilter.component.html'
|
|
|
|
})
|
|
|
|
|
2020-07-14 14:41:09 +02:00
|
|
|
export class SearchFilterComponent implements OnInit, OnChanges{
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
@Input() filter:Filter;
|
|
|
|
@Input() showResultCount:boolean = true;
|
|
|
|
@Input() isDisabled:boolean = false;
|
|
|
|
@Input() addShowMore:boolean = true;
|
|
|
|
@Input() showMoreInline: boolean = true;
|
2020-04-28 12:07:53 +02:00
|
|
|
@Input() filterValuesNum: number = 6;
|
2020-02-17 15:19:14 +01:00
|
|
|
public showAll:boolean = false;
|
|
|
|
public _maxCharacters:number =28;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
@Output() toggleModal = new EventEmitter();
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
@Output() modalChange = new EventEmitter();
|
|
|
|
@Output() onFilterChange = new EventEmitter();
|
|
|
|
keyword = "";
|
2020-02-24 17:27:01 +01:00
|
|
|
sortBy = "name";
|
2020-10-20 11:11:15 +02:00
|
|
|
queryParams = {};
|
|
|
|
@Input() actionRoute:boolean = false;
|
|
|
|
@Input() quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string };
|
2020-02-17 15:19:14 +01:00
|
|
|
public isOpen:boolean=false;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-10-20 11:11:15 +02:00
|
|
|
constructor (private _router: Router, private route: ActivatedRoute) {
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:09:18 +02:00
|
|
|
ngOnInit() {
|
2020-10-20 11:11:15 +02:00
|
|
|
this.route.queryParams.subscribe(params => {
|
|
|
|
this.queryParams = Object.assign({}, params);
|
|
|
|
});
|
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'));
|
2020-07-14 17:18:14 +02: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 => {
|
|
|
|
if(value.selected){
|
|
|
|
this.filter.radioValue = value.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-05-05 16:09:18 +02:00
|
|
|
}
|
2020-07-14 14:41:09 +02:00
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if(changes.filter) {
|
|
|
|
this.filter.values = this.filter.values.filter(value => !value.name.toLowerCase().includes('unknown') && !value.name.toLowerCase().includes('not available'));
|
2020-07-15 16:11:49 +02:00
|
|
|
if(this.filter.filterType == "radio"){
|
|
|
|
this.filter.radioValue = "";
|
|
|
|
this.filter.values.forEach(value => {
|
|
|
|
if(value.selected){
|
|
|
|
this.filter.radioValue = value.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-14 14:41:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public _formatTitle(title,length){
|
2020-07-28 17:55:59 +02:00
|
|
|
return (((title+" ("+length+")").length >this._maxCharacters)?(title.substring(0,(this._maxCharacters - (" ("+length+")").length - ('...').length))+"..."):title)+" ("+(length>95?"100":length)+")";
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-22 16:15:23 +02:00
|
|
|
public _formatName(value){
|
2020-02-17 15:19:14 +01:00
|
|
|
//let maxLineLength = 24;
|
|
|
|
let maxLineLength = 35;
|
|
|
|
|
2019-07-22 16:15:23 +02:00
|
|
|
//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)+'...';
|
|
|
|
}
|
|
|
|
if(value.name.length+value.number.toLocaleString().length +1 +3> maxLineLength){
|
|
|
|
return value.name.substr(0, (maxLineLength- 3 -4 - value.number.toLocaleString().length))+'...';
|
|
|
|
}
|
|
|
|
|
|
|
|
return value.name;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-07-19 12:52:29 +02:00
|
|
|
|
|
|
|
filterKeywords(value){
|
|
|
|
if(this.keyword.length > 0){
|
|
|
|
if(value.toLowerCase().indexOf(this.keyword.toLowerCase()) ==-1){
|
|
|
|
return false;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-07-19 12:52:29 +02:00
|
|
|
}
|
|
|
|
return true;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
filterChange(selected:boolean){
|
2020-02-24 17:27:01 +01:00
|
|
|
if(selected){
|
|
|
|
this.filter.countSelectedValues++;
|
|
|
|
}else{
|
|
|
|
this.filter.countSelectedValues--;
|
|
|
|
}
|
2020-02-25 13:36:30 +01:00
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-07-15 18:11:07 +02:00
|
|
|
uniqueFilterChange(value: Value) {
|
|
|
|
let tmp = value.selected;
|
|
|
|
value.selected = !tmp;
|
|
|
|
|
|
|
|
if(value.selected){
|
2020-08-06 09:24:44 +02:00
|
|
|
this.filter.countSelectedValues = 1;
|
2019-07-15 18:11:07 +02:00
|
|
|
}else{
|
2020-08-06 09:24:44 +02:00
|
|
|
this.filter.countSelectedValues = 0;
|
2019-07-15 18:11:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:18:14 +02:00
|
|
|
this.filter.values.forEach(value => {
|
|
|
|
value.selected = (value.id == this.filter.radioValue);
|
|
|
|
});
|
2020-02-25 13:36:30 +01:00
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
2019-07-15 18:11:07 +02:00
|
|
|
}
|
2020-02-17 15:19:14 +01:00
|
|
|
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
|
|
|
|
});
|
2020-07-14 17:18:14 +02:00
|
|
|
this.filter.radioValue = "";
|
2020-02-17 15:19:14 +01:00
|
|
|
}
|
2020-02-24 17:27:01 +01:00
|
|
|
|
|
|
|
getSelectedAndTopValues(filter, topNum: number = 6):any{
|
|
|
|
var values = [];
|
|
|
|
|
|
|
|
for (let i=0; i < topNum; i++){
|
|
|
|
if(filter.values.length <= i) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
values.push(filter.values[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(filter.countSelectedValues >0){
|
|
|
|
for (let i=topNum; i < filter.values.length; i++){
|
|
|
|
if(filter.values[i].selected){
|
|
|
|
values.push(filter.values[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:52:29 +02:00
|
|
|
getSelectedValues(filter, sortBy:string = "num"):any{
|
2017-12-19 13:53:46 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-19 12:52:29 +02:00
|
|
|
if(sortBy == "name"){
|
|
|
|
|
|
|
|
selected.sort((n1,n2)=> {
|
|
|
|
if (n1.name > n2.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n1.name < n2.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
return selected;
|
|
|
|
|
|
|
|
}
|
2018-07-25 15:20:54 +02:00
|
|
|
getNotSelectedValues(filter, sortBy:string = "num"):any{
|
2017-12-19 13:53:46 +01:00
|
|
|
var notSselected = [];
|
2020-02-17 15:19:14 +01:00
|
|
|
for (var i=0; i < filter.values.length; i++){
|
|
|
|
if(!filter.values[i].selected){
|
|
|
|
notSselected.push(filter.values[i]);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2020-02-17 15:19:14 +01:00
|
|
|
}
|
2018-07-19 12:52:29 +02:00
|
|
|
|
|
|
|
if(sortBy == "name"){
|
|
|
|
|
|
|
|
notSselected.sort((n1,n2)=> {
|
|
|
|
if (n1.name > n2.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (n1.name < n2.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
return notSselected;
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:27:01 +01:00
|
|
|
sort(values: Value[]) {
|
|
|
|
let sorted: Value[] = values.slice();
|
|
|
|
if(this.sortBy == "name"){
|
|
|
|
|
|
|
|
sorted.sort((n1,n2)=> {
|
2020-09-15 10:43:20 +02:00
|
|
|
if (n1.name.toLowerCase() > n2.name.toLowerCase()) {
|
2020-02-24 17:27:01 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n1.name < n2.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
toggle() {
|
|
|
|
this.isOpen = !this.isOpen;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:15:23 +02:00
|
|
|
getFilterName(value){
|
|
|
|
let name = value.name +" ("+ value.number.format()+")";
|
2020-02-17 15:19:14 +01:00
|
|
|
console.log(name);
|
2019-07-22 16:15:23 +02:00
|
|
|
return name;
|
|
|
|
}
|
2020-10-20 11:11:15 +02:00
|
|
|
getRoute(){
|
|
|
|
return properties.baseLink + this._router.url.split("?")[0];
|
|
|
|
}
|
|
|
|
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] && params[filter.filterId].indexOf(encodeURIComponent(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] && params[filter.filterId].indexOf(encodeURIComponent(value.id))!=-1) {
|
|
|
|
// has this filter and the value -- > remove it
|
|
|
|
|
|
|
|
let values = params[filter.filterId].split(',');
|
|
|
|
values.splice(values.indexOf('"' + encodeURIComponent(value.id)+'"'), 1);
|
|
|
|
params[filter.filterId] =values.join(',');
|
|
|
|
if(values.length == 0){
|
|
|
|
delete params[filter.filterId];
|
|
|
|
}
|
|
|
|
} else if(!qf){
|
|
|
|
//has no filter, no value --> add the value
|
|
|
|
params[filter.filterId] = '"' + encodeURIComponent(value.id) + '"' ;
|
|
|
|
}
|
2020-10-20 15:49:53 +02:00
|
|
|
delete params['page'];
|
2020-10-20 11:11:15 +02:00
|
|
|
return params;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|