Project

General

Profile

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

    
4
import {AdvancedField, Filter} 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() advancedSearchLink;
21
  @Input() advancedSearchLinkParameters;
22
  @Input() simpleView:boolean = false;
23
  @Input() formPlaceholderText = "Type Keywords...";
24
  @Input() pageTitle;
25
  @Output() queryChange  = new EventEmitter();
26
  @Input() @Output() resultTypes;
27
  @Input()  quickFilter:{filter: Filter, selected:boolean, filterId:string, value:string};
28
  validDateFrom: boolean = true;
29
  validDateTo: boolean = true;
30

    
31
  newFieldId:string;
32
  newFieldName:string;
33
  fieldList:{[id:string]:any[]} = {};
34
  public searchFields:SearchFields = new SearchFields();
35
  properties:EnvProperties;
36
  public operators: {name:string, id:string}[] = this.searchFields.ADVANCED_SEARCH_OPERATORS;
37

    
38
  constructor (private route: ActivatedRoute) {
39

    
40
  }
41

    
42
    ngOnInit() {
43
      this.route.data
44
        .subscribe((data: { envSpecific: EnvProperties }) => {
45
          this.properties = data.envSpecific;
46

    
47
        });
48
      for(var i = 0; i < this.fieldIds.length; i++){
49
        this.fieldList[this.fieldIds[i]]=[];
50
      }
51
      this.newFieldId = this.fieldIds[0];
52
      this.newFieldName = this.fieldIdsMap[this.newFieldId].name;
53
     }
54
  simpleKeywordChanged($event){
55
      this.selectedFields[0].value = $event.value;
56
    this.queryChanged();
57
  }
58
    queryChanged() {
59
    console.log("Q CHanged!")
60
      this.validDateFrom = true;
61
      this.validDateTo = true;
62

    
63
      this.selectedFields.forEach(selectedField => {
64
        if(selectedField.type == 'date') {
65
          if (selectedField.dateValue.type.indexOf("range") != -1) {
66
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.from))) {
67
              //console.info("INVALID: isValidDate FROM");
68
              this.validDateFrom = false;
69
            }
70
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.to))) {
71
              //console.info("INVALID: isValidDate TO");
72
              this.validDateTo = false;
73
            }
74
          }
75
        }
76
      });
77

    
78
      if(this.validDateFrom && this.validDateTo) {
79

    
80
        this.queryChange.emit({
81

    
82
        });
83
      }
84
    }
85

    
86
    addField() {
87
        this.newFieldId = this.fieldIds[0];
88
        var type = this.fieldIdsMap[this.newFieldId].type;
89
        if(type == "boolean"){
90
          this.selectedFields.push(new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, type, "true", "and"));
91
        }else{
92
          this.selectedFields.push(new AdvancedField(this.newFieldId, this.fieldIdsMap[this.newFieldId].param,this.fieldIdsMap[this.newFieldId].name, type, "", "and"));
93
        }
94

    
95
    }
96

    
97
    removeField(index: number) {
98
      if(this.selectedFields.length == 1){
99
        this.selectedFields[index] = new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, this.fieldIdsMap[this.newFieldId].type, "", "and");
100
      }else{
101
        this.selectedFields.splice(index, 1);
102
      }
103
    }
104

    
105
    fieldOperatorChanged(index: number, operatorId: string, operatorName: string) {
106
         this.selectedFields[index].operatorId = operatorId;
107
         this.selectedFields[index].operatorName = operatorName;
108
    }
109
    validateDate(index: number, value: string){
110
      this.selectedFields[index].valid = Dates.isValidYear(value);
111
    }
112

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

    
116
          var id= this.fieldIds[0];
117
          this.selectedFields[index].name = this.fieldIdsMap[id].name;
118
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
119
         this.selectedFields[index].value = "";
120
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
121

    
122
         var id =fieldId;//this.selectedFields[index].id;
123
         this.selectedFields[index].name = this.fieldIdsMap[id].name;
124
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
125
         this.selectedFields[index].value = "";
126
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
127
         if(this.fieldIdsMap[id].type == "boolean"){
128
           this.selectedFields[index].value = "true";
129
         }
130
    }
131
    valueChanged($event,index:number){
132
      this.selectedFields[index].value = $event.value;
133
    }
134
    listUpdated($event,fieldId:number){
135
      this.fieldList[fieldId] = $event.value;
136
    }
137

    
138
}
(2-2/51)