Project

General

Profile

1
import {Component, Input, ViewChild} from '@angular/core';
2
import { ActivatedRoute} from '@angular/router';
3
import {Location} from '@angular/common';
4
import { Filter, Value} from '../searchUtils/searchHelperClasses.class';
5
import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
6
import {SearchResult}     from '../../utils/entities/searchResult';
7
import {ErrorCodes} from '../../utils/properties/errorCodes';
8
import {SearchFields, FieldDetails} from '../../utils/properties/searchFields';
9
import {SearchPageComponent } from '../searchUtils/searchPage.component';
10
import {SearchUtilsClass } from '../searchUtils/searchUtils.class';
11
import{EnvProperties} from '../../utils/properties/env-properties';
12

    
13
@Component({
14
    selector: 'search-organizations',
15
    template: `
16

    
17
    <search-page pageTitle="Search Organizations"
18
                  formPlaceholderText = "Search for Organizations"
19
                 type="organizations" entityType="organization" [(filters)] = "filters"
20
                 [(results)] = "results"  [(searchUtils)] = "searchUtils"
21
                 [baseUrl] = "baseUrl" (queryChange)="queryChanged($event)"
22
                 [csvParams]="csvParams" csvPath="organizations" advancedSearchLink="/search/advanced/organizations"
23
                 [disableForms]="disableForms"
24
                 [loadPaging]="loadPaging"
25
                 [oldTotalResults]="oldTotalResults"
26
                 [piwikSiteId]=piwikSiteId 
27
                 searchFormClass="organizationsSearchForm">
28
    </search-page>
29

    
30
    `
31

    
32
})
33
export class SearchOrganizationsComponent {
34
  private errorCodes: ErrorCodes;
35
@Input() piwikSiteId = null;
36
  public results =[];
37
  public filters =[];
38
  public baseUrl:string;
39
  public searchUtils:SearchUtilsClass = new SearchUtilsClass();
40
  public sub: any;
41
  public subResults: any;
42
  public searchFields:SearchFields = new SearchFields();
43
  public refineFields: string[] =  this.searchFields.ORGANIZATION_REFINE_FIELDS;
44
  public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
45
  public urlParams : Map<string, string>;
46
  public _location:Location;
47
  public csvParams: string;
48
  public disableForms: boolean = false;
49
  public loadPaging: boolean = true;
50
  public oldTotalResults: number = 0;
51
  pagingLimit = 0;
52
properties: EnvProperties;
53
  @ViewChild (SearchPageComponent) searchPage : SearchPageComponent ;
54

    
55
  constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
56

    
57
    this.errorCodes = new ErrorCodes();
58
    this.searchUtils.status = this.errorCodes.LOADING;
59
    this.searchUtils.page =1;
60

    
61
  }
62

    
63
  public ngOnInit() {
64
    this.route.data
65
      .subscribe((data: { envSpecific: EnvProperties }) => {
66
        this.properties = data.envSpecific;
67
        this.baseUrl = data.envSpecific.searchLinkToOrganizations;
68
        this.pagingLimit = data.envSpecific.pagingLimit;
69

    
70
      });
71
    this.searchPage.refineFields = this.refineFields;
72
    this.searchPage.fieldIdsMap = this.fieldIdsMap;
73
    var firstLoad = true;
74
    this.sub =  this.route.queryParams.subscribe(params => {
75
      if(params['page'] && this.searchUtils.page != params['page']) {
76
        this.loadPaging = false;
77
        this.oldTotalResults = this.searchUtils.totalResults;
78
      }
79

    
80
      this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
81
      var refine = true;
82
      if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
83
        refine = false;
84

    
85
      }
86
      firstLoad = false;
87
      this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
88

    
89
      var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
90
       this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
91
    });
92
  }
93

    
94
  public ngOnDestroy() {
95
    if(this.sub){
96
      this.sub.unsubscribe();
97
    }
98
    if(this.subResults){
99
      this.subResults.unsubscribe();
100
    }
101
  }
102

    
103

    
104
    public getResults(keyword:string,refine:boolean, page: number, size: number){
105
      var parameters = "";
106
      if(keyword.length > 0){
107
        parameters = "q=" + keyword;
108
      }
109
      this._getResults(parameters,refine,page,this.searchUtils.size);
110
    }
111
    private _getResults(parameters:string,refine:boolean, page: number, size: number){
112
      if(page > this.pagingLimit) {
113
        size=0;
114
      }
115
      if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
116
        this.csvParams = parameters;
117

    
118
        this.searchUtils.status = this.errorCodes.LOADING;
119
        //this.searchPage.openLoading();
120
        this.disableForms = true;
121
        this.results = [];
122
        this.searchUtils.totalResults = 0;
123

    
124
        this.subResults = this._searchOrganizationsService.searchOrganizations(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(), this.properties).subscribe(
125
          data => {
126
              this.searchUtils.totalResults = data[0];
127
              console.info("search Organizations: [Parameters:"+parameters+" ]  [total results:"+this.searchUtils.totalResults+"]");
128
              this.results = data[1];
129
              if(refine){
130
                this.filters = data[2];
131
              }
132
              this.searchPage.checkSelectedFilters(this.filters);
133
              this.searchPage.updateBaseUrlWithParameters(this.filters);
134
              //var errorCodes:ErrorCodes = new ErrorCodes();
135
              this.searchUtils.status = this.errorCodes.DONE;
136
              if(this.searchUtils.totalResults == 0 ){
137
                this.searchUtils.status = this.errorCodes.NONE;
138
              }
139
              //this.searchPage.closeLoading();
140
              this.disableForms = false;
141

    
142
              if(this.searchUtils.status == this.errorCodes.DONE) {
143
                // Page out of limit!!!
144
                let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
145
                if(!(Number.isInteger(totalPages))) {
146
                    totalPages = (parseInt(totalPages, 10) + 1);
147
                }
148
                if(totalPages < page) {
149
                  this.searchUtils.totalResults = 0;
150
                  this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
151
                }
152
              }
153
          },
154
          err => {
155
              console.log(err);
156
               //TODO check erros (service not available, bad request)
157
              // if( ){
158
              //   this.searchUtils.status = ErrorCodes.ERROR;
159
              // }
160
              //var errorCodes:ErrorCodes = new ErrorCodes();
161
              //this.searchUtils.status = errorCodes.ERROR;
162
              if(err.status == '404') {
163
                this.searchUtils.status = this.errorCodes.NOT_FOUND;
164
              } else if(err.status == '500') {
165
                this.searchUtils.status = this.errorCodes.ERROR;
166
              } else {
167
                this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
168
              }
169

    
170
              //this.searchPage.closeLoading();
171
              this.disableForms = false;
172
          }
173
        );
174
      }
175
    }
176

    
177

    
178
  public queryChanged($event) {
179
    this.loadPaging = true;
180

    
181
    var parameters = $event.value;
182
    console.info("queryChanged: Execute search query "+parameters);
183
    this._getResults(parameters, true, this.searchUtils.page, this.searchUtils.size);
184
  }
185
}
(5-5/12)