openaire-library/utils/rangeFilter/rangeFilter.component.ts

84 lines
2.7 KiB
TypeScript
Raw Normal View History

import {Component, Input, Output, EventEmitter} from '@angular/core';
import { RangeFilter } from './rangeFilterHelperClasses.class';
import { Dates } from "../string-utils.class";
import {ActivatedRoute, Router} from "@angular/router";
import {Filter, Value} from "../../searchPages/searchUtils/searchHelperClasses.class";
import {properties} from "../../../../environments/environment";
@Component({
selector: 'range-filter',
templateUrl: 'rangeFilter.component.html'
})
export class RangeFilterComponent {
@Input() filter:RangeFilter;
@Input() isDisabled:boolean = false;
@Input() showQuickButtons:boolean = true;
public _maxCharacters:number =28;
public focusedInput: string = "";
@Input() yearMin: number = Dates.yearMin;
@Input() yearMax: number = Dates.yearMax;
@Input() mandatoryRange:boolean = false;
public currentYear: number = Dates.currentYear;
@Output() onFilterChange = new EventEmitter();
@Input() actionRoute:boolean = false;
queryParams = {};
constructor(private _router: Router, private route: ActivatedRoute) {}
ngOnInit() {
this.route.queryParams.subscribe(params => {
this.queryParams = Object.assign({}, params);
});
}
public _formatTitle(title){
return ((title.length > this._maxCharacters)?(title.substring(0,(this._maxCharacters - ('...').length))+"..."):title);
}
yearChanged(yearsSelected: number = null){
if(yearsSelected != null) {
this.filter.selectedFromValue = (this.currentYear - yearsSelected) + "";
this.filter.selectedToValue = this.currentYear + "";
}
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;
}
stringToNum(value: string): number {
return +(value);
}
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'];
}
delete params['page'];
return params;
}
}