openaire-library/searchPages/searchUtils/advancedSearchForm.componen...

139 lines
5.0 KiB
TypeScript

import {Component, EventEmitter, Input, Output} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {AdvancedField, Filter} from '../searchUtils/searchHelperClasses.class';
import {SearchFields} from '../../utils/properties/searchFields';
import {Dates} from '../../utils/string-utils.class';
import {EnvProperties} from '../../utils/properties/env-properties';
@Component({
selector: 'advanced-search-form',
templateUrl: 'advancedSearchForm.component.html'
})
export class AdvancedSearchFormComponent {
@Input() entityType;
@Input() fieldIds: string[];
@Input() fieldIdsMap;
@Input() selectedFields:AdvancedField[];
@Input() isDisabled: boolean = false;
@Input() simpleSearchLink;
@Input() advancedSearchLink;
@Input() advancedSearchLinkParameters;
@Input() simpleView:boolean = false;
@Input() formPlaceholderText = "Type Keywords...";
@Input() pageTitle;
@Output() queryChange = new EventEmitter();
@Input() @Output() resultTypes;
@Input() quickFilter:{filter: Filter, selected:boolean, filterId:string, value:string};
validDateFrom: boolean = true;
validDateTo: boolean = true;
newFieldId:string;
newFieldName:string;
fieldList:{[id:string]:any[]} = {};
public searchFields:SearchFields = new SearchFields();
properties:EnvProperties;
public operators: {name:string, id:string}[] = this.searchFields.ADVANCED_SEARCH_OPERATORS;
constructor (private route: ActivatedRoute) {
}
ngOnInit() {
this.route.data
.subscribe((data: { envSpecific: EnvProperties }) => {
this.properties = data.envSpecific;
});
for(var i = 0; i < this.fieldIds.length; i++){
this.fieldList[this.fieldIds[i]]=[];
}
this.newFieldId = this.fieldIds[0];
this.newFieldName = this.fieldIdsMap[this.newFieldId].name;
}
simpleKeywordChanged($event){
this.selectedFields[0].value = $event.value;
this.queryChanged();
}
queryChanged() {
console.log("Q CHanged!")
this.validDateFrom = true;
this.validDateTo = true;
this.selectedFields.forEach(selectedField => {
if(selectedField.type == 'date') {
if (selectedField.dateValue.type.indexOf("range") != -1) {
if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.from))) {
//console.info("INVALID: isValidDate FROM");
this.validDateFrom = false;
}
if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.to))) {
//console.info("INVALID: isValidDate TO");
this.validDateTo = false;
}
}
}
});
if(this.validDateFrom && this.validDateTo) {
this.queryChange.emit({
});
}
}
addField() {
this.newFieldId = this.fieldIds[0];
var type = this.fieldIdsMap[this.newFieldId].type;
if(type == "boolean"){
this.selectedFields.push(new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, type, "true", "and"));
}else{
this.selectedFields.push(new AdvancedField(this.newFieldId, this.fieldIdsMap[this.newFieldId].param,this.fieldIdsMap[this.newFieldId].name, type, "", "and"));
}
}
removeField(index: number) {
if(this.selectedFields.length == 1){
this.selectedFields[index] = new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, this.fieldIdsMap[this.newFieldId].type, "", "and");
}else{
this.selectedFields.splice(index, 1);
}
}
fieldOperatorChanged(index: number, operatorId: string, operatorName: string) {
this.selectedFields[index].operatorId = operatorId;
this.selectedFields[index].operatorName = operatorName;
}
validateDate(index: number, value: string){
this.selectedFields[index].valid = Dates.isValidYear(value);
}
fieldIdsChanged(index: number, fieldId:string ) {
//console.log("Field index::"+index + " " + this.selectedFields[index].id + " function id:" +fieldId);
var id= this.fieldIds[0];
this.selectedFields[index].name = this.fieldIdsMap[id].name;
this.selectedFields[index].type = this.fieldIdsMap[id].type;
this.selectedFields[index].value = "";
this.selectedFields[index].param = this.fieldIdsMap[id].param;
var id =fieldId;//this.selectedFields[index].id;
this.selectedFields[index].name = this.fieldIdsMap[id].name;
this.selectedFields[index].type = this.fieldIdsMap[id].type;
this.selectedFields[index].value = "";
this.selectedFields[index].param = this.fieldIdsMap[id].param;
if(this.fieldIdsMap[id].type == "boolean"){
this.selectedFields[index].value = "true";
}
}
valueChanged($event,index:number){
this.selectedFields[index].value = $event.value;
}
listUpdated($event,fieldId:number){
this.fieldList[fieldId] = $event.value;
}
}