Newer
Older
import { Component, OnInit, ViewChild, ElementRef, EventEmitter, Output, Input } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TranslateService } from '@ngx-translate/core';
import { AppService } from '../../../service/app.service';
import { FunctionsService } from '../../../service/functions.service';
import { Observable } from 'rxjs';
import { FormControl } from '@angular/forms';
import { startWith, map, tap } from 'rxjs/operators';
import { LatinisePipe } from 'ngx-pipes';
import { MatExpansionPanel } from '@angular/material/expansion';
import { IndexingFieldsService } from '../../../service/indexing-fields.service';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-criteria-tool',
templateUrl: 'criteria-tool.component.html',
styleUrls: ['criteria-tool.component.scss', '../../indexation/indexing-form/indexing-form.component.scss']
})
export class CriteriaToolComponent implements OnInit {
loading: boolean = true;
criteria: any = [];
currentCriteria: any = [];
filteredCriteria: Observable<string[]>;
searchTermControl = new FormControl();
searchCriteria = new FormControl();
hideCriteriaList: boolean = true;
@Input() searchTerm: string = 'Foo';
@Input() defaultCriteria: any = [];
@Output() searchUrlGenerated = new EventEmitter<any>();
@ViewChild('criteriaTool', { static: false }) criteriaTool: MatExpansionPanel;
@ViewChild('searchCriteriaInput', { static: false }) searchCriteriaInput: ElementRef;
constructor(
private _activatedRoute: ActivatedRoute,
public translate: TranslateService,
public http: HttpClient,
public appService: AppService,
public functions: FunctionsService,
public indexingFields: IndexingFieldsService,
private latinisePipe: LatinisePipe) {
_activatedRoute.queryParams.subscribe(
params => {
this.searchTerm = params.value;
}
);
}
async ngOnInit(): Promise<void> {
// console.log('getAllFields()', await this.indexingFields.getAllFields());
this.searchTermControl.setValue(this.searchTerm);
this.criteria = await this.indexingFields.getAllFields();
this.criteria.forEach((element: any) => {
if (this.defaultCriteria.indexOf(element.identifier) > -1) {
element.control = new FormControl('');
this.addCriteria(element);
}
this.filteredCriteria = this.searchCriteria.valueChanges
.pipe(
startWith(''),
map(value => this._filter(value))
);
this.loading = false;
setTimeout(() => {
this.searchTermControl.valueChanges
.pipe(
startWith(''),
map(value => {
if (typeof value === 'string' && !this.functions.empty(value)) {
this.searchTerm = value;
}
})
).subscribe();
this.criteriaTool.open();
}, 500);
private _filter(value: string): string[] {
if (typeof value === 'string') {
const filterValue = this.latinisePipe.transform(value.toLowerCase());
return this.criteria.filter((option: any) => this.latinisePipe.transform(option['label'].toLowerCase()).includes(filterValue));
return this.criteria;
}
}
isCurrentCriteria(criteriaId: string) {
return this.currentCriteria.filter((currCrit: any) => currCrit.identifier === criteriaId).length > 0;
async addCriteria(criteria: any) {
criteria.control = new FormControl('');
await this.initField(criteria);
this.currentCriteria.push(criteria);
this.searchTermControl.setValue(this.searchTerm);
this.searchCriteria.reset();
// this.searchCriteriaInput.nativeElement.blur();
setTimeout(() => {
this.criteriaTool.open();
}, 0);
}
initField(field: any) {
try {
this['set_' + field.identifier + '_field'](field);
} catch (error) {
// console.log(error);
}
}
removeCriteria(index: number) {
this.currentCriteria.splice(index, 1);
if (this.currentCriteria.length === 0) {
this.criteriaTool.close();
}
getFilterControl() {
return this.searchCriteria;
}
getCriterias() {
return this.criteria;
}
getFilteredCriterias() {
return this.filteredCriteria;
}
focusFilter() {
this.hideCriteriaList = false;
setTimeout(() => {
this.searchCriteriaInput.nativeElement.focus();
}, 100);
}
getCurrentCriteriaValues() {
const objCriteria = {};
if (!this.functions.empty(this.searchTermControl.value)) {
objCriteria['quickSearch'] = {
values: this.searchTermControl.value
};
}
this.currentCriteria.forEach((field: any) => {
if (!this.functions.empty(field.control.value)) {
objCriteria[field.identifier] = {
values: field.control.value
};
}
});
this.searchUrlGenerated.emit(objCriteria);
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
this.criteriaTool.close();
}
getLabelValue(identifier: string, value: string) {
if (this.functions.empty(value)) {
return this.translate.instant('lang.undefined');
} else if (['doctype', 'destination'].indexOf(identifier) > -1) {
return this.criteria.filter((field: any) => field.identifier === identifier)[0].values.filter((val: any) => val.id === value)[0].title;
} else {
return this.criteria.filter((field: any) => field.identifier === identifier)[0].values.filter((val: any) => val.id === value)[0].label;
}
}
getLabelValues(identifier: string, values: string[]) {
if (values.length === 0) {
return this.translate.instant('lang.undefined');
} else if (['doctype', 'destination'].indexOf(identifier) > -1) {
return this.criteria.filter((field: any) => field.identifier === identifier)[0].values.filter((val: any) => values.indexOf(val.id) > -1).map((val: any) => val.title);
} else {
return this.criteria.filter((field: any) => field.identifier === identifier)[0].values.filter((val: any) => values.indexOf(val.id) > -1).map((val: any) => val.label);
}
}
refreshCriteria(criteria: any) {
this.currentCriteria.forEach((field: any, index: number) => {
if (criteria[field.identifier] !== undefined) {
field.control.setValue(criteria[field.identifier].values);
}
});
this.getCurrentCriteriaValues();
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
}
set_doctype_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/doctypes`).pipe(
tap((data: any) => {
let arrValues: any[] = [];
data.structure.forEach((doctype: any) => {
if (doctype['doctypes_second_level_id'] === undefined) {
arrValues.push({
id: doctype.doctypes_first_level_id,
label: doctype.doctypes_first_level_label,
title: doctype.doctypes_first_level_label,
disabled: true,
isTitle: true,
color: doctype.css_style
});
data.structure.filter((info: any) => info.doctypes_first_level_id === doctype.doctypes_first_level_id && info.doctypes_second_level_id !== undefined && info.description === undefined).forEach((secondDoctype: any) => {
arrValues.push({
id: secondDoctype.doctypes_second_level_id,
label: ' ' + secondDoctype.doctypes_second_level_label,
title: secondDoctype.doctypes_second_level_label,
disabled: true,
isTitle: true,
color: secondDoctype.css_style
});
arrValues = arrValues.concat(data.structure.filter((infoDoctype: any) => infoDoctype.doctypes_second_level_id === secondDoctype.doctypes_second_level_id && infoDoctype.description !== undefined).map((infoType: any) => {
return {
id: infoType.type_id,
label: ' ' + infoType.description,
title: infoType.description,
disabled: false,
isTitle: false,
};
}));
});
}
});
elem.values = arrValues;
elem.event = 'calcLimitDate';
resolve(true);
})
).subscribe();
});
}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
set_priority_field(elem: any) {
return new Promise((resolve, reject) => {
this.http.get(`../rest/priorities`).pipe(
tap((data: any) => {
elem.values = data.priorities;
resolve(true);
})
).subscribe();
});
}
set_destination_field(elem: any) {
let route = `../rest/indexingModels/entities`;
return new Promise((resolve, reject) => {
this.http.get(route).pipe(
tap((data: any) => {
let title = '';
elem.values = elem.values.concat(data.entities.map((entity: any) => {
title = entity.entity_label;
for (let index = 0; index < entity.level; index++) {
entity.entity_label = ' ' + entity.entity_label;
}
return {
id: entity.id,
title: title,
label: entity.entity_label,
disabled: false
};
}));
resolve(true);
})
).subscribe();
});
}