Project

General

Profile

1 61381 k.triantaf
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
  @Output() queryChange  = new EventEmitter();
28
  @Input()resultTypes;
29
  @Input()  quickFilter:{filter: Filter, selected:boolean, filterId:string, value:string};
30
  validDateFrom: boolean = true;
31
  validDateTo: boolean = true;
32
  @Input() customFilter: SearchCustomFilter;
33
  newFieldId:string;
34
  newFieldName:string;
35
  fieldList:{[id:string]:any[]} = {};
36
  public searchFields:SearchFields = new SearchFields();
37
  properties:EnvProperties;
38
  public operators: {name:string, id:string}[] = this.searchFields.ADVANCED_SEARCH_OPERATORS;
39
  selectedEntity;
40
  selectedEntitySimpleUrl;
41
  selectedEntityAdvancedUrl;
42
  @Input() entitiesSelection:boolean;
43
  @Input() showSwitchSearchLink:boolean = true;
44
  sub;
45
  constructor (private route: ActivatedRoute, private router: Router) {
46
47
  }
48
  ngOnDestroy() {
49
    if (this.sub instanceof Subscriber) {
50
      this.sub.unsubscribe();
51
    }
52
  }
53
    ngOnInit() {
54
    this.selectedEntity = this.entityType;
55
56
          this.properties = properties;
57
58
      for(var i = 0; i < this.fieldIds.length; i++){
59
        this.fieldList[this.fieldIds[i]]=[];
60
      }
61
      this.newFieldId = this.fieldIds[0];
62
      this.newFieldName = this.fieldIdsMap[this.newFieldId].name;
63
     }
64
  simpleEntityChanged($event){
65
    this.selectedEntity = $event.entity;
66
    this.selectedEntitySimpleUrl = $event.simpleUrl;
67
    this.selectedEntityAdvancedUrl = $event.advancedUrl;
68
  }
69
  simpleKeywordChanged($event){
70
    // this.selectedFields[0].value = $event.value;
71
    if(this.selectedEntity == this.entityType) {
72
      this.queryChanged();
73
    }else{
74
      this.router.navigate([this.selectedEntitySimpleUrl],  {queryParams:{q:this.selectedFields[0].value,op:"and"}});
75
    }
76
  }
77
    queryChanged() {
78
      this.validDateFrom = true;
79
      this.validDateTo = true;
80
81
      this.selectedFields.forEach(selectedField => {
82
        if(selectedField.type == 'date') {
83
          if (selectedField.dateValue.type.indexOf("range") != -1) {
84
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.from))) {
85
              //console.info("INVALID: isValidDate FROM");
86
              this.validDateFrom = false;
87
            }
88
            if(!Dates.isValidDate(Dates.getDateToString(selectedField.dateValue.to))) {
89
              //console.info("INVALID: isValidDate TO");
90
              this.validDateTo = false;
91
            }
92
          }
93
        }
94
      });
95
96
      if(this.validDateFrom && this.validDateTo) {
97
98
        this.queryChange.emit({
99
100
        });
101
      }
102
    }
103
104
    addField() {
105
        this.newFieldId = this.fieldIds[0];
106
        var type = this.fieldIdsMap[this.newFieldId].type;
107
        if(type == "boolean"){
108
          this.selectedFields.push(new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, type, "true", "and"));
109
        }else{
110
          this.selectedFields.push(new AdvancedField(this.newFieldId, this.fieldIdsMap[this.newFieldId].param,this.fieldIdsMap[this.newFieldId].name, type, "", "and"));
111
        }
112
113
    }
114
115
    removeField(index: number) {
116
      if(this.selectedFields.length == 1){
117
        this.selectedFields[index] = new AdvancedField(this.newFieldId,this.fieldIdsMap[this.newFieldId].param, this.fieldIdsMap[this.newFieldId].name, this.fieldIdsMap[this.newFieldId].type, "", "and");
118
      }else{
119
        this.selectedFields.splice(index, 1);
120
      }
121
      //if only one filter left, set the operator to and
122
      if(this.selectedFields.length==1){
123
        this.selectedFields[0].operatorId = "and";
124
      }
125
    }
126
127
    fieldOperatorChanged(index: number, operatorId: string, operatorName: string) {
128
         this.selectedFields[index].operatorId = operatorId;
129
         this.selectedFields[index].operatorName = operatorName;
130
    }
131
    validateDate(index: number, value: string){
132
      this.selectedFields[index].valid = Dates.isValidYear(value);
133
    }
134
135
    fieldIdsChanged(index: number, fieldId:string ) {
136
      //console.log("Field index::"+index + "  " + this.selectedFields[index].id + " function id:" +fieldId);
137
138
          var id= this.fieldIds[0];
139
          this.selectedFields[index].name = this.fieldIdsMap[id].name;
140
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
141
         this.selectedFields[index].value = "";
142
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
143
144
         var id =fieldId;//this.selectedFields[index].id;
145
         this.selectedFields[index].name = this.fieldIdsMap[id].name;
146
         this.selectedFields[index].type = this.fieldIdsMap[id].type;
147
         this.selectedFields[index].value = "";
148
         this.selectedFields[index].param = this.fieldIdsMap[id].param;
149
         if(this.fieldIdsMap[id].type == "boolean"){
150
           this.selectedFields[index].value = "true";
151
         }
152
    }
153
    valueChanged($event,index:number){
154
      this.selectedFields[index].value = $event.value;
155
    }
156
    updatedValueLabel($event,index:number){
157
      this.selectedFields[index].valueLabel = $event.value;
158
    }
159
    listUpdated($event,fieldId:number){
160
      this.fieldList[fieldId] = $event.value;
161
    }
162
163
}