All files / filter-bar si-filter-bar.component.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 9/9
100% Lines 18/18

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                  1x                 12x 12x             12x                 12x           12x           12x             12x                 12x 12x           12x   12x     4x       1x 1x       1x 1x       4x          
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { booleanAttribute, Component, input, model, output, viewChild } from '@angular/core';
import {
  SiAutoCollapsableListAdditionalContentDirective,
  SiAutoCollapsableListDirective,
  SiAutoCollapsableListItemDirective,
  SiAutoCollapsableListOverflowItemDirective
} from '@siemens/element-ng/auto-collapsable-list';
import { BackgroundColorVariant } from '@siemens/element-ng/common';
import { SiTranslatePipe, t } from '@siemens/element-translate-ng/translate';
 
import { Filter } from './filter';
import { SiFilterPillComponent } from './si-filter-pill.component';
 
@Component({
  selector: 'si-filter-bar',
  imports: [
    SiAutoCollapsableListDirective,
    SiAutoCollapsableListItemDirective,
    SiAutoCollapsableListOverflowItemDirective,
    SiAutoCollapsableListAdditionalContentDirective,
    SiFilterPillComponent,
    SiTranslatePipe
  ],
  templateUrl: './si-filter-bar.component.html',
  styleUrl: './si-filter-bar.component.scss',
  host: {
    '[class.reset]': 'allowReset()'
  }
})
export class SiFilterBarComponent {
  /**
   * Custom text if no filters are selected.
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILTER_BAR.NO_FILTERS:No filters applied`)
   * ```
   */
  readonly filterDefaultText = input(
    t(() => $localize`:@@SI_FILTER_BAR.NO_FILTERS:No filters applied`)
  );
  /**
   * Array of filter items to show
   *
   * @defaultValue []
   */
  readonly filters = model<Filter[]>([]);
  /**
   * Text for reset button
   *
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILTER_BAR.RESET_FILTERS:Reset filters`)
   * ```
   */
  readonly resetText = input(t(() => $localize`:@@SI_FILTER_BAR.RESET_FILTERS:Reset filters`));
  /**
   * Set false to hide reset filters button
   *
   * @defaultValue true
   */
  readonly allowReset = input(true, { transform: booleanAttribute });
  /**
   * Color to use for component background
   *
   * @defaultValue 'base-1'
   */
  readonly colorVariant = input<BackgroundColorVariant>('base-1');
 
  /**
   * Set to true to disable component
   *
   * @defaultValue false
   */
  readonly disabled = input(false, { transform: booleanAttribute });
 
  /**
   * Custom text for the collapsed filters.
   * @defaultValue
   * ```
   * t(() => $localize`:@@SI_FILTER_BAR.COLLAPSED_FILTERS_DESCRIPTION:+ {{count}} filters`)
   * ```
   */
  readonly collapsedFiltersDescription = input(
    t(() => $localize`:@@SI_FILTER_BAR.COLLAPSED_FILTERS_DESCRIPTION:+ {{count}} filters`)
  );
 
  /**
   * Output callback to be executed when the reset filter is clicked
   */
  readonly resetFilters = output<void>();
 
  private readonly collapsableListDirective = viewChild.required(SiAutoCollapsableListDirective);
 
  protected deleteFilters(deletedPill: Filter): void {
    this.filters.set(this.filters().filter(filter => filter !== deletedPill));
  }
 
  protected onResetFilters(): void {
    this.filters.set([]);
    this.resetFilters.emit();
  }
 
  protected deleteOverflowFilter(): void {
    this.filters.update(filters =>
      filters.slice(
        0,
        this.collapsableListDirective()
          .items()
          .findIndex(item => !item.isVisible())
      )
    );
  }
}