1
|
import {Component, OnInit, Input, Output, EventEmitter, ViewChild} from '@angular/core';
|
2
|
import { ActivatedRoute, Router } from "@angular/router";
|
3
|
import {SearchResult} from '../../openaireLibrary/utils/entities/searchResult';
|
4
|
import {ErrorCodes} from '../../openaireLibrary/utils/properties/errorCodes';
|
5
|
import {SearchUtilsClass } from '../../openaireLibrary/searchPages/searchUtils/searchUtils.class';
|
6
|
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
|
7
|
import {SearchDataprovidersService} from '../../openaireLibrary/services/searchDataproviders.service';
|
8
|
import {RouterHelper} from '../../openaireLibrary/utils/routerHelper.class';
|
9
|
import {StringUtils} from '../../openaireLibrary/utils/string-utils.class';
|
10
|
import {ManageCommunityContentProvidersService} from '../../services/manageContentProviders.service';
|
11
|
import {Session} from '../../openaireLibrary/login/utils/helper.class';
|
12
|
import {LoginErrorCodes} from '../../openaireLibrary/login/utils/guardHelper.class';
|
13
|
import {properties} from "../../../environments/environment";
|
14
|
import {FormBuilder, FormGroup} from "@angular/forms";
|
15
|
import {SearchInputComponent} from "../../openaireLibrary/sharedComponents/search-input/search-input.component";
|
16
|
import {Subscriber} from "rxjs";
|
17
|
import {debounceTime, distinctUntilChanged} from "rxjs/operators";
|
18
|
import {ResultPreview} from "../../openaireLibrary/utils/result-preview/result-preview";
|
19
|
|
20
|
declare var UIkit;
|
21
|
|
22
|
@Component({
|
23
|
selector: 'add-content-providers',
|
24
|
templateUrl: './add-content-providers.component.html',
|
25
|
})
|
26
|
|
27
|
export class AddContentProvidersComponent implements OnInit {
|
28
|
private subscriptions: any[] = [];
|
29
|
public subResults: any;
|
30
|
|
31
|
private community: string = '';
|
32
|
|
33
|
public routerHelper: RouterHelper = new RouterHelper();
|
34
|
public properties: EnvProperties = properties;
|
35
|
public errorCodes: ErrorCodes;
|
36
|
public openaireSearchUtils: SearchUtilsClass = new SearchUtilsClass();
|
37
|
@Output() communityContentProvidersChanged = new EventEmitter();
|
38
|
@Input() communityContentProviders = [];
|
39
|
public openaireContentProviders = [];
|
40
|
public queryParameters: string = "";
|
41
|
|
42
|
// public pagingLimit: number = properties.pagingLimit;
|
43
|
public resultsPerPage: number = properties.resultsPerPage;
|
44
|
|
45
|
filterForm: FormGroup;
|
46
|
@ViewChild('searchInputComponent') searchInputComponent: SearchInputComponent;
|
47
|
|
48
|
private contentProviderUrl: string = "https://" + ((properties.environment == "beta" || properties.environment == "development") ? "beta." : "") + "explore.openaire.eu" + properties.searchLinkToDataProvider;
|
49
|
public body: string = "Send from page";
|
50
|
|
51
|
@Output() toggleView: EventEmitter<any> = new EventEmitter();
|
52
|
|
53
|
constructor(private route: ActivatedRoute, private _router: Router,
|
54
|
private _searchContentProvidersService: SearchDataprovidersService,
|
55
|
private _manageCommunityContentProvidersService: ManageCommunityContentProvidersService,
|
56
|
private _fb: FormBuilder) {
|
57
|
this.errorCodes = new ErrorCodes();
|
58
|
this.openaireSearchUtils.status = this.errorCodes.LOADING;
|
59
|
}
|
60
|
|
61
|
ngOnInit() {
|
62
|
this.subscriptions.push(this.route.params.subscribe(params => {
|
63
|
this.openaireSearchUtils.status = this.errorCodes.LOADING;
|
64
|
|
65
|
this.community = params['community'];
|
66
|
// this.contentProviderUrl = "https://" + ((this.properties.environment == "beta" || this.properties.environment == "development") ? "beta." : "")
|
67
|
// + this.community + ".openaire.eu" + this.properties.searchLinkToDataProvider;
|
68
|
|
69
|
this._getOpenaireContentProviders("", 1, this.resultsPerPage);
|
70
|
|
71
|
this.body = "[Please write your message here]";
|
72
|
this.body = StringUtils.URIEncode(this.body);
|
73
|
}));
|
74
|
|
75
|
this.openaireSearchUtils.keyword = "";
|
76
|
|
77
|
this.filterForm = this._fb.group({
|
78
|
keyword: [''],
|
79
|
});
|
80
|
|
81
|
this.subscriptions.push(this.filterForm.get('keyword').valueChanges
|
82
|
.pipe(debounceTime(1000), distinctUntilChanged())
|
83
|
.subscribe(value => {
|
84
|
this.keywordChanged(value);
|
85
|
})
|
86
|
);
|
87
|
}
|
88
|
|
89
|
public ngOnDestroy() {
|
90
|
this.subscriptions.forEach(sub => {
|
91
|
if (sub instanceof Subscriber) {
|
92
|
sub.unsubscribe();
|
93
|
}
|
94
|
});
|
95
|
|
96
|
if(this.subResults){
|
97
|
this.subResults.unsubscribe();
|
98
|
}
|
99
|
}
|
100
|
|
101
|
public addContentProvider(contenProvider: SearchResult) {
|
102
|
if (!Session.isLoggedIn()) {
|
103
|
this._router.navigate(['/user-info'], {
|
104
|
queryParams: {
|
105
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
106
|
"redirectUrl": this._router.url
|
107
|
}
|
108
|
});
|
109
|
} else {
|
110
|
this.subscriptions.push(this._manageCommunityContentProvidersService.addContentProvider(this.properties, this.community, contenProvider).subscribe(
|
111
|
data => {
|
112
|
this.communityContentProviders.push(data);
|
113
|
UIkit.notification('Content Provider successfully added!', {
|
114
|
status: 'success',
|
115
|
timeout: 6000,
|
116
|
pos: 'bottom-right'
|
117
|
});
|
118
|
this.communityContentProvidersChanged.emit({
|
119
|
value: this.communityContentProviders,
|
120
|
});
|
121
|
},
|
122
|
err => {
|
123
|
this.handleError('An error has been occurred. Try again later!');
|
124
|
console.error(err.status);
|
125
|
}
|
126
|
));
|
127
|
}
|
128
|
}
|
129
|
|
130
|
public removeContentProvider(contentProvider) {
|
131
|
if (!Session.isLoggedIn()) {
|
132
|
this._router.navigate(['/user-info'], {
|
133
|
queryParams: {
|
134
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
135
|
"redirectUrl": this._router.url
|
136
|
}
|
137
|
});
|
138
|
} else {
|
139
|
let communityContentProvider = this.getCommunityContentProvider(contentProvider);
|
140
|
let contentProviderId: string = communityContentProvider['id'];
|
141
|
this.subscriptions.push(this._manageCommunityContentProvidersService.removeContentProvider(this.properties, this.community, contentProviderId).subscribe(
|
142
|
data => {
|
143
|
let index = this.communityContentProviders.indexOf(communityContentProvider);
|
144
|
this.communityContentProviders.splice(index, 1);
|
145
|
UIkit.notification('Content Provider successfully removed!', {
|
146
|
status: 'success',
|
147
|
timeout: 6000,
|
148
|
pos: 'bottom-right'
|
149
|
});
|
150
|
this.communityContentProvidersChanged.emit({
|
151
|
value: this.communityContentProviders,
|
152
|
});
|
153
|
},
|
154
|
err => {
|
155
|
this.handleError('An error has been occurred. Try again later!');
|
156
|
console.error(err);
|
157
|
}
|
158
|
));
|
159
|
}
|
160
|
}
|
161
|
|
162
|
public getCommunityContentProvider(contentProvider: any): string {
|
163
|
let index: number = 0;
|
164
|
for (let communityContentProvider of this.communityContentProviders) {
|
165
|
if (communityContentProvider.openaireId == contentProvider.id) {
|
166
|
return communityContentProvider;
|
167
|
}
|
168
|
index++;
|
169
|
}
|
170
|
return "";
|
171
|
}
|
172
|
|
173
|
public getResultPreview(result: SearchResult): ResultPreview {
|
174
|
return ResultPreview.searchResultConvert(result, "dataprovider");
|
175
|
}
|
176
|
|
177
|
// public inCommunity(contentProvider: any): any {
|
178
|
// for(let communityContentProvider of this.communityContentProviders) {
|
179
|
// if(communityContentProvider.openaireId == contentProvider.id) {
|
180
|
// return true;
|
181
|
// }
|
182
|
// }
|
183
|
//
|
184
|
// if(this.undo[contentProvider.id]) {
|
185
|
// return true;
|
186
|
// }
|
187
|
// return false;
|
188
|
// }
|
189
|
|
190
|
private _getOpenaireContentProviders(parameters: string, page: number, size: number) {
|
191
|
if (!Session.isLoggedIn()) {
|
192
|
this._router.navigate(['/user-info'], {
|
193
|
queryParams: {
|
194
|
"errorCode": LoginErrorCodes.NOT_VALID,
|
195
|
"redirectUrl": this._router.url
|
196
|
}
|
197
|
});
|
198
|
} else {
|
199
|
// if (page > this.pagingLimit) {
|
200
|
// size = 0;
|
201
|
// }
|
202
|
|
203
|
if (this.openaireSearchUtils.status == this.errorCodes.LOADING) {
|
204
|
this.openaireSearchUtils.status = this.errorCodes.LOADING;
|
205
|
|
206
|
this.openaireContentProviders = [];
|
207
|
this.openaireSearchUtils.totalResults = 0;
|
208
|
|
209
|
if(this.subResults){
|
210
|
this.subResults.unsubscribe();
|
211
|
}
|
212
|
this.subResults = this._searchContentProvidersService.searchDataproviders(parameters, null, page, size, [], this.properties).subscribe(
|
213
|
data => {
|
214
|
this.openaireSearchUtils.totalResults = data[0];
|
215
|
this.openaireContentProviders = data[1];
|
216
|
|
217
|
//this.searchPage.checkSelectedFilters(this.filters);
|
218
|
this.openaireSearchUtils.status = this.errorCodes.DONE;
|
219
|
if (this.openaireSearchUtils.totalResults == 0) {
|
220
|
this.openaireSearchUtils.status = this.errorCodes.NONE;
|
221
|
}
|
222
|
|
223
|
// if (this.openaireSearchUtils.status == this.errorCodes.DONE) {
|
224
|
// // Page out of limit!!!
|
225
|
// let totalPages: any = this.openaireSearchUtils.totalResults / (this.openaireSearchUtils.size);
|
226
|
// if (!(Number.isInteger(totalPages))) {
|
227
|
// totalPages = (parseInt(totalPages, 10) + 1);
|
228
|
// }
|
229
|
// if (totalPages < page) {
|
230
|
// this.openaireSearchUtils.totalResults = 0;
|
231
|
// this.openaireSearchUtils.status = this.errorCodes.OUT_OF_BOUND;
|
232
|
// }
|
233
|
// }
|
234
|
},
|
235
|
err => {
|
236
|
console.error(err);
|
237
|
//TODO check erros (service not available, bad request)
|
238
|
if (err.status == '404') {
|
239
|
this.openaireSearchUtils.status = this.errorCodes.NOT_FOUND;
|
240
|
} else if (err.status == '500') {
|
241
|
this.openaireSearchUtils.status = this.errorCodes.ERROR;
|
242
|
} else {
|
243
|
this.openaireSearchUtils.status = this.errorCodes.NOT_AVAILABLE;
|
244
|
}
|
245
|
}
|
246
|
);
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
|
251
|
totalPages(): number {
|
252
|
let totalPages: any = this.openaireSearchUtils.totalResults / (this.resultsPerPage);
|
253
|
if (!(Number.isInteger(totalPages))) {
|
254
|
totalPages = (parseInt(totalPages, 10) + 1);
|
255
|
}
|
256
|
return totalPages;
|
257
|
}
|
258
|
|
259
|
keywordChanged(keyword) {
|
260
|
this.openaireSearchUtils.keyword = keyword;
|
261
|
this.buildQueryParameters();
|
262
|
this.goTo(1);
|
263
|
}
|
264
|
|
265
|
buildQueryParameters() {
|
266
|
this.queryParameters = "";
|
267
|
|
268
|
if (this.openaireSearchUtils.keyword) {
|
269
|
this.queryParameters = "q=" + StringUtils.URIEncode(this.openaireSearchUtils.keyword);
|
270
|
}
|
271
|
}
|
272
|
|
273
|
goTo(page: number = 1) {
|
274
|
this.openaireSearchUtils.page = page;
|
275
|
this.openaireSearchUtils.status = this.errorCodes.LOADING;
|
276
|
this._getOpenaireContentProviders(this.queryParameters, page, this.resultsPerPage);
|
277
|
}
|
278
|
|
279
|
back() {
|
280
|
this.toggleView.emit(null);
|
281
|
}
|
282
|
|
283
|
public onSearchClose() {
|
284
|
this.openaireSearchUtils.keyword = this.filterForm.get('keyword').value;
|
285
|
}
|
286
|
|
287
|
public resetInput() {
|
288
|
this.openaireSearchUtils.keyword = null;
|
289
|
this.searchInputComponent.reset()
|
290
|
}
|
291
|
|
292
|
handleError(message: string) {
|
293
|
UIkit.notification(message, {
|
294
|
status: 'danger',
|
295
|
timeout: 6000,
|
296
|
pos: 'bottom-right'
|
297
|
});
|
298
|
}
|
299
|
}
|