2020-02-17 15:19:14 +01:00
|
|
|
import {Component, Input, Output, EventEmitter, ViewChild, ElementRef} from '@angular/core';
|
2017-12-19 13:53:46 +01:00
|
|
|
import { Filter, Value} from './searchHelperClasses.class';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'search-filter',
|
|
|
|
templateUrl: 'searchFilter.component.html'
|
|
|
|
})
|
|
|
|
|
|
|
|
export class SearchFilterComponent {
|
|
|
|
|
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";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-02-17 15:19:14 +01:00
|
|
|
public isOpen:boolean=false;
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2020-05-05 16:09:18 +02:00
|
|
|
constructor () {
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:09:18 +02:00
|
|
|
ngOnInit() {
|
|
|
|
this.filter.values = this.filter.values.filter(value => !value.name.toLowerCase().includes('unknown') && !value.name.toLowerCase().includes('not available'));
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
public _formatTitle(title,length){
|
2020-04-27 10:14:05 +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){
|
|
|
|
this.filter.countSelectedValues++;
|
|
|
|
}else{
|
|
|
|
this.filter.countSelectedValues--;
|
|
|
|
}
|
|
|
|
|
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-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)=> {
|
|
|
|
if (n1.name > n2.name) {
|
|
|
|
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;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|