2018-10-05 08:56:50 +02:00
|
|
|
import { Component, ViewEncapsulation, OnInit, Input, Output, ViewChild, EventEmitter } from '@angular/core';
|
|
|
|
import { Observable } from 'rxjs/Observable';
|
|
|
|
import { FormControl } from '@angular/forms';
|
2018-10-05 12:03:22 +02:00
|
|
|
import { MatSelectionList, MatListOption } from '@angular/material';
|
|
|
|
import { SelectionModel } from '@angular/cdk/collections';
|
2018-10-05 08:56:50 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-facet-section-component',
|
|
|
|
templateUrl: './facet-search-section.component.html',
|
|
|
|
styleUrls: ['./facet-search-section.component.scss'],
|
|
|
|
encapsulation: ViewEncapsulation.None,
|
|
|
|
})
|
|
|
|
export class FacetSearchSectionComponent implements OnInit {
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
searchEnabled = false;
|
|
|
|
|
2018-10-08 17:14:27 +02:00
|
|
|
@Input()
|
|
|
|
requestDelay = 200;
|
|
|
|
|
2018-10-05 12:03:22 +02:00
|
|
|
@Input()
|
|
|
|
multipleSelect = true;
|
|
|
|
|
2018-10-05 08:56:50 +02:00
|
|
|
@Input()
|
|
|
|
filterOptions: (value) => Observable<any[]>;
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
options: Observable<any[]> = Observable.of([]);
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
displayTitleFunc: (value) => string;
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
displayValueFunc: (value) => string;
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
selectedChanged = new EventEmitter();
|
|
|
|
|
|
|
|
@Output()
|
|
|
|
optionRemoved = new EventEmitter();
|
|
|
|
|
|
|
|
optionSearchControl = new FormControl('');
|
|
|
|
|
|
|
|
private selectedOptions: any[] = [];
|
|
|
|
|
|
|
|
@ViewChild('optionsList') selectionList: MatSelectionList;
|
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2018-10-05 12:03:22 +02:00
|
|
|
if (!this.multipleSelect) { this.selectionList.selectedOptions = new SelectionModel<MatListOption>(this.multipleSelect); }
|
2018-10-08 17:14:27 +02:00
|
|
|
this.optionSearchControl.valueChanges.debounceTime(this.requestDelay)
|
|
|
|
.distinctUntilChanged().subscribe(x => { if (this.filterOptions) { this.options = this.filterOptions(x); } });
|
2018-10-05 08:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public selectionChanged(event: any) {
|
2018-10-05 12:03:22 +02:00
|
|
|
const eventValue = event.option.value;
|
|
|
|
if (event.option.selected) { this.selectedOptions.push(eventValue); }
|
|
|
|
if (!event.option.selected) {
|
|
|
|
const index = this.selectedOptions.map(x => this.displayValue(x)).indexOf(this.displayValue(eventValue));
|
|
|
|
this.selectedOptions.splice(index, 1);
|
|
|
|
}
|
2018-10-05 08:56:50 +02:00
|
|
|
this.selectedChanged.emit(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeOption(project) {
|
|
|
|
const list = this.selectionList.selectedOptions.selected.map(x => x.value);
|
|
|
|
const indexOfProject = list.indexOf(project);
|
|
|
|
if (this.selectionList.selectedOptions.selected[indexOfProject]) {
|
|
|
|
this.selectionList.selectedOptions.selected[indexOfProject].selected = false;
|
|
|
|
this.selectionList.selectedOptions.selected.splice(indexOfProject, 1);
|
|
|
|
}
|
2018-10-05 12:03:22 +02:00
|
|
|
this.selectedOptions.splice(this.selectedOptions.map(x => this.displayValue(x)).indexOf(this.displayValue(project)), 1);
|
2018-10-05 08:56:50 +02:00
|
|
|
this.optionRemoved.emit(project);
|
|
|
|
}
|
|
|
|
|
|
|
|
public isOptionSelected(value) {
|
2018-10-05 12:03:22 +02:00
|
|
|
return this.selectedOptions.map(x => this.displayValue(x)).indexOf(this.displayValue(value)) !== -1;
|
2018-10-05 08:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
displayLabel(value) {
|
|
|
|
return this.displayTitleFunc ? this.displayTitleFunc(value) : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
displayValue(value) {
|
|
|
|
return this.displayValueFunc ? this.displayValueFunc(value) : value;
|
|
|
|
}
|
2018-10-05 12:03:22 +02:00
|
|
|
|
2018-10-05 08:56:50 +02:00
|
|
|
}
|