Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

    
88
    }
89

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

    
93
    }
94

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

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

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

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

    
128
}
(2-2/45)