2022-03-31 13:57:13 +02:00
|
|
|
import {HttpClient} from "@angular/common/http";
|
2022-04-14 17:05:47 +02:00
|
|
|
import {Component, OnDestroy, OnInit} from "@angular/core";
|
2022-03-31 13:57:13 +02:00
|
|
|
import {Subscription} from "rxjs";
|
|
|
|
|
|
|
|
import {Breadcrumb} from "../openaireLibrary/utils/breadcrumbs/breadcrumbs.component";
|
|
|
|
import {EnvProperties} from "../openaireLibrary/utils/properties/env-properties";
|
|
|
|
import {properties} from "src/environments/environment";
|
2022-04-14 17:05:47 +02:00
|
|
|
import {FormBuilder, FormControl} from "@angular/forms";
|
|
|
|
import {ActivatedRoute} from "@angular/router";
|
|
|
|
import {Location} from "@angular/common";
|
2022-03-31 13:57:13 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'fos',
|
|
|
|
templateUrl: 'fos.component.html',
|
|
|
|
styleUrls: ['fos.component.css']
|
|
|
|
})
|
|
|
|
export class FosComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
public fos: any = [];
|
|
|
|
public index: number = 0;
|
2022-04-07 15:20:10 +02:00
|
|
|
|
|
|
|
public keywordControl: FormControl;
|
|
|
|
public keyword: string = null;
|
|
|
|
|
2022-04-14 17:05:47 +02:00
|
|
|
public viewResults = [];
|
|
|
|
|
|
|
|
public result = [];
|
2022-04-07 15:20:10 +02:00
|
|
|
|
2022-03-31 13:57:13 +02:00
|
|
|
properties: EnvProperties = properties;
|
2022-05-25 08:08:41 +02:00
|
|
|
public breadcrumbs: Breadcrumb[] = [{name: 'home', route: '/'}, {name: 'Fields of Science'}];
|
2022-03-31 13:57:13 +02:00
|
|
|
|
|
|
|
subscriptions: Subscription[] = [];
|
|
|
|
|
|
|
|
constructor(
|
2022-04-07 15:20:10 +02:00
|
|
|
private httpClient: HttpClient,
|
2022-04-14 17:05:47 +02:00
|
|
|
private fb: FormBuilder,
|
|
|
|
private location: Location,
|
|
|
|
private route: ActivatedRoute,
|
2022-03-31 13:57:13 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2022-04-14 17:05:47 +02:00
|
|
|
this.keywordControl = this.fb.control('');
|
2022-05-16 23:31:28 +02:00
|
|
|
this.httpClient.get(properties.domain+'/assets/vocabulary/fos.json').subscribe(data => {
|
2022-04-14 17:05:47 +02:00
|
|
|
this.fos = data['fos'];
|
|
|
|
this.subscriptions.push(this.route.queryParams.subscribe(params => {
|
|
|
|
if(params.keyword) {
|
|
|
|
this.keywordControl.setValue(params.keyword);
|
|
|
|
this.keyword = this.keywordControl.value;
|
|
|
|
this.findMatches();
|
|
|
|
}
|
|
|
|
}));
|
2022-03-31 13:57:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
|
|
|
for (let sub of this.subscriptions) {
|
|
|
|
sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
changeDisplayedFos(i) {
|
|
|
|
this.index = i;
|
2022-04-07 15:20:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
if(this.keywordControl.value) {
|
|
|
|
this.keyword = this.keywordControl.value;
|
2022-04-14 17:05:47 +02:00
|
|
|
// TODO: router.navigate();
|
|
|
|
this.location.go(window.location.pathname + '?keyword=' + this.keyword);
|
2022-04-07 15:20:10 +02:00
|
|
|
this.findMatches();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
findMatches() {
|
2022-04-14 17:05:47 +02:00
|
|
|
console.log(this.keyword);
|
|
|
|
this.viewResults = JSON.parse(JSON.stringify(this.fos));
|
|
|
|
let matchLevel1: boolean = false;
|
|
|
|
let matchLevel2: boolean = false;
|
2022-04-07 15:20:10 +02:00
|
|
|
// 1st level search
|
2022-04-14 17:05:47 +02:00
|
|
|
if(this.viewResults.length) {
|
|
|
|
this.viewResults = this.viewResults.filter(item => {
|
|
|
|
if(item.id.includes(this.keyword.toLowerCase())) {
|
|
|
|
matchLevel1 = true;
|
|
|
|
} else {
|
|
|
|
matchLevel1 = false;
|
|
|
|
}
|
|
|
|
// // 2nd level search
|
|
|
|
if(item.children?.length && !matchLevel1) {
|
|
|
|
item.children = item.children.filter(subItem => {
|
|
|
|
if(subItem.id.includes(this.keyword.toLowerCase())) {
|
|
|
|
matchLevel2 = true;
|
|
|
|
} else {
|
|
|
|
matchLevel2 = false;
|
|
|
|
}
|
|
|
|
// 3rd level search
|
|
|
|
if(subItem.children?.length && !matchLevel2) {
|
|
|
|
subItem.children = subItem.children.filter(subSubItem => subSubItem.id.includes(this.keyword.toLowerCase()));
|
|
|
|
}
|
|
|
|
return subItem.children?.length > 0 || matchLevel2;
|
|
|
|
});
|
2022-04-07 15:20:10 +02:00
|
|
|
}
|
2022-04-14 17:05:47 +02:00
|
|
|
return item.children?.length > 0;
|
2022-04-07 15:20:10 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 08:42:37 +02:00
|
|
|
highlightKeyword(name) {
|
2022-04-14 17:05:47 +02:00
|
|
|
if(name.includes(this.keyword.toLowerCase())) {
|
2022-04-13 08:42:37 +02:00
|
|
|
return name.replace(new RegExp(this.keyword, "gi"), (matchedValue) => `<mark class="highlighted">${matchedValue}</mark>`);
|
|
|
|
} else {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 15:20:10 +02:00
|
|
|
clearKeyword() {
|
|
|
|
this.keyword = null;
|
2022-04-14 17:05:47 +02:00
|
|
|
this.keywordControl.setValue('');
|
|
|
|
this.location.go(window.location.pathname);
|
2022-03-31 13:57:13 +02:00
|
|
|
}
|
|
|
|
}
|