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

120 lines
3.9 KiB
TypeScript
Raw Normal View History

import {Component, EventEmitter, Input, OnChanges, Output, SimpleChanges} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {Filter} from "./searchHelperClasses.class";
@Component({
selector: 'quick-selections',
template: `
<form *ngIf="resultTypes" [formGroup]="control">
<div>
<mat-slide-toggle *ngIf="quickFilter"
class="example-margin" formControlName="QFselected" (change)="quickFilterChanged()">
{{quickFilter.value}}
</mat-slide-toggle>
</div>
<span *ngIf="resultTypes">
<span> <input type="checkbox" id="publ" name="Publications" formControlName="publication" (change)="changed()">Publications </span>
<span> <input type="checkbox" formControlName="dataset" (change)="changed()"> Research Data </span>
<span> <input type="checkbox" formControlName="software" (change)="changed()"> Software </span>
<span> <input type="checkbox" formControlName="other" (change)="changed()"> Other Research outcomes </span>
</span>
</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;
constructor(private _fb: FormBuilder) {
this.control = this._fb.group({
publication: true,
dataset: true,
software: true,
other: true,
QFselected: true
});
}
changed(typeChanged: boolean = true) {
if (!this.initialized && this.isDisabled) {
this.initialized = true;
return;
}
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;
// this.resultTypes.open = value.open;
if (typeChanged && 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 (value.QFselected) {
for (let filterValue of this.quickFilter.filter.values) {
filterValue.selected = (filterValue.name == this.quickFilter.value)
}
this.quickFilter.filter.countSelectedValues = 1;
} else {
for (let filterValue of this.quickFilter.filter.values) {
filterValue.selected = false;
}
this.quickFilter.filter.countSelectedValues = 0;
}
this.typeChange.emit({});
}
ngOnInit() {
if (this.resultTypes) {
this.setFormValues();
}
}
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
});
}
}