1
|
import {Component, Input, ViewChild} from '@angular/core';
|
2
|
import { ActivatedRoute} from '@angular/router';
|
3
|
import {Location} from '@angular/common';
|
4
|
import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
|
5
|
import {ErrorCodes} from '../../utils/properties/errorCodes';
|
6
|
import {ErrorMessagesComponent} from '../../utils/errorMessages.component';
|
7
|
import {SearchFields} from '../../utils/properties/searchFields';
|
8
|
import {SearchPageComponent } from '../searchUtils/searchPage.component';
|
9
|
import {SearchCustomFilter, SearchUtilsClass} from '../searchUtils/searchUtils.class';
|
10
|
import{EnvProperties} from '../../utils/properties/env-properties';
|
11
|
|
12
|
@Component({
|
13
|
selector: 'search-organizations',
|
14
|
template: `
|
15
|
|
16
|
<search-page pageTitle="Search Organizations"
|
17
|
formPlaceholderText = "Search for Organizations"
|
18
|
type="organizations" entityType="organization" [(filters)] = "filters"
|
19
|
[(results)] = "results" [(searchUtils)] = "searchUtils"
|
20
|
[baseUrl] = "baseUrl"
|
21
|
[csvParams]="csvParams" csvPath="organizations" advancedSearchLink="/search/advanced/organizations"
|
22
|
[disableForms]="disableForms"
|
23
|
[loadPaging]="loadPaging"
|
24
|
[oldTotalResults]="oldTotalResults"
|
25
|
[piwikSiteId]=piwikSiteId
|
26
|
[searchFormClass]="customFilter && customFilter.queryFieldName == 'communityId' ?
|
27
|
'communityPanelBackground' : 'organizationsSearchForm'">
|
28
|
</search-page>
|
29
|
|
30
|
`
|
31
|
|
32
|
})
|
33
|
export class SearchOrganizationsComponent {
|
34
|
private errorCodes: ErrorCodes;
|
35
|
private errorMessages: ErrorMessagesComponent;
|
36
|
@Input() piwikSiteId = null;
|
37
|
public results =[];
|
38
|
public filters =[];
|
39
|
public baseUrl:string;
|
40
|
public searchUtils:SearchUtilsClass = new SearchUtilsClass();
|
41
|
public sub: any;
|
42
|
public subResults: any;
|
43
|
public searchFields:SearchFields = new SearchFields();
|
44
|
public refineFields: string[] = this.searchFields.ORGANIZATION_REFINE_FIELDS;
|
45
|
public fieldIdsMap = this.searchFields.ORGANIZATION_FIELDS;
|
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
|
@Input() customFilter:SearchCustomFilter= null;
|
55
|
|
56
|
constructor (private route: ActivatedRoute, private _searchOrganizationsService: SearchOrganizationsService ) {
|
57
|
|
58
|
this.errorCodes = new ErrorCodes();
|
59
|
this.errorMessages = new ErrorMessagesComponent();
|
60
|
this.searchUtils.status = this.errorCodes.LOADING;
|
61
|
this.searchUtils.page =1;
|
62
|
|
63
|
}
|
64
|
|
65
|
public ngOnInit() {
|
66
|
this.route.data
|
67
|
.subscribe((data: { envSpecific: EnvProperties }) => {
|
68
|
this.properties = data.envSpecific;
|
69
|
this.baseUrl = data.envSpecific.searchLinkToOrganizations;
|
70
|
this.pagingLimit = data.envSpecific.pagingLimit;
|
71
|
|
72
|
});
|
73
|
this.searchPage.refineFields = this.refineFields;
|
74
|
this.searchPage.fieldIdsMap = this.fieldIdsMap;
|
75
|
var firstLoad = true;
|
76
|
this.sub = this.route.queryParams.subscribe(params => {
|
77
|
this.loadPaging = true;
|
78
|
if(params['page'] && this.searchUtils.page != params['page']) {
|
79
|
this.loadPaging = false;
|
80
|
this.oldTotalResults = this.searchUtils.totalResults;
|
81
|
}
|
82
|
|
83
|
this.searchUtils.keyword = (params['keyword']?params['keyword']:'');
|
84
|
var refine = true;
|
85
|
if(this.searchUtils.page != ((params['page']=== undefined)?1:+params['page']) && this.filters && !firstLoad){
|
86
|
refine = false;
|
87
|
|
88
|
}
|
89
|
firstLoad = false;
|
90
|
this.searchUtils.page = (params['page']=== undefined)?1:+params['page'];
|
91
|
this.searchUtils.size = (params['size']=== undefined)?10:+params['size'];
|
92
|
if(this.searchUtils.size != 5 && this.searchUtils.size != 10 && this.searchUtils.size != 20 && this.searchUtils.size != 50) {
|
93
|
this.searchUtils.size = 10;
|
94
|
}
|
95
|
|
96
|
this.searchPage.customFilter = this.customFilter;
|
97
|
var queryParameters = this.searchPage.getQueryParametersFromUrl(params);
|
98
|
this._getResults(queryParameters, refine, this.searchUtils.page, this.searchUtils.size);
|
99
|
});
|
100
|
}
|
101
|
|
102
|
public ngOnDestroy() {
|
103
|
if(this.sub){
|
104
|
this.sub.unsubscribe();
|
105
|
}
|
106
|
if(this.subResults){
|
107
|
this.subResults.unsubscribe();
|
108
|
}
|
109
|
}
|
110
|
|
111
|
|
112
|
public getResults(keyword:string,refine:boolean, page: number, size: number){
|
113
|
var parameters = "";
|
114
|
if(keyword.length > 0){
|
115
|
parameters = "q=" + keyword;
|
116
|
}
|
117
|
this._getResults(parameters,refine,page,this.searchUtils.size);
|
118
|
}
|
119
|
private _getResults(parameters:string,refine:boolean, page: number, size: number){
|
120
|
if(page > this.pagingLimit) {
|
121
|
size=0;
|
122
|
}
|
123
|
if(page <= this.pagingLimit || this.searchUtils.status == this.errorCodes.LOADING) {
|
124
|
this.csvParams = parameters;
|
125
|
|
126
|
this.searchUtils.status = this.errorCodes.LOADING;
|
127
|
//this.searchPage.openLoading();
|
128
|
this.disableForms = true;
|
129
|
this.results = [];
|
130
|
this.searchUtils.totalResults = 0;
|
131
|
|
132
|
this.subResults = this._searchOrganizationsService.searchOrganizations(parameters,(refine)?this.searchPage.getRefineFieldsQuery():null, page, size, this.searchPage.getFields(), this.properties).subscribe(
|
133
|
data => {
|
134
|
this.searchUtils.totalResults = data[0];
|
135
|
//console.info("search Organizations: [Parameters:"+parameters+" ] [total results:"+this.searchUtils.totalResults+"]");
|
136
|
this.results = data[1];
|
137
|
if(refine){
|
138
|
this.filters = data[2];
|
139
|
}
|
140
|
this.searchPage.checkSelectedFilters(this.filters);
|
141
|
this.searchPage.updateBaseUrlWithParameters(this.filters);
|
142
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
143
|
this.searchUtils.status = this.errorCodes.DONE;
|
144
|
if(this.searchUtils.totalResults == 0 ){
|
145
|
this.searchUtils.status = this.errorCodes.NONE;
|
146
|
}
|
147
|
//this.searchPage.closeLoading();
|
148
|
this.disableForms = false;
|
149
|
|
150
|
if(this.searchUtils.status == this.errorCodes.DONE) {
|
151
|
// Page out of limit!!!
|
152
|
let totalPages:any = this.searchUtils.totalResults/(this.searchUtils.size);
|
153
|
if(!(Number.isInteger(totalPages))) {
|
154
|
totalPages = (parseInt(totalPages, 10) + 1);
|
155
|
}
|
156
|
if(totalPages < page) {
|
157
|
this.searchUtils.totalResults = 0;
|
158
|
this.searchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
159
|
}
|
160
|
}
|
161
|
},
|
162
|
err => {
|
163
|
//console.log(err);
|
164
|
this.handleError("Error getting organizations", err);
|
165
|
this.searchUtils.status = this.errorMessages.getErrorCode(err.status);
|
166
|
|
167
|
//TODO check erros (service not available, bad request)
|
168
|
// if( ){
|
169
|
// this.searchUtils.status = ErrorCodes.ERROR;
|
170
|
// }
|
171
|
//var errorCodes:ErrorCodes = new ErrorCodes();
|
172
|
//this.searchUtils.status = errorCodes.ERROR;
|
173
|
/*if(err.status == '404') {
|
174
|
this.searchUtils.status = this.errorCodes.NOT_FOUND;
|
175
|
} else if(err.status == '500') {
|
176
|
this.searchUtils.status = this.errorCodes.ERROR;
|
177
|
} else {
|
178
|
this.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
179
|
}*/
|
180
|
|
181
|
//this.searchPage.closeLoading();
|
182
|
this.disableForms = false;
|
183
|
}
|
184
|
);
|
185
|
}
|
186
|
}
|
187
|
|
188
|
|
189
|
|
190
|
private handleError(message: string, error) {
|
191
|
console.error("Organizations simple Search Page: "+message, error);
|
192
|
}
|
193
|
}
|