2022-04-05 15:45:38 +02:00
|
|
|
import {Component, EventEmitter, Input, Output} from '@angular/core';
|
|
|
|
import {RangeFilter} from './rangeFilterHelperClasses.class';
|
|
|
|
import {Dates, StringUtils} from "../string-utils.class";
|
2020-10-20 11:11:15 +02:00
|
|
|
import {ActivatedRoute, Router} from "@angular/router";
|
|
|
|
import {properties} from "../../../../environments/environment";
|
2022-04-05 15:45:38 +02:00
|
|
|
import {FormBuilder} from "@angular/forms";
|
2020-02-17 15:19:14 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'range-filter',
|
|
|
|
templateUrl: 'rangeFilter.component.html'
|
|
|
|
})
|
|
|
|
|
|
|
|
export class RangeFilterComponent {
|
|
|
|
|
|
|
|
@Input() filter:RangeFilter;
|
|
|
|
@Input() isDisabled:boolean = false;
|
2020-05-14 16:38:50 +02:00
|
|
|
@Input() showQuickButtons:boolean = true;
|
2020-02-17 15:19:14 +01:00
|
|
|
public _maxCharacters:number =28;
|
|
|
|
public focusedInput: string = "";
|
2020-07-10 10:16:58 +02:00
|
|
|
@Input() yearMin: number = Dates.yearMin;
|
|
|
|
@Input() yearMax: number = Dates.yearMax;
|
|
|
|
@Input() mandatoryRange:boolean = false;
|
2020-02-24 14:38:08 +01:00
|
|
|
public currentYear: number = Dates.currentYear;
|
2022-04-05 15:45:38 +02:00
|
|
|
public yearValidators = [StringUtils.inValidYearValidator(this.yearMin, this.yearMax)];
|
|
|
|
public formValidators = [StringUtils.fromYearAfterToYearValidator];
|
|
|
|
public rangeForm;
|
2020-02-17 15:19:14 +01:00
|
|
|
|
|
|
|
@Output() onFilterChange = new EventEmitter();
|
2020-10-20 11:11:15 +02:00
|
|
|
@Input() actionRoute:boolean = false;
|
|
|
|
queryParams = {};
|
2022-04-05 15:45:38 +02:00
|
|
|
constructor(private _router: Router, private route: ActivatedRoute, private _fb: FormBuilder) {}
|
2020-02-17 15:19:14 +01:00
|
|
|
|
2020-10-20 11:11:15 +02:00
|
|
|
ngOnInit() {
|
2022-04-05 15:45:38 +02:00
|
|
|
if(this.mandatoryRange) {
|
|
|
|
this.formValidators.push(StringUtils.rangeRequired(this.mandatoryRange));
|
|
|
|
}
|
|
|
|
this.rangeForm = this._fb.group({
|
|
|
|
yearFrom: [this.filter.selectedFromValue, this.yearValidators],
|
|
|
|
yearTo: [this.filter.selectedToValue, this.yearValidators]
|
|
|
|
}, {validators: this.formValidators});
|
2020-10-20 11:11:15 +02:00
|
|
|
this.route.queryParams.subscribe(params => {
|
|
|
|
this.queryParams = Object.assign({}, params);
|
|
|
|
});
|
|
|
|
}
|
2020-02-17 15:19:14 +01:00
|
|
|
|
|
|
|
public _formatTitle(title){
|
|
|
|
return ((title.length > this._maxCharacters)?(title.substring(0,(this._maxCharacters - ('...').length))+"..."):title);
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:38:08 +01:00
|
|
|
yearChanged(yearsSelected: number = null){
|
|
|
|
if(yearsSelected != null) {
|
|
|
|
this.filter.selectedFromValue = (this.currentYear - yearsSelected) + "";
|
|
|
|
this.filter.selectedToValue = this.currentYear + "";
|
2022-04-05 19:08:11 +02:00
|
|
|
} else {
|
|
|
|
this.filter.selectedFromValue = this.rangeForm.get('yearFrom').value;
|
|
|
|
this.filter.selectedToValue = this.rangeForm.get('yearTo').value;
|
2020-02-24 14:38:08 +01:00
|
|
|
}
|
2020-02-17 15:19:14 +01:00
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
clearFilter() {
|
|
|
|
this.filter.selectedFromValue = null;
|
|
|
|
this.filter.selectedToValue = null;
|
|
|
|
this.onFilterChange.emit({
|
|
|
|
value: this.filter
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getFilterName(value){
|
|
|
|
let name = value.name +" ("+ value.number.format()+")";
|
|
|
|
return name;
|
|
|
|
}
|
2020-03-16 11:06:47 +01:00
|
|
|
|
|
|
|
stringToNum(value: string): number {
|
|
|
|
return +(value);
|
|
|
|
}
|
2020-10-20 11:11:15 +02:00
|
|
|
getFilterUrl(year:number, selected:boolean){
|
|
|
|
return properties.baseLink + (this._router.url.indexOf('year=range') == -1 ? this._router.url: (this._router.url.split("year=range"+this.filter.selectedFromValue+':'+this.filter.selectedToValue).join(''))) + (selected ? '' : (this._router.url.indexOf('?')!=-1?'&':'?') +'year=range'+(this.currentYear - year) + ':'+ this.currentYear);
|
|
|
|
}
|
|
|
|
getRoute(){
|
|
|
|
return properties.baseLink + this._router.url.split("?")[0];
|
|
|
|
}
|
|
|
|
getParams(year:number, selected:boolean){
|
|
|
|
let params = Object.assign({}, this.queryParams);
|
|
|
|
if(!selected){
|
|
|
|
params['year'] = 'range'+(this.currentYear - year) + ':'+ this.currentYear;
|
|
|
|
}else{
|
|
|
|
delete params['year'];
|
|
|
|
}
|
2020-10-20 15:49:53 +02:00
|
|
|
|
|
|
|
delete params['page'];
|
2020-10-20 11:11:15 +02:00
|
|
|
return params;
|
|
|
|
}
|
2020-02-17 15:19:14 +01:00
|
|
|
}
|