File

projects/storefrontlib/cms-components/product/product-list/product-facet-navigation/facet/facet.component.ts

Metadata

changeDetection ChangeDetectionStrategy.OnPush
selector cx-facet
templateUrl ./facet.component.html

Index

Properties
Methods
Inputs
HostBindings
Accessors

Constructor

constructor(facetService: FacetService, elementRef: ElementRef, cd: ChangeDetectorRef)
Parameters :
Name Type Optional
facetService FacetService No
elementRef ElementRef<HTMLElement> No
cd ChangeDetectorRef No

Inputs

collapseIcon
Type : ICON_TYPE
Default value : ICON_TYPE.COLLAPSE
expandIcon
Type : ICON_TYPE
Default value : ICON_TYPE.EXPAND

configurable icon that is used to collapse the facet group

facet

HostBindings

class.multi-select
Type : boolean

Methods

decreaseVisibleValues
decreaseVisibleValues()

Decreases the number of visible values for the facet. This is delegated to facetService.decreaseVisibleValues.

Returns : void
getLinkParams
getLinkParams(value: FacetValue)
Parameters :
Name Type Optional
value FacetValue No
Returns : any
increaseVisibleValues
increaseVisibleValues()

Increases the number of visible values for the facet. This is delegated to facetService.increaseVisibleValues.

Returns : void
openLink
openLink(event: KeyboardEvent)
Parameters :
Name Type Optional
event KeyboardEvent No
Returns : void
toggleGroup
toggleGroup(event: UIEvent)

Handles clicking the heading of the facet group, which means toggling the visibility of the group (collapse / expand) and optionally focusing the group.

Parameters :
Name Type Optional
event UIEvent No
Returns : void

Properties

Protected _facet
Type : Facet
collapseIcon
Type : ICON_TYPE
Default value : ICON_TYPE.COLLAPSE
Decorators :
@Input()
expandIcon
Type : ICON_TYPE
Default value : ICON_TYPE.EXPAND
Decorators :
@Input()

configurable icon that is used to collapse the facet group

isMultiSelect
Type : boolean
Decorators :
@HostBinding('class.multi-select')
keyboardFocus
Type : FocusDirective
Decorators :
@ViewChild(FocusDirective)
state$
Type : Observable<FacetCollapseState>
values
Type : QueryList<ElementRef<HTMLElement>>
Decorators :
@ViewChildren('facetValue')

Accessors

facet
getfacet()
setfacet(value: Facet)
Parameters :
Name Type Optional
value Facet No
Returns : void
isExpanded
getisExpanded()
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  ElementRef,
  HostBinding,
  Input,
  QueryList,
  ViewChild,
  ViewChildren,
} from '@angular/core';
import { Facet, FacetValue } from '@spartacus/core';
import { Observable } from 'rxjs';
import { ICON_TYPE } from '../../../../../cms-components/misc/icon/icon.model';
import { FocusDirective } from '../../../../../layout/a11y/keyboard-focus/focus.directive';
import { FacetCollapseState } from '../facet.model';
import { FacetService } from '../services/facet.service';

@Component({
  selector: 'cx-facet',
  templateUrl: './facet.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FacetComponent {
  protected _facet: Facet;

  state$: Observable<FacetCollapseState>;

  /** configurable icon that is used to collapse the facet group  */
  @Input() expandIcon: ICON_TYPE = ICON_TYPE.EXPAND;
  @Input() collapseIcon: ICON_TYPE = ICON_TYPE.COLLAPSE;

  @HostBinding('class.multi-select') isMultiSelect: boolean;

  @ViewChildren('facetValue') values: QueryList<ElementRef<HTMLElement>>;

  @ViewChild(FocusDirective) keyboardFocus: FocusDirective;

  @Input()
  set facet(value: Facet) {
    this._facet = value;
    this.isMultiSelect = !!value.multiSelect;
    this.state$ = this.facetService.getState(value);
  }

  get facet(): Facet {
    return this._facet;
  }

  constructor(
    protected facetService: FacetService,
    protected elementRef: ElementRef<HTMLElement>,
    protected cd: ChangeDetectorRef
  ) {}

  /**
   * Handles clicking the heading of the facet group, which means toggling
   * the visibility of the group (collapse / expand) and optionally focusing
   * the group.
   */
  toggleGroup(event: UIEvent) {
    const host: HTMLElement = this.elementRef.nativeElement;
    const isLocked = this.keyboardFocus?.isLocked;

    this.facetService.toggle(this.facet, this.isExpanded);

    if (!isLocked || this.isExpanded) {
      host.focus();
      // we stop propagating the event as otherwise the focus on the host will trigger
      // an unlock event from the LockFocus directive.
      event.stopPropagation();
    }
  }

  get isExpanded(): boolean {
    return this.values.first.nativeElement.offsetParent !== null;
  }

  openLink(event: KeyboardEvent) {
    (event.target as HTMLElement).click();
    event.preventDefault();
  }

  /**
   * Increases the number of visible values for the facet. This is delegated
   * to `facetService.increaseVisibleValues`.
   */
  increaseVisibleValues(): void {
    this.facetService.increaseVisibleValues(this.facet);
  }

  /**
   * Decreases the number of visible values for the facet. This is delegated
   * to `facetService.decreaseVisibleValues`.
   */
  decreaseVisibleValues(): void {
    this.facetService.decreaseVisibleValues(this.facet);
  }

  getLinkParams(value: FacetValue) {
    return this.facetService.getLinkParams(value.query?.query.value);
  }
}
<ng-container *ngIf="state$ | async as state">
  <button class="heading" (click)="toggleGroup($event)">
    {{ facet.name }}
    <cx-icon class="collapse-icon" [type]="collapseIcon"></cx-icon>
    <cx-icon class="expand-icon" [type]="expandIcon"></cx-icon>
  </button>

  <a
    *ngFor="let value of facet.values | slice: 0:state.topVisible"
    #facetValue
    routerLink="./"
    [queryParams]="getLinkParams(value)"
    class="value"
    [class.selected]="value.selected"
    [cxFocus]="value.name"
    (keydown.space)="openLink($event)"
  >
    <span>
      <span class="label">{{ value.name }}</span>
      <span class="count">{{ value.count }}</span>
    </span>
  </a>

  <div class="more">
    <a
      *ngFor="
        let value of facet.values | slice: state.topVisible:state.maxVisible
      "
      #facetValue
      routerLink="./"
      [queryParams]="getLinkParams(value)"
      class="value"
      [class.selected]="value.selected"
      [cxFocus]="value.name"
      (keydown.space)="openLink($event)"
    >
      <span
        >{{ value.name }}<span class="count">{{ value.count }}</span></span
      >
    </a>

    <button
      *ngIf="state.maxVisible > state.topVisible"
      (click)="decreaseVisibleValues()"
      class="cx-action-link"
      cxFocus="moreorless"
    >
      {{ 'productList.showLess' | cxTranslate }}
    </button>

    <button
      *ngIf="state.maxVisible > 0 && state.maxVisible < facet.values.length"
      (click)="increaseVisibleValues()"
      class="cx-action-link"
      cxFocus="moreorless"
    >
      {{ 'productList.showMore' | cxTranslate }}
    </button>
  </div>
</ng-container>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""