metadata-validator-ui/src/app/pages/oaipmh-validator/validation-settings/oaipmh-validator.component.ts

136 lines
5.2 KiB
TypeScript

import {ChangeDetectorRef, Component, OnInit, ViewChild} from '@angular/core';
import {OaipmhValidatorService} from "../../../services/oaipmh-validator.service";
import {UntypedFormBuilder, UntypedFormGroup, Validators} from "@angular/forms";
import {Option} from "../../../openaire-library/sharedComponents/input/input.component";
import {StringUtils} from "../../../openaire-library/utils/string-utils.class";
import {JobResult} from "../../entities/JobResult";
import {Router} from "@angular/router";
import {Subscriber} from "rxjs";
declare var UIkit: any;
@Component({
selector: 'app-oaipmh-validator',
templateUrl: './oaipmh-validator.component.html',
styleUrls: ['./oaipmh-validator.component.less']
})
export class OaipmhValidatorComponent implements OnInit {
@ViewChild("right_sidebar") right_sidebar;
@ViewChild("right_sidebar_header") right_sidebar_header;
@ViewChild("right_sidebar_footer") right_sidebar_footer;
public options: Option[] = [
{label: 'OpenAIRE Guidelines for Data Archives Profile v2 & OpenAIRE FAIR Guidelines for Data Repositories Profile', value: 'OpenAIRE Guidelines for Data Archives Profile v2'},
{label: 'OpenAIRE Guidelines for Literature Repositories Profile v3', value: 'OpenAIRE Guidelines for Literature Repositories Profile v3'},
{label: 'OpenAIRE Guidelines for Literature Repositories Profile v4', value: 'OpenAIRE Guidelines for Literature Repositories Profile v4'},
{label: 'OpenAIRE FAIR Guidelines for Data Repositories Profile', value: 'OpenAIRE FAIR Guidelines for Data Repositories Profile'}
];
public form: UntypedFormGroup;
public sets: Option[] = [{label: 'All sets', value: 'all'}];
public recordsNum: number = 10;
public loadingSets: boolean = false;
public offset: number = 0;
public help: boolean = false;
public right_sidebar_body_height: number = 0;
public right_sidebar_card_height: number = 0;
subscriptions = [];
constructor(private fb: UntypedFormBuilder, private router: Router,
private cdr: ChangeDetectorRef,
private validator: OaipmhValidatorService) {
this.form = this.fb.group({
url: this.fb.control("", [Validators.required, StringUtils.urlValidator()]),//[Validators.required/*, Validators.email*/]),
guidelines: this.fb.control("", Validators.required),
recordsNum: this.fb.control(null, Validators.required),
set: this.fb.control('all', Validators.required)
});
}
ngOnInit() {}
ngAfterViewInit() {
if (typeof document !== 'undefined') {
if(document.getElementById("main-menu")) {
this.offset = Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--header-height'));
} else {
this.offset = 0;
}
}
}
ngOnDestroy() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscriber) {
subscription.unsubscribe();
}
});
}
openHelp() {
this.help = true;
this.cdr.detectChanges();
if (typeof document !== 'undefined') {
console.log(this.right_sidebar.nativeElement.offsetHeight);
console.log(this.right_sidebar_header.nativeElement.offsetHeight);
console.log(this.right_sidebar_footer.nativeElement.offsetHeight);
this.right_sidebar_card_height = this.right_sidebar.nativeElement.offsetHeight - this.right_sidebar_header.nativeElement.offsetHeight + 1;
this.right_sidebar_body_height = this.right_sidebar_card_height - this.right_sidebar_footer.nativeElement.offsetHeight;
}
}
updateRecordsNum(increase: boolean = true) {
this.recordsNum = this.recordsNum + (increase ? 10 : -10);
this.form.get('recordsNum').setValue(this.recordsNum);
}
getSets() {
this.loadingSets = true;
let options: Option[] = [{label: 'All sets', value: 'all'}];
if(this.form.get('setName')) {
this.form.get('setName').setValue('all');
}
if(this.form.get('url') && this.form.get('url').value && StringUtils.isValidUrl(this.form.get('url').value)) {
this.subscriptions.push(this.validator.getSets(this.form.get('url').value).subscribe(sets => {
for(let set of sets) {
options.push({label: set['setName'], value: set['setSpec']});
}
this.sets = options;
this.loadingSets = false;
}, error => {
this.sets = options;
this.loadingSets = false;
}));
} else {
this.sets = options;
this.loadingSets = false;
}
}
validate() {
this.subscriptions.push(this.validator.validate(this.form.get('guidelines').value, this.form.get('url').value, this.form.get('recordsNum').value, this.form.get('set').value).subscribe(
(result: JobResult) => {
this.router.navigate(['/oaipmh-history'], {
queryParams: { 'jobId': result.id }
});
},
error => {
// if(error.status == 400) {
UIkit.notification((error.error && error.error.message) ? error.error.message: error.message, {
status: 'danger',
timeout: 4000,
pos: 'bottom-right'
});
// }
// if(error.status == 422) {
// this.router.navigate(['/oaipmh-history'], {
// queryParams: { 'jobId': error.error.id }
// })
// }
}
));
}
}