Project

General

Profile

1
import {Component, EventEmitter, Input, Output} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3

    
4
import {AdvancedField} from '../searchUtils/searchHelperClasses.class';
5
import {SearchFields} from '../../utils/properties/searchFields';
6
import {Dates} from '../../utils/string-utils.class';
7
import {EnvProperties} from '../../utils/properties/env-properties';
8

    
9
@Component({
10
    selector: 'advanced-search-form',
11
    templateUrl: 'advancedSearchForm.component.html'
12
})
13
export class AdvancedSearchFormComponent {
14
  @Input() entityType;
15
  @Input() fieldIds:  string[];
16
  @Input() fieldIdsMap;
17
  @Input() selectedFields:AdvancedField[];
18
  @Input() isDisabled: boolean = false;
19
  @Input() simpleSearchLink;
20
  @Input() pageTitle;
21
  @Output() queryChange  = new EventEmitter();
22

    
23
  validDateFrom: boolean = true;
24
  validDateTo: boolean = true;
25

    
26
  newFieldId:string;
27
  newFieldName:string;
28
  fieldList:{[id:string]:any[]} = {};
29
  public searchFields:SearchFields = new SearchFields();
30
properties:EnvProperties;
31
  public operators: {name:string, id:string}[] = this.searchFields.ADVANCED_SEARCH_OPERATORS;
32
    constructor (private route: ActivatedRoute) {
33
     }
34

    
35
    ngOnInit() {
36
      this.route.data
37
        .subscribe((data: { envSpecific: EnvProperties }) => {
38
          this.properties = data.envSpecific;
39

    
40
        });
41
      for(var i = 0; i < this.fieldIds.length; i++){
42
        this.fieldList[this.fieldIds[i]]=[];
43
      }
44
      this.newFieldId = this.fieldIds[0];
45
      this.newFieldName = this.fieldIdsMap[this.newFieldId].name;
46
     }
47

    
48
    queryChanged() {
49
      this.validDateFrom = true;
50
      this.validDateTo = true;
51

    
52
      this.selectedFields.forEach(selectedField => {
53
        if(selectedField.type == 'date') {
54
          if (selectedField.dateValue.type.indexOf("range") != -1) {
55
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.from))) {
56
              //console.info("INVALID: isValidDate FROM");
57
              this.validDateFrom = false;
58
            }
59
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.to))) {
60
              //console.info("INVALID: isValidDate TO");
61
              this.validDateTo = false;
62
            }
63
          }
64
        }
65
      });
66

    
67
      if(this.validDateFrom && this.validDateTo) {
68
        //console.info("emit");
69

    
70
        this.queryChange.emit({
71
          // selectedFields: this.selectedFields,
72
          // selectedQuantifiers: this.selectedQuantifiers,
73
          // keywords: this.keywords
74
        });
75
      }
76
    }
77

    
78
    addField() {
79
        this.newFieldId = this.fieldIds[0];
80
        var type = this.fieldIdsMap[this.newFieldId].type;
81
        if(type == "boolean"){
82
          this.selectedFields.push(new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, type, "true", "and"));
83
        }else{
84
          this.selectedFields.push(new AdvancedField(this.newFieldId, this.fieldIdsMap[this.newFieldId].param,this.fieldIdsMap[this.newFieldId].name, type, "", "and"));
85
        }
86

    
87
    }
88

    
89
    removeField(index: number) {
90
        this.selectedFields.splice(index, 1);
91

    
92
    }
93

    
94
    fieldOperatorChanged(index: number, operatorId: string, operatorName: string) {
95
         this.selectedFields[index].operatorId = operatorId;
96
         this.selectedFields[index].operatorName = operatorName;
97
    }
98
    validateDate(index: number, value: string){
99
      this.selectedFields[index].valid = Dates.isValidYear(value);
100
    }
101

    
102
    fieldIdsChanged(index: number, fieldId:string ) {
103
      //console.log("Field index::"+index + "  " + this.selectedFields[index].id + " function id:" +fieldId);
104

    
105
          var id= this.fieldIds[0];
106
          this.selectedFields[index].name = this.fieldIdsMap[id].name;
107
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
108
         this.selectedFields[index].value = "";
109
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
110

    
111
         var id =fieldId;//this.selectedFields[index].id;
112
         this.selectedFields[index].name = this.fieldIdsMap[id].name;
113
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
114
         this.selectedFields[index].value = "";
115
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
116
         if(this.fieldIdsMap[id].type == "boolean"){
117
           this.selectedFields[index].value = "true";
118
         }
119
    }
120
    valueChanged($event,index:number){
121
      this.selectedFields[index].value = $event.value;
122
    }
123
    listUpdated($event,fieldId:number){
124
      this.fieldList[fieldId] = $event.value;
125
    }
126

    
127
}
(2-2/45)