Project

General

Profile

1
import {Component, EventEmitter, Input, Output} from '@angular/core';
2
import {ActivatedRoute, Router} 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
import {SearchCustomFilter} from "./searchUtils.class";
9
import {Subscriber} from "rxjs";
10
import {properties} from "../../../../environments/environment";
11

    
12
@Component({
13
    selector: 'advanced-search-form',
14
    templateUrl: 'advancedSearchForm.component.html'
15
})
16
export class AdvancedSearchFormComponent {
17
  @Input() entityType;
18
  @Input() fieldIds:  string[];
19
  @Input() fieldIdsMap;
20
  @Input() selectedFields:AdvancedField[];
21
  @Input() isDisabled: boolean = false;
22
  @Input() simpleSearchLink;
23
  @Input() advancedSearchLink;
24
  @Input() advancedSearchLinkParameters;
25
  @Input() simpleView:boolean = false;
26
  @Input() formPlaceholderText = "Type Keywords...";
27
  @Input() pageTitle;
28
  @Output() queryChange  = new EventEmitter();
29
  @Input()resultTypes;
30
  @Input()  quickFilter:{filter: Filter, selected:boolean, filterId:string, value:string};
31
  validDateFrom: boolean = true;
32
  validDateTo: boolean = true;
33
  @Input() customFilter: SearchCustomFilter;
34
  newFieldId:string;
35
  newFieldName:string;
36
  fieldList:{[id:string]:any[]} = {};
37
  public searchFields:SearchFields = new SearchFields();
38
  properties:EnvProperties;
39
  public operators: {name:string, id:string}[] = this.searchFields.ADVANCED_SEARCH_OPERATORS;
40
  selectedEntity;
41
  selectedEntitySimpleUrl;
42
  selectedEntityAdvancedUrl;
43
  @Input() entitiesSelection:boolean;
44
  @Input() showSwitchSearchLink:boolean = true;
45
  sub;
46
  constructor (private route: ActivatedRoute, private router: Router) {
47

    
48
  }
49
  ngOnDestroy() {
50
    if (this.sub instanceof Subscriber) {
51
      this.sub.unsubscribe();
52
    }
53
  }
54
    ngOnInit() {
55
    this.selectedEntity = this.entityType;
56

    
57
          this.properties = properties;
58

    
59
      for(var i = 0; i < this.fieldIds.length; i++){
60
        this.fieldList[this.fieldIds[i]]=[];
61
      }
62
      this.newFieldId = this.fieldIds[0];
63
      this.newFieldName = this.fieldIdsMap[this.newFieldId].name;
64
     }
65
  simpleEntityChanged($event){
66
    this.selectedEntity = $event.entity;
67
    this.selectedEntitySimpleUrl = $event.simpleUrl;
68
    this.selectedEntityAdvancedUrl = $event.advancedUrl;
69
  }
70
  simpleKeywordChanged($event){
71
    // this.selectedFields[0].value = $event.value;
72
    if(this.selectedEntity == this.entityType) {
73
      this.queryChanged();
74
    }else{
75
      this.router.navigate([this.selectedEntitySimpleUrl],  {queryParams:{q:this.selectedFields[0].value,op:"and"}});
76
    }
77
  }
78
    queryChanged() {
79
      this.validDateFrom = true;
80
      this.validDateTo = true;
81

    
82
      this.selectedFields.forEach(selectedField => {
83
        if(selectedField.type == 'date') {
84
          if (selectedField.dateValue.type.indexOf("range") != -1) {
85
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.from))) {
86
              //console.info("INVALID: isValidDate FROM");
87
              this.validDateFrom = false;
88
            }
89
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.to))) {
90
              //console.info("INVALID: isValidDate TO");
91
              this.validDateTo = false;
92
            }
93
          }
94
        }
95
      });
96

    
97
      if(this.validDateFrom && this.validDateTo) {
98

    
99
        this.queryChange.emit({
100

    
101
        });
102
      }
103
    }
104

    
105
    addField() {
106
        this.newFieldId = this.fieldIds[0];
107
        var type = this.fieldIdsMap[this.newFieldId].type;
108
        if(type == "boolean"){
109
          this.selectedFields.push(new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, type, "true", "and"));
110
        }else{
111
          this.selectedFields.push(new AdvancedField(this.newFieldId, this.fieldIdsMap[this.newFieldId].param,this.fieldIdsMap[this.newFieldId].name, type, "", "and"));
112
        }
113

    
114
    }
115

    
116
    removeField(index: number) {
117
      if(this.selectedFields.length == 1){
118
        this.selectedFields[index] = new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, this.fieldIdsMap[this.newFieldId].type, "", "and");
119
      }else{
120
        this.selectedFields.splice(index, 1);
121
      }
122
      //if only one filter left, set the operator to and
123
      if(this.selectedFields.length==1){
124
        this.selectedFields[0].operatorId = "and";
125
      }
126
    }
127

    
128
    fieldOperatorChanged(index: number, operatorId: string, operatorName: string) {
129
         this.selectedFields[index].operatorId = operatorId;
130
         this.selectedFields[index].operatorName = operatorName;
131
    }
132
    validateDate(index: number, value: string){
133
      this.selectedFields[index].valid = Dates.isValidYear(value);
134
    }
135

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

    
139
          var id= this.fieldIds[0];
140
          this.selectedFields[index].name = this.fieldIdsMap[id].name;
141
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
142
         this.selectedFields[index].value = "";
143
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
144

    
145
         var id =fieldId;//this.selectedFields[index].id;
146
         this.selectedFields[index].name = this.fieldIdsMap[id].name;
147
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
148
         this.selectedFields[index].value = "";
149
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
150
         if(this.fieldIdsMap[id].type == "boolean"){
151
           this.selectedFields[index].value = "true";
152
         }
153
    }
154
    valueChanged($event,index:number){
155
      this.selectedFields[index].value = $event.value;
156
    }
157
    updatedValueLabel($event,index:number){
158
      this.selectedFields[index].valueLabel = $event.value;
159
    }
160
    listUpdated($event,fieldId:number){
161
      this.fieldList[fieldId] = $event.value;
162
    }
163

    
164
}
(2-2/47)