All files / src/app/shared/components/filter/filter-text filter-text.component.ts

69.56% Statements 16/23
53.33% Branches 8/15
60% Functions 6/10
70% Lines 14/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 702x     2x                                 2x   1x           1x 1x     1x             1x 1x     2x   2x                     1x   1x 1x                    
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
 
import { Facet } from 'ish-core/models/facet/facet.model';
import { Filter } from 'ish-core/models/filter/filter.model';
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
/**
 * The Filter Text Component displays a filter group. The facets of the filter group are presented as links with optional clear-button.
 *
 * @example
 * <ish-filter-text
 *               [filterElement]="element"
 *               (applyFilter)="applyFilter($event)"
 * </ish-filter-text>
 */
@Component({
  selector: 'ish-filter-text',
  templateUrl: './filter-text.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FilterTextComponent implements OnInit {
  @Input({ required: true }) filterElement: Filter;
  @Output() applyFilter: EventEmitter<{ searchParameter: URLFormParams }> = new EventEmitter();
 
  /**
   * two-way-binding (banana in a box) [(showAll)]="showAllElements[element.name]"
   */
  @Output()
  showAllChange = new EventEmitter<boolean>();
  private showAllValue = false;
  @Input()
  get showAll() {
    return this.showAllValue;
  }
  set showAll(val) {
    this.showAllValue = val;
    this.showAllChange.emit(this.showAllValue);
  }
 
  maxLevel = 0;
  facets: Facet[] = [];
 
  ngOnInit() {
    this.maxLevel = Math.max(...this.filterElement.facets.map(o => o.level)) || 0;
 
    this.facets = this.filterElement.facets.filter(x => x.selected || !this.maxLevel || x.level >= this.maxLevel);
  }
 
  filter(facet: Facet) {
    this.applyFilter.emit({ searchParameter: facet.searchParameter });
  }
 
  /**
   * sort selected to top, increase limitCount to selectedCount on showLess
   */
  getFacets() {
    const facets = [...this.facets];
 
    if (this.showAll || this.maxLevel >= 1 || this.filterElement.limitCount === -1) {
      return facets;
    }
 
    const selectedFacetsCount = facets.filter(x => x.selected).length;
 
    return facets
      .sort((a, b) => (a.selected > b.selected ? -1 : a.selected < b.selected ? 1 : 0))
      .slice(0, Math.max(this.filterElement.limitCount || 0, selectedFacetsCount));
  }
}