You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.9 KiB
TypeScript
93 lines
2.9 KiB
TypeScript
import { SelectionModel } from '@angular/cdk/collections';
|
|
import { Component, EventEmitter, Input, OnInit, Output, ViewChild, ViewEncapsulation } from '@angular/core';
|
|
import { FormControl } from '@angular/forms';
|
|
import { MatListOption, MatSelectionList } from '@angular/material';
|
|
import { Observable } from 'rxjs';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
import { BaseComponent } from '../../../../core/common/base/base.component';
|
|
|
|
@Component({
|
|
selector: 'app-facet-section-component',
|
|
templateUrl: './facet-search-section.component.html',
|
|
styleUrls: ['./facet-search-section.component.scss'],
|
|
encapsulation: ViewEncapsulation.None,
|
|
})
|
|
export class FacetSearchSectionComponent extends BaseComponent implements OnInit {
|
|
|
|
@Input()
|
|
searchEnabled = false;
|
|
|
|
@Input()
|
|
requestDelay = 200;
|
|
|
|
@Input()
|
|
multipleSelect = true;
|
|
|
|
@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 {
|
|
if (!this.multipleSelect) { this.selectionList.selectedOptions = new SelectionModel<MatListOption>(this.multipleSelect); }
|
|
this.optionSearchControl.valueChanges.debounceTime(this.requestDelay)
|
|
.distinctUntilChanged()
|
|
.pipe(takeUntil(this._destroyed))
|
|
.subscribe(x => { if (this.filterOptions) { this.options = this.filterOptions(x); } });
|
|
}
|
|
|
|
public selectionChanged(event: any) {
|
|
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);
|
|
}
|
|
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);
|
|
}
|
|
this.selectedOptions.splice(this.selectedOptions.map(x => this.displayValue(x)).indexOf(this.displayValue(project)), 1);
|
|
this.optionRemoved.emit(project);
|
|
}
|
|
|
|
public isOptionSelected(value) {
|
|
return this.selectedOptions.map(x => this.displayValue(x)).indexOf(this.displayValue(value)) !== -1;
|
|
}
|
|
|
|
displayLabel(value) {
|
|
return this.displayTitleFunc ? this.displayTitleFunc(value) : value;
|
|
}
|
|
|
|
displayValue(value) {
|
|
return this.displayValueFunc ? this.displayValueFunc(value) : value;
|
|
}
|
|
|
|
}
|