1
|
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
|
2
|
import {ActivatedRoute, Router} from '@angular/router';
|
3
|
import {SearchCommunityDataprovidersService} from '../../../openaireLibrary/connect/contentProviders/searchDataproviders.service';
|
4
|
import {EnvProperties} from '../../../openaireLibrary/utils/properties/env-properties';
|
5
|
import {
|
6
|
Constraint,
|
7
|
ContentProvider,
|
8
|
Criteria,
|
9
|
SelectionCriteria
|
10
|
} from '../../../openaireLibrary/utils/entities/contentProvider';
|
11
|
import {AbstractControl, FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
|
12
|
import {ManageCommunityContentProvidersService} from '../../../services/manageContentProviders.service';
|
13
|
import {Title} from '@angular/platform-browser';
|
14
|
import {properties} from "../../../../environments/environment";
|
15
|
import {Subscription} from "rxjs";
|
16
|
import {Option} from "../../../openaireLibrary/sharedComponents/input/input.component";
|
17
|
import {MatSlideToggleChange} from "@angular/material/slide-toggle";
|
18
|
import {HelperFunctions} from "../../../openaireLibrary/utils/HelperFunctions.class";
|
19
|
import {CriteriaUtils} from "../criteria-utils";
|
20
|
|
21
|
declare var UIkit;
|
22
|
|
23
|
@Component({
|
24
|
selector: 'criteria',
|
25
|
templateUrl: './criteria.component.html',
|
26
|
styleUrls: ['criteria.component.css'],
|
27
|
})
|
28
|
export class CriteriaComponent implements OnInit, OnDestroy {
|
29
|
public community: string = '';
|
30
|
public openaireId: string = '';
|
31
|
public dataProvider: ContentProvider = null;
|
32
|
public selectionCriteria: FormGroup;
|
33
|
public properties: EnvProperties = properties;
|
34
|
public fields: Option[] = CriteriaUtils.fields;
|
35
|
public verbs: Option[] = CriteriaUtils.verbs;
|
36
|
public loading = true;
|
37
|
/** Paging */
|
38
|
public page: number = 1;
|
39
|
public pageSize: number = 5;
|
40
|
private subs: any[] = [];
|
41
|
|
42
|
constructor(private route: ActivatedRoute, private router: Router,
|
43
|
private title: Title,
|
44
|
private searchCommunityDataprovidersService: SearchCommunityDataprovidersService,
|
45
|
private manageCommunityContentProvidersService: ManageCommunityContentProvidersService,
|
46
|
private cdr: ChangeDetectorRef,
|
47
|
private fb: FormBuilder) {
|
48
|
}
|
49
|
|
50
|
ngOnInit() {
|
51
|
this.subs.push(this.route.params.subscribe(params => {
|
52
|
this.community = params['community'];
|
53
|
this.route.params.subscribe(params => {
|
54
|
if (params['provider']) {
|
55
|
this.openaireId = params['provider'];
|
56
|
}
|
57
|
this.searchCommunityDataprovidersService.searchDataproviders(this.properties, this.community).subscribe(dataProviders => {
|
58
|
dataProviders.forEach(dataProvider => {
|
59
|
if (dataProvider.openaireId == this.openaireId) {
|
60
|
this.dataProvider = dataProvider;
|
61
|
this.title.setTitle(this.community.toUpperCase() + ' | Criteria for ' + this.dataProvider.officialname);
|
62
|
}
|
63
|
});
|
64
|
if (!this.dataProvider) {
|
65
|
this.navigateToError();
|
66
|
} else {
|
67
|
this.reset();
|
68
|
this.loading = false;
|
69
|
}
|
70
|
});
|
71
|
});
|
72
|
}));
|
73
|
}
|
74
|
|
75
|
public ngOnDestroy() {
|
76
|
this.subs.forEach(subscription => {
|
77
|
if (subscription instanceof Subscription) {
|
78
|
subscription.unsubscribe();
|
79
|
}
|
80
|
})
|
81
|
}
|
82
|
|
83
|
private navigateToError() {
|
84
|
this.router.navigate(['/error'], {queryParams: {'page': this.properties.baseLink + this.router.url}});
|
85
|
}
|
86
|
|
87
|
reset() {
|
88
|
this.page = 1;
|
89
|
if (this.dataProvider) {
|
90
|
this.selectionCriteria = this.fb.group({
|
91
|
criteria: this.fb.array([])
|
92
|
});
|
93
|
let selectionCriteria = this.dataProvider.selectioncriteria;
|
94
|
if (selectionCriteria) {
|
95
|
selectionCriteria.criteria.forEach(criterion => {
|
96
|
let constraintArray: FormArray = this.fb.array([]);
|
97
|
criterion.constraint.forEach(constraint => {
|
98
|
constraintArray.push(this.fb.group({
|
99
|
field: this.fb.control(constraint.field, Validators.required),
|
100
|
verb: this.fb.control(this.removeSuffix(constraint.verb), Validators.required),
|
101
|
value: this.fb.control(constraint.value, Validators.required),
|
102
|
verb_suffix: this.fb.control(this.getSuffix(constraint.verb))
|
103
|
}));
|
104
|
});
|
105
|
this.criteria.push(this.fb.group({
|
106
|
constraint: constraintArray
|
107
|
}));
|
108
|
});
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
get currentPage(): AbstractControl[] {
|
114
|
if (this.criteria) {
|
115
|
return this.criteria.controls.slice((this.page - 1) * this.pageSize, this.page * this.pageSize);
|
116
|
} else {
|
117
|
return [];
|
118
|
}
|
119
|
}
|
120
|
|
121
|
getIndex(index: number): number {
|
122
|
return (this.page - 1)*this.pageSize + index;
|
123
|
}
|
124
|
|
125
|
public get criteria(): FormArray {
|
126
|
return this.selectionCriteria.get('criteria') as FormArray;
|
127
|
}
|
128
|
|
129
|
public getConstraint(i: number): FormArray {
|
130
|
return this.criteria.at(i).get('constraint') as FormArray;
|
131
|
}
|
132
|
|
133
|
public addCriteria() {
|
134
|
let constraintArray: FormArray = this.fb.array([
|
135
|
this.fb.group({
|
136
|
field: this.fb.control('', Validators.required),
|
137
|
verb: this.fb.control('contains', Validators.required),
|
138
|
value: this.fb.control('', Validators.required),
|
139
|
verb_suffix: this.fb.control('_caseinsensitive')
|
140
|
})
|
141
|
]);
|
142
|
this.criteria.push(this.fb.group({
|
143
|
constraint: constraintArray
|
144
|
}));
|
145
|
this.page = Math.ceil(this.criteria.length/this.pageSize);
|
146
|
this.cdr.detectChanges();
|
147
|
}
|
148
|
|
149
|
public addConstraint(i: number) {
|
150
|
let constraintArray: FormArray = this.criteria.at(i).get('constraint') as FormArray;
|
151
|
constraintArray.push(this.fb.group({
|
152
|
field: this.fb.control('', Validators.required),
|
153
|
verb: this.fb.control('contains', Validators.required),
|
154
|
value: this.fb.control('', Validators.required),
|
155
|
verb_suffix: this.fb.control('_caseinsensitive')
|
156
|
}));
|
157
|
this.cdr.detectChanges();
|
158
|
}
|
159
|
|
160
|
public removeConstraint(i: number, j: number) {
|
161
|
let constraintArray: FormArray = this.criteria.at(i).get('constraint') as FormArray;
|
162
|
constraintArray.removeAt(j);
|
163
|
if (constraintArray.length === 0) {
|
164
|
this.criteria.removeAt(i);
|
165
|
if (this.currentPage.length === 0) {
|
166
|
this.page = 1;
|
167
|
}
|
168
|
}
|
169
|
this.cdr.detectChanges();
|
170
|
}
|
171
|
|
172
|
get dataProviderCriteria(): Criteria[] {
|
173
|
return (this.dataProvider && this.dataProvider.selectioncriteria && this.dataProvider.selectioncriteria.criteria)?this.dataProvider.selectioncriteria.criteria:[];
|
174
|
}
|
175
|
|
176
|
get dirty() {
|
177
|
if(!this.dataProvider || !this.criteria) {
|
178
|
return false;
|
179
|
} else if(this.criteria.length !== this.dataProviderCriteria.length) {
|
180
|
return true;
|
181
|
} else {
|
182
|
return this.dataProviderCriteria.filter((criterion, i) => {
|
183
|
if(criterion.constraint.length !== this.getConstraint(i).length) {
|
184
|
return true;
|
185
|
} else {
|
186
|
let temp = this.getConstraint(i).value;
|
187
|
return criterion.constraint.filter((constraint, j) => {
|
188
|
return constraint.field !== temp[j].field || constraint.verb !== (temp[j].verb + temp[j].verb_suffix) || constraint.value !== temp[j].value;
|
189
|
}).length > 0;
|
190
|
}
|
191
|
}).length > 0;
|
192
|
}
|
193
|
}
|
194
|
|
195
|
save() {
|
196
|
if (this.selectionCriteria.valid) {
|
197
|
this.loading = true;
|
198
|
this.dataProvider.selectioncriteria = this.parseForm(this.selectionCriteria.value);
|
199
|
this.manageCommunityContentProvidersService.saveContentProvider(this.properties, this.dataProvider).subscribe(() => {
|
200
|
this.reset();
|
201
|
this.loading = false;
|
202
|
UIkit.notification('Filters has been <b>successfully updated</b>', {
|
203
|
status: 'success',
|
204
|
timeout: 6000,
|
205
|
pos: 'bottom-right'
|
206
|
});
|
207
|
});
|
208
|
} else {
|
209
|
UIkit.notification('An error has been occurred. Try again later!', {
|
210
|
status: 'danger',
|
211
|
timeout: 6000,
|
212
|
pos: 'bottom-right'
|
213
|
});
|
214
|
}
|
215
|
}
|
216
|
|
217
|
caseSensitive(event: MatSlideToggleChange, constraint: AbstractControl) {
|
218
|
if(event.checked) {
|
219
|
constraint.get('verb_suffix').setValue('');
|
220
|
} else {
|
221
|
constraint.get('verb_suffix').setValue('_caseinsensitive');
|
222
|
}
|
223
|
}
|
224
|
|
225
|
removeSuffix(verb: string) {
|
226
|
return verb.replace('_caseinsensitive', '');
|
227
|
}
|
228
|
|
229
|
getSuffix(verb: string) {
|
230
|
if(verb.includes('_caseinsensitive')) {
|
231
|
return '_caseinsensitive';
|
232
|
} else {
|
233
|
return '';
|
234
|
}
|
235
|
}
|
236
|
|
237
|
parseForm(formValue): SelectionCriteria {
|
238
|
let value = HelperFunctions.copy(formValue);
|
239
|
let selectionCriteria: SelectionCriteria = new SelectionCriteria();
|
240
|
selectionCriteria.criteria = [];
|
241
|
value.criteria.forEach(criterion => {
|
242
|
let criteria = new Criteria();
|
243
|
criteria.constraint = [];
|
244
|
criterion.constraint.forEach(constraint => {
|
245
|
criteria.constraint.push(new Constraint(constraint.verb + constraint.verb_suffix, constraint.field, constraint.value));
|
246
|
});
|
247
|
selectionCriteria.criteria.push(criteria);
|
248
|
})
|
249
|
return selectionCriteria;
|
250
|
}
|
251
|
}
|