openaire-library/searchPages/searchUtils/quick-selections.component.ts

161 lines
5.8 KiB
TypeScript

import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {Filter} from "./searchHelperClasses.class";
import {EnvProperties} from "../../utils/properties/env-properties";
import {ConfigurationService} from "../../utils/configuration/configuration.service";
import {Subject, Subscription} from "rxjs";
import {debounceTime} from "rxjs/operators";
@Component({
selector: 'quick-selections',
template: `
<form *ngIf="resultTypes || quickFilter" [formGroup]="control" class="uk-text-small uk-margin-small-bottom">
<div *ngIf="quickFilter" class="uk-margin-small-top uk-margin-small-left">
<span class="uk-text-bold">{{quickFilter.value}}</span>
<mat-slide-toggle
class="uk-margin-small-left" formControlName="QFselected" (change)="quickFilterChanged()">
</mat-slide-toggle>
</div>
<div *ngIf="resultTypes && showEntities" class="uk-margin-small-top uk-margin-small-left">
<span class="uk-text-muted">Include: </span>
<span>
<span *ngIf="showPublications" class="uk-margin-small-left"> <input type="checkbox" id="publ" name="Publications" formControlName="publication" (change)="changed()"> Publications </span>
<span *ngIf="showDatasets" class="uk-margin-small-left"> <input type="checkbox" formControlName="dataset" (change)="changed()"> Research data </span>
<span *ngIf="showSoftware" class="uk-margin-small-left"> <input type="checkbox" formControlName="software" (change)="changed()"> Software </span>
<span *ngIf="showOrp" class="uk-margin-small-left"> <input type="checkbox" formControlName="other" (change)="changed()"> Other research products </span>
</span>
</div>
</form>
`
})
export class QuickSelectionsComponent implements OnChanges {
@Input() resultTypes;
@Output() typeChange = new EventEmitter();
@Input() isDisabled;
@Input() quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string };
@Input() QFselected: boolean;
control: FormGroup;
initialized = false;
@Input() properties: EnvProperties;
showPublications:boolean= false;
showDatasets:boolean= false;
showSoftware: boolean = false;
showOrp: boolean = false;
showEntities = false;
resultTypesObs: Subscription ;
private clicks = new Subject();
constructor(private _fb: FormBuilder, private config: ConfigurationService) {
this.control = this._fb.group({
publication: true,
dataset: true,
software: true,
other: true,
QFselected: true
});
}
changed() {
if (!this.initialized && this.isDisabled) {
this.initialized = true;
return;
}
this.clicks.next();
}
actuallyChanged(){
let value = this.control.getRawValue();
this.resultTypes.publication = value.publication;
this.resultTypes.dataset = value.dataset;
this.resultTypes.software = value.software;
this.resultTypes.other = value.other;
if (this.resultTypes && !this.resultTypes.publication && !this.resultTypes.dataset && !this.resultTypes.software && !this.resultTypes.other) {
this.resultTypes.publication = true;
this.resultTypes.dataset = true;
this.resultTypes.software = true;
this.resultTypes.other = true;
this.setFormValues();
}
this.typeChange.emit({});
}
quickFilterChanged() {
let value = this.control.getRawValue();
this.quickFilter.selected = value.QFselected;
if(this.quickFilter.filter) {
this.quickFilter.filter.countSelectedValues = 0;
if (value.QFselected) {
for (let filterValue of this.quickFilter.filter.values) {
if((filterValue.name == this.quickFilter.value)) {
filterValue.selected = true
this.quickFilter.filter.countSelectedValues = 1;
}else{
filterValue.selected = false;
}
}
} else {
for (let filterValue of this.quickFilter.filter.values) {
filterValue.selected = false;
}
}
}
this.typeChange.emit({});
}
ngOnInit() {
if (this.resultTypes) {
this.setFormValues();
}
if(this.properties) {
this.config.getCommunityInformation(this.properties, this.properties.adminToolsCommunity).subscribe(data => {
var showEntity = {};
for (var i = 0; i < data['entities'].length; i++) {
showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
}
this.showPublications = showEntity["publication"];
this.showDatasets = showEntity["dataset"];
this.showSoftware = showEntity["software"];
this.showOrp = showEntity["orp"];
this.showEntities = this.showPublications || this.showDatasets || this.showSoftware || this.showOrp;
});
}
this.resultTypesObs = this.clicks.pipe(
debounceTime(1000)
).subscribe(e =>{this.actuallyChanged()} );
}
ngOnChanges(changes: SimpleChanges): void {
if (changes.isDisabled) {
if (changes.isDisabled.currentValue == true) {
this.control.disable();
} else if (changes.isDisabled.currentValue == false) {
this.control.enable();
}
}
if (changes.QFselected) {
let value = this.control.getRawValue();
if (changes.QFselected.currentValue != value.QFselected) {
this.setFormValues();
}
}
}
setFormValues() {
this.control.setValue({
publication: (this.resultTypes && this.resultTypes.publication)?this.resultTypes.publication:null,
dataset: (this.resultTypes && this.resultTypes.dataset)?this.resultTypes.dataset:null,
software: (this.resultTypes && this.resultTypes.software)?this.resultTypes.software:null,
other: (this.resultTypes && this.resultTypes.other)?this.resultTypes.other:null,
QFselected: this.QFselected
});
}
}